Time Limit:6 Sec Memory limit:128 MB
submit:153 solved:11
Submitstatusweb Board Description
Clam Wei to the right girl sent a necklace, this necklace is composed of lowercase letters of the end-to-end string, sister looked at the necklace to the Clam Wei said, "I hope it is symmetrical", the clam Wei thought after the decision, from the necklace to intercept a paragraph, If this is a palindrome, then the sister wears is symmetrical. Because of the magic of the clam, he can turn one of the letters on the necklace into any other letter, but since the magic limit he can only change two times, now Clam wants to know what the longest length of the necklace he can intercept. For simplicity, We assume that the length of the clam's intercept must be an odd number.
Input
the first line of integer T (1<=t<=10), which represents the number of data groups.
Each set of data is a string s, which represents the necklace, |s|<=100000.
Output
each set of data outputs a number, the longest intercept length.
Sample Input1abcdaaaSample Output7HINT
The sample string changes one letter into ABCBAAA, and the whole necklace can be turned into a palindrome Aabcbaa.
Thought: (DZs taught me). Because it is circular, then the S into SS, similar to the length of the longest palindrome with the hash of I as the center, for each position I, first two points to pos1, then pos1-i-(i-pos1+i) for the current palindrome segment, pos1-=2, equivalent to modify an operation, Continue two points to a position pos2. Do this two times, which is equivalent to two modification operations
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>#include<map>#include<string>using namespacestd;Const intx =123;Const intN =200005; unsignedLong LongH1[n], h2[n], xp[n];CharS[n];intN, M;voidInithash () {H1[n]= H2[n] =0; intt =0; for(inti = n-1; I >=0; --i) {H2[i]= H2[i +1] * x +S[i]; H1[i]= H1[i +1] * x + s[t++]; } xp[0] =1; for(inti =1; I <= N; ++i) Xp[i] = xp[i-1] *x;} unsignedLong LongGethash (intIintLintf) {unsignedLong Longh; if(f = =1) H= H1[i]-h1[i + L] *Xp[l]; Elseh= H2[i]-h2[i + L] *Xp[l]; returnh;}voidinit () {scanf ("%s", s); M=strlen (s); for(inti =0; I < m; ++i) S[i + m] =S[i]; N= M <<1; Inithash ();}int Get(inti) {intL =0, R = i +1; while(R-l >1) { intM = (L + R) >>1; if(N-i + M <= n && i +1+ M <= n && gethash (n-i, M,1) = = Gethash (i +1M2)) L=M; ElseR =M; } returnL;}intChangeintIintCEN) { intL =0, R = i +2; while(R-l >1) { intM = (L + R) >>1; if(N-i-1+ M <= N &&2* cen-i + M <= n && gethash (N-i-1M1) = = Gethash (2* Cen-i, M,2)) L=M; ElseR =M; } returnL;}intsolve () {intpos1, Pos2, Pos3, LS1, LS2; if(M <=5)returnm; intAns =5; for(inti =3; I < n; ++i) {intx =Get(i); POS1= i-x; if(x +2+ i < n) pos1-=2; LS1=Change (POS1, i); Pos2= Pos1-ls1 +1; if(Pos2 = =1&& I-pos2 + i +1< n) Pos3 =0; Else if(Pos2 = =0) Pos3 =Pos2; Else{POS3=Pos2; if(I-pos2 + i +2<N) {pos2-=2; LS2=Change (Pos2, i); POS3= Pos2-ls2 +1; }} ans= Max (ans, (I-POS3) *2+1); } returnmin (m, ans);}intMain () {//freopen ("in", "R", stdin); int_; scanf"%d", &_); while(_ --) {init (); intAns =solve (); if(Ans%2==0) ans--; printf ("%d\n", ans); } return 0;}/************************************************************** problem:1876 user:atrp language:c++ Resul t:accepted time:2676 Ms memory:6208 kb****************************************************************/
View Code
Zzuli 1876: Clam's necklace Hash + dichotomy