//Zrodlo: //http://codeforces.com/blog/entry/12143? // #include<bits/stdc++.h> #include"palindromy.h" #include"message.h" using namespace std; # ifdef DEB # define debug(...) fprintf(stderr, __VA_ARGS__) # else # define debug(...) #endif #define F first #define S second #define MP make_pair #define PB push_back #define LL long long #define LD long double #define PII pair<int, int> #define PLL pair<LL, LL> #define pb pop_back #define VI vector<int> #define VPI vector<PII> #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define FORD(i,a,b) for(int i = (a); i >= (b); i--) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define ALL(x) (x).begin(), (x).end() #define siz(V) ((int)V.size()) int n; LL res; vector<char> s; int main() { if(MyNodeId()>0) return 0; n=GetLength(); REP(i, n) s.PB(GetLetter(i)); vector<int> p(n,0); for(int z=0,l=0,r=0; z<2; z++,l=0,r=0) { for(int i=0; i<n; i++) { if(i<r) p[i]=min(r-i+!z,p[l+r-i+!z]); int L=i-p[i], R=i+p[i]-!z; while(L-1>=0 && R+1<n && s[L-1]==s[R+1]) p[i]++,L--,R++; if(R>r) l=L,r=R; } REP(i, siz(p)) { res+=p[i]+z; p[i]=0; } } printf("%lld\n", res); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | //Zrodlo: //http://codeforces.com/blog/entry/12143? // #include<bits/stdc++.h> #include"palindromy.h" #include"message.h" using namespace std; # ifdef DEB # define debug(...) fprintf(stderr, __VA_ARGS__) # else # define debug(...) #endif #define F first #define S second #define MP make_pair #define PB push_back #define LL long long #define LD long double #define PII pair<int, int> #define PLL pair<LL, LL> #define pb pop_back #define VI vector<int> #define VPI vector<PII> #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define FORD(i,a,b) for(int i = (a); i >= (b); i--) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define ALL(x) (x).begin(), (x).end() #define siz(V) ((int)V.size()) int n; LL res; vector<char> s; int main() { if(MyNodeId()>0) return 0; n=GetLength(); REP(i, n) s.PB(GetLetter(i)); vector<int> p(n,0); for(int z=0,l=0,r=0; z<2; z++,l=0,r=0) { for(int i=0; i<n; i++) { if(i<r) p[i]=min(r-i+!z,p[l+r-i+!z]); int L=i-p[i], R=i+p[i]-!z; while(L-1>=0 && R+1<n && s[L-1]==s[R+1]) p[i]++,L--,R++; if(R>r) l=L,r=R; } REP(i, siz(p)) { res+=p[i]+z; p[i]=0; } } printf("%lld\n", res); return 0; } |