Portal: Palindrome
Test instructions: Given a string, the longest palindrome substring is obtained.
Analysis: Manach naked topic, core understanding Mx>i?p[i]=min (p[2*id-i],mx-i): 1.
#pragmaComment (linker, "/stack:1024000000,1024000000")#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<limits.h>#include<iostream>#include<algorithm>#include<queue>#include<cstdlib>#include<stack>#include<vector>#include<Set>#include<map>#defineLL Long Long#defineMoD 1000000007#defineINF 0x3f3f3f3f#defineEPS 1e-6#defineN 1000010#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1#definePII pair<int,int>using namespacestd;inline LL Read () {CharCh=getchar (); LL x=0, f=1; while(ch>'9'|| ch<'0'){if(ch=='-') f=-1; ch=GetChar ();} while(ch<='9'&&ch>='0') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}intp[n<<1],ans,len,num,mx,id;Chars[n],str[n<<1];voidbuild () {Len=strlen (s); num=0; Str[num++]='@'; str[num++]='#'; for(intI=0; i<len;i++) {Str[num++]=S[i]; Str[num++]='#'; } Str[num]=0;}voidManacher () {ans=0; mx=0; Memset (P,0,sizeof(p)); for(intI=1; i<num;i++) { if(mx>i) P[i]=min (p[2*id-i],mx-i); Elsep[i]=1; while(Str[i-p[i]]==str[i+p[i]]) p[i]++; if(P[I]+I>MX) mx=p[i]+i,id=i; if(ans<p[i]-1) ans=p[i]-1; }}intMain () {intcas=1; while(SCANF ("%s", s) >0) { if(strcmp (s),"END")==0) Break; Build (); Manacher (); printf ("Case %d:%d\n", cas++, ans); }}View Code
poj3974 (Manacher)