#include <bits/stdc++.h> #include "message.h" #include "palindromy.h" #define FOR(i,b,e) for(int i=(b); i <= (e); ++i) #define FORD(i,b,e) for(int i=(b); i >= (e); --i) #define SIZE(c) (int) (c).size() #define FORE(i,c) FOR(i,0,SIZE(c)-1) #define FORDE(i,c) FORD(i,SIZE(c)-1,0) #define pb push_back #define mp make_pair #define st first #define nd second using namespace std; typedef long long ll; typedef pair <int,int> pii; typedef pair <ll,ll> pll; typedef vector <int> VI; typedef vector <bool> VB; typedef vector <pii> VP; typedef vector <ll> VL; typedef vector <pll> VPL; typedef vector <VI> VVI; typedef vector <VL> VVL; typedef vector <VB> VVB; typedef vector <VP> VVP; const int MOD = 1000000007; const int INF = 1000000001; const ll LINF = 1000000000000000001LL; /*************************************************************************/ VI manacher(int n, const string &s, bool even) { VI res(n); int i = 0, t = 0; while (i < n) { while (s[i+1-t] == s[i+1+t+even]) ++t; res[i] = t; int k = 1; while (k < t && res[i-k] != res[i]-k) { res[i+k] = min(res[i-k], res[i]-k); ++k; } t = max(0,t-k); i += k; } return res; } /*************************************************************************/ int main() { if (MyNodeId() != 0) { return 0; } int n = GetLength(); string s = "#"; FOR(i,0,n-1) { s.pb(GetLetter(i)); } s.pb('$'); ll ans = 0; FOR(even,0,1) { VI result = manacher(n, s, even); for (int r : result) { ans += r; } } cout << ans; 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 | #include <bits/stdc++.h> #include "message.h" #include "palindromy.h" #define FOR(i,b,e) for(int i=(b); i <= (e); ++i) #define FORD(i,b,e) for(int i=(b); i >= (e); --i) #define SIZE(c) (int) (c).size() #define FORE(i,c) FOR(i,0,SIZE(c)-1) #define FORDE(i,c) FORD(i,SIZE(c)-1,0) #define pb push_back #define mp make_pair #define st first #define nd second using namespace std; typedef long long ll; typedef pair <int,int> pii; typedef pair <ll,ll> pll; typedef vector <int> VI; typedef vector <bool> VB; typedef vector <pii> VP; typedef vector <ll> VL; typedef vector <pll> VPL; typedef vector <VI> VVI; typedef vector <VL> VVL; typedef vector <VB> VVB; typedef vector <VP> VVP; const int MOD = 1000000007; const int INF = 1000000001; const ll LINF = 1000000000000000001LL; /*************************************************************************/ VI manacher(int n, const string &s, bool even) { VI res(n); int i = 0, t = 0; while (i < n) { while (s[i+1-t] == s[i+1+t+even]) ++t; res[i] = t; int k = 1; while (k < t && res[i-k] != res[i]-k) { res[i+k] = min(res[i-k], res[i]-k); ++k; } t = max(0,t-k); i += k; } return res; } /*************************************************************************/ int main() { if (MyNodeId() != 0) { return 0; } int n = GetLength(); string s = "#"; FOR(i,0,n-1) { s.pb(GetLetter(i)); } s.pb('$'); ll ans = 0; FOR(even,0,1) { VI result = manacher(n, s, even); for (int r : result) { ans += r; } } cout << ans; return 0; } /*************************************************************************/ |