Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int,int>
- #define vec vector
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvl(vector <ll> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvv(vector <vector <int> > &v){
- for (auto x: v) cv(x);
- cout<<"\n";
- }
- void cvb(vector <bool> v){
- for (bool x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvs(vector <string> v){
- for (auto a: v){
- cout<<a<<"\n";
- }
- }
- void cvp(vector <pii> a){
- for (auto p: a){
- cout<<p.first<<' '<<p.second<<"\n";
- }
- cout<<"\n";
- }
- ll mod=1e9+7, p =101;
- bool sh=0;
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- string s; cin>>s; int n = s.size();
- vector <ll> pf(n), powp(n,1);
- for (int i = 1; i < n; ++i) {
- powp[i] = powp[i - 1] * p % mod;
- }
- pf[0] = s[0] - 'a' + 1;
- for (int i = 1; i < n; ++i){
- pf[i] = pf[i - 1] + (s[i] - 'a' + 1) * powp[i];
- pf[i] %= mod;
- }
- if (sh){
- cout<<"s\n";
- cout<<s<<"\n";
- cout<<"pf\n";
- cvl(pf);
- }
- vector <int> ans;
- //отдельно проверить a = 0
- int b=0;
- for (int a = 1; a <= n; ++a){
- //if ((n - a - b) % (a + b) != 0) continue;
- //[0;a-1] - suf, [n-b-1; n - 1] - pref
- ll A,B;
- bool ok = 1;
- if (sh){
- cout<<"a b = "<<a<<' '<<b<<"\n";
- }
- int id;
- for (int i = a; i + (a + b) - 1 < n - b; i += a ){//+-1 после i + (a + b) ТАК СИЛЬНО ВСЕ МЕНЯЕТ -- ПОЧЕМУ???
- id = i;
- if (sh){
- cout<<"l r = "<<i<<' '<<i + a + b - 1<<"\n";
- }
- A = pf[a - 1] * powp[i] % mod;
- B = (pf[i + a - 1] - pf[i - 1] + mod) % mod;
- if (A != B){
- ok=0;
- break;
- }
- }
- B = (pf[n - 1] - pf[id - 1] + mod) % mod;
- A = pf[n - id - 1] * powp[id] % mod;
- if (A != B){
- ok = 0;
- }
- if (ok){
- //cout<<"OK\n";
- //cout<<"a b = "<<a<<' '<<b<<"\n";
- ans.push_back(a+b);
- }
- }
- int a = 0;
- for (int b = 1; b <= n; ++b){
- //if ((n - a - b) % (a + b) != 0) continue;
- //[0;a-1] - suf, [n-b-1; n - 1] - pref
- ll A,B;
- bool ok = 1;
- if (sh){
- cout<<"a b = "<<a<<' '<<b<<"\n";
- }
- int id;
- for (int i = n - b; i - b >= 0; i -= b){//+-1 после i + (a + b) ТАК СИЛЬНО ВСЕ МЕНЯЕТ -- ПОЧЕМУ???
- id = i;
- if (sh){
- cout<<"l r = "<<i<<' '<<i + a + b - 1<<"\n";
- }
- //A - база с конца
- A = (pf[n - 1] - pf[n - b - 1] + mod) % mod;
- B = (pf[i + b - 1] - pf[i - 1] + mod) % mod * powp[n - b] % mod;
- if (A != B){
- ok=0;
- break;
- }
- }
- B = pf[id - 1] * powp[n - b] % mod;
- A = (pf[n - b + id - 1] - pf[n - b - 1] + mod) % mod;
- if (A != B){
- ok = 0;
- }
- if (ok){
- //cout<<"OK\n";
- //cout<<"a b = "<<a<<' '<<b<<"\n";
- ans.push_back(a+b);
- }
- }
- for (int a = 1; a <= n; ++a){
- for (int b = 1; b <= n - a; ++b){
- //if ((n - a - b) % (a + b) != 0) continue;
- //[0;a-1] - suf, [n-b-1; n - 1] - pref
- bool ok = 1;
- if (sh){
- cout<<"a b = "<<a<<' '<<b<<"\n";
- }
- for (int i = a; i + (a + b) - 1 < n - b; i += a + b){//+-1 после i + (a + b) ТАК СИЛЬНО ВСЕ МЕНЯЕТ -- ПОЧЕМУ???
- if (sh){
- cout<<"l r = "<<i<<' '<<i + a + b - 1<<"\n";
- }
- ll A = (pf[a - 1] * powp[n - b] % mod + (pf[n - 1] - pf[n - b - 1] + mod) % mod) % mod;
- ll B = (pf[i + a + b - 1] - pf[i - 1] + mod) * powp[n - (i + a + b - 1)] % mod;
- if (A != B){
- ok=0;
- break;
- }
- }
- if (ok){
- //cout<<"OK\n";
- //cout<<"a b = "<<a<<' '<<b<<"\n";
- ans.push_back(a+b);
- }
- }
- }
- sort(ans.begin(), ans.end());
- if (sh){
- cout<<"ans\n";
- cv(ans);
- }
- cout<<ans[0];
- }
- /*
- abaabaabaa
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement