#include<iostream> #include<algorithm> #include<cstring> using namespace std; const int maxn=110010; char t[maxn],s[maxn*3]; int p[maxn*3],lens; void init() { memset(s,'\0',sizeof(s)); int len=strlen(t); lens=0; s[lens++]='+'; for(int i=0;i<len;i++) s[lens++]='#',s[lens++]=t[i]; s[lens]='#'; } int Manacher() { int Maxlen=1,id=0,mx=0; memset(p,0,sizeof(p)); for ( int i=1; i<lens; i++ ){ p[i]= mx>i ? min( p[2*id-i],mx-i) : 1; while( s[i-p[i]] == s[i+p[i]] ) p[i]++; if( mx < i+p[i] ) mx=i+p[i], id=i; Maxlen=max( Maxlen,p[i]-1); } return Maxlen; } int main() { while(~scanf("%s",t)){ init(); printf("%d\n",Manacher()); } return 0; }
来源:https://www.cnblogs.com/overrate-wsj/p/12275094.html