#include <bits/stdc++.h> #include "message.h" #include "palindromy.h" using namespace std; typedef long long ll; const int ILE = 51001000; int N; char T[ILE]; int R[ILE]; int main() { if(MyNodeId() != 0) return 0; //scanf("%d%s", &N, T); N = GetLength(); for(int i = 0; i < N; ++i) T[i] = GetLetter(i); R[0] = 0; //Oblicz promienie palindromow parzystych int i = 1; int t = 0; R[0] = 0; ll sum = 0; while(i <= N) { while(T[i - t] == T[i + t + 1] && i + t + 1 < N && i - t >= 0) ++t; //oblicz wielkosc palindromu parzystego o srodku w punkcie i R[i] = t; //Algorytm Manachera int k = 1; while(k <= t && R[i - k] != R[i] - k) { R[i + k] = min(R[i-k],(R[i]-k)); k++; } t -= k; t = max(0 , t); i += k; } for(int i = 0; i < N; ++i) { sum += R[i]; R[i] = 0; } //Oblicz promienie palindromow nieparzystych i = 1; t = 0; R[0] = 0; while(i <= N) { while(i + t + 1 < N && i - t - 1 >= 0 && T[i - t - 1] == T[i + t + 1]) ++t; //oblicz wielkosc palindromu nieparzystego o srodku w punkcie i R[i] = t; //Algorytm Manachera int k = 1; while(k <= t && R[i - k] != R[i] - k) { R[i + k] = min(R[i-k],(R[i]-k)); k++; } t -= k; t = max(0 , t); i += k; } for(int i = 0; i < N; ++i) sum += R[i]; printf("%lld\n", sum + N); //for(int i = 0; i < ILE; ++i) //printf("R[%d] = %d\n", i, R[i]); }
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 | #include <bits/stdc++.h> #include "message.h" #include "palindromy.h" using namespace std; typedef long long ll; const int ILE = 51001000; int N; char T[ILE]; int R[ILE]; int main() { if(MyNodeId() != 0) return 0; //scanf("%d%s", &N, T); N = GetLength(); for(int i = 0; i < N; ++i) T[i] = GetLetter(i); R[0] = 0; //Oblicz promienie palindromow parzystych int i = 1; int t = 0; R[0] = 0; ll sum = 0; while(i <= N) { while(T[i - t] == T[i + t + 1] && i + t + 1 < N && i - t >= 0) ++t; //oblicz wielkosc palindromu parzystego o srodku w punkcie i R[i] = t; //Algorytm Manachera int k = 1; while(k <= t && R[i - k] != R[i] - k) { R[i + k] = min(R[i-k],(R[i]-k)); k++; } t -= k; t = max(0 , t); i += k; } for(int i = 0; i < N; ++i) { sum += R[i]; R[i] = 0; } //Oblicz promienie palindromow nieparzystych i = 1; t = 0; R[0] = 0; while(i <= N) { while(i + t + 1 < N && i - t - 1 >= 0 && T[i - t - 1] == T[i + t + 1]) ++t; //oblicz wielkosc palindromu nieparzystego o srodku w punkcie i R[i] = t; //Algorytm Manachera int k = 1; while(k <= t && R[i - k] != R[i] - k) { R[i + k] = min(R[i-k],(R[i]-k)); k++; } t -= k; t = max(0 , t); i += k; } for(int i = 0; i < N; ++i) sum += R[i]; printf("%lld\n", sum + N); //for(int i = 0; i < ILE; ++i) //printf("R[%d] = %d\n", i, R[i]); } |