// Author: Arkadiusz Pawlik (based on sol2 by Lech Duraj) #include #include #include #include #include #include #define dprintf(...) //#define dprintf(...) fprintf(stderr,__VA_ARGS__) using namespace std; const int maxn = 100*1000; const int maxp = 3*maxn+1; int pal[maxn]; int who[maxn]; int middle[maxp]; int radius[maxp]; vector link[maxp]; vector endq[maxn]; vector events[maxn]; int shorter[maxp]; int half[maxp]; int dp[maxp],dph[maxp]; string A; int pc; int n; int trim(int node, int x, int s=0) { if (x==0) return node; if ( x%(1<<(s+1)) != 0) { node = link[node][s]; x -= (1<i) { int j = f - (i - f); who[i] = who[j]; pal[i] = pal[j]; int over = i+pal[i]-f-pal[f]; if (over>0) { pal[i] -= over; who[i] = trim(who[i],over); } } else pal[i] = 0; while(i+pal[i]0 && A[i+pal[i]]==A[i-pal[i]-1]) { pal[i]++; f = i; middle[pc] = i; radius[pc] = pal[i]; link[pc].clear(); link[pc].push_back(who[i]); int q = 1; while((1 << q) <= radius[pc]) { link[pc].push_back(link[link[pc][q-1]][q-1]); q++; } who[i] = pc; pc++; } } } int query(int i, set &S, int x) { auto t = S.lower_bound(x+1); if (t!=S.begin()) { t--; int over = i - (*t - pal[*t]); return trim(who[*t], over); } else return 0; } volatile int sink = 0; void blackhole(const string & A) { int n = A.size(); for (int i = 0; i < n; ++i) { int r = 0; int x = 0; while (i-r-1 >= 0 && i+r < n && A[i-r-1] == A[i+r]) { x |= (A[i+r] != A[i]); sink = r ^ 42; ++r; if (r == 5 && !x) { // Bail out if it looks uniform. break; } } } } int main() { int TT; cin >> TT; while(TT--) { cin >> A; blackhole(A); n = A.length(); manacher(); for(int i=0; i0) events[i-pal[i]].push_back(i); set S; for(int i=0; i