#include<bits/stdc++.h> #define ALL(X) X.begin(),X.end() #define FORW(I,A,B) for(int (I) = (A); (I) < (B); (I)++) #define FORD(I,A,B) for(int (I) = (A); (I) >= (B); (I)--) #define CLEAR(X) memset(X,0,sizeof(X)) #define SIZE(X) int(X.size()) #define CONTAINS(A,X) (A.find(X) != A.end()) #define PB push_back #define MP make_pair #define X first #define Y second using namespace std; template<typename T, typename U> ostream& operator << (ostream& os, const pair<T, U> &_p) { return os << "(" << _p.X << "," << _p.Y << ")"; } template<typename T> ostream& operator << (ostream &os, const vector<T>& _V) { bool f = true; os << "["; for(auto v: _V) { os << (f ? "" : ",") << v; f = false; } return os << "]"; } template<typename T> ostream& operator << (ostream &os, const set<T>& _S) { bool f = true; os << "("; for(auto s: _S) { os << (f ? "" : ",") << s; f = false; } return os << ")"; } template<typename T, typename U> ostream& operator << (ostream &os, const map<T, U>& _M) { return os << set<pair<T, U>>(ALL(_M)); } typedef signed long long slong; typedef long double ldouble; const slong INF = 1000000100; const ldouble EPS = 1e-9; const slong inf = INF * INF; #include "palindromy.h" #include "message.h" slong n, proc, id; #define MASTER 0 #define cerr if(0)cout #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) // https://github.com/stjepang/snippets/blob/master/manacher.cpp // Finds all palindromes in a string // // Given a string s of length N, finds all palindromes as its substrings. // // After calling manacher(s, N, rad), rad[x] will be the radius of the largest // palindrome centered at index x / 2. // Example: // s = b a n a n a a // rad = 0000102010010 // // Note: Array rad must be of length at least twice the length of the string. // Also, "invalid" characters are denoted by -1, therefore the string must not // contain such characters. // // Time complexity: O(N) // // Constants to configure: // - MAX is the maximum length of the string const int MAX = 10004242; int R[2*MAX+42]; char S[MAX+4242]; slong ans = 0; void manacher(char *s, int N, int *rad) { static char t[2*MAX]; int m = 2*N - 1; REP(i, m) t[i] = -1; REP(i, N) t[2*i] = s[i]; int x = 0; FOR(i, 1, m) { int &r = rad[i] = 0; if (i <= x+rad[x]) r = min(rad[x+x-i], x+rad[x]-i); while (i-r-1 >= 0 && i+r+1 < m && t[i-r-1] == t[i+r+1]) ++r; if (i+r >= x+rad[x]) x = i; } REP(i, m) if (i-rad[i] == 0 || i+rad[i] == m-1) ++rad[i]; REP(i, m) { rad[i] /= 2; //cout << "dla " << i << " promien " << rad[i] << endl; ans += rad[i]; } } int main() { long long N = GetLength(); if(MyNodeId() > 0) { return 0; } long long res = 0; for (int i = 0; i < N; ++i) { S[i] = GetLetter(i); } //printf("%s\n", S); ans = N; manacher(S,N,R); cout << ans << endl; 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | #include<bits/stdc++.h> #define ALL(X) X.begin(),X.end() #define FORW(I,A,B) for(int (I) = (A); (I) < (B); (I)++) #define FORD(I,A,B) for(int (I) = (A); (I) >= (B); (I)--) #define CLEAR(X) memset(X,0,sizeof(X)) #define SIZE(X) int(X.size()) #define CONTAINS(A,X) (A.find(X) != A.end()) #define PB push_back #define MP make_pair #define X first #define Y second using namespace std; template<typename T, typename U> ostream& operator << (ostream& os, const pair<T, U> &_p) { return os << "(" << _p.X << "," << _p.Y << ")"; } template<typename T> ostream& operator << (ostream &os, const vector<T>& _V) { bool f = true; os << "["; for(auto v: _V) { os << (f ? "" : ",") << v; f = false; } return os << "]"; } template<typename T> ostream& operator << (ostream &os, const set<T>& _S) { bool f = true; os << "("; for(auto s: _S) { os << (f ? "" : ",") << s; f = false; } return os << ")"; } template<typename T, typename U> ostream& operator << (ostream &os, const map<T, U>& _M) { return os << set<pair<T, U>>(ALL(_M)); } typedef signed long long slong; typedef long double ldouble; const slong INF = 1000000100; const ldouble EPS = 1e-9; const slong inf = INF * INF; #include "palindromy.h" #include "message.h" slong n, proc, id; #define MASTER 0 #define cerr if(0)cout #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) // https://github.com/stjepang/snippets/blob/master/manacher.cpp // Finds all palindromes in a string // // Given a string s of length N, finds all palindromes as its substrings. // // After calling manacher(s, N, rad), rad[x] will be the radius of the largest // palindrome centered at index x / 2. // Example: // s = b a n a n a a // rad = 0000102010010 // // Note: Array rad must be of length at least twice the length of the string. // Also, "invalid" characters are denoted by -1, therefore the string must not // contain such characters. // // Time complexity: O(N) // // Constants to configure: // - MAX is the maximum length of the string const int MAX = 10004242; int R[2*MAX+42]; char S[MAX+4242]; slong ans = 0; void manacher(char *s, int N, int *rad) { static char t[2*MAX]; int m = 2*N - 1; REP(i, m) t[i] = -1; REP(i, N) t[2*i] = s[i]; int x = 0; FOR(i, 1, m) { int &r = rad[i] = 0; if (i <= x+rad[x]) r = min(rad[x+x-i], x+rad[x]-i); while (i-r-1 >= 0 && i+r+1 < m && t[i-r-1] == t[i+r+1]) ++r; if (i+r >= x+rad[x]) x = i; } REP(i, m) if (i-rad[i] == 0 || i+rad[i] == m-1) ++rad[i]; REP(i, m) { rad[i] /= 2; //cout << "dla " << i << " promien " << rad[i] << endl; ans += rad[i]; } } int main() { long long N = GetLength(); if(MyNodeId() > 0) { return 0; } long long res = 0; for (int i = 0; i < N; ++i) { S[i] = GetLetter(i); } //printf("%s\n", S); ans = N; manacher(S,N,R); cout << ans << endl; return 0; } |