http://www.cnblogs.com/JKAI/p/6947537.html 题解可看jk大神的 网上其实蛮多的 主要看我代码呐
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int M=250005; int len,id,p[M],ans; char s[M]; int main() { while(scanf("%s",s)!=EOF){ memset(p,0,sizeof(p)); len=strlen(s); ans=1; id=0; for(int i=len;i>=0;i--) s[i*2+2]=s[i],s[i*2+1]='#'; s[0]='&'; len=len*2+1; for(int i=2;i<=len;i++){ if(id+p[id]>i) p[i]=min(p[id*2-i],id+p[id]-i); else p[i]=1; while(s[i-p[i]]==s[i+p[i]]) p[i]++; if(p[i]>ans) ans=p[i]; if(id+p[id]<i+p[i]) id=i; } printf("%d ",ans-1); } return 0; }