附上博客http://blog.csdn.net/muxidreamtohit/article/details/8865210
#include <iostream> #include <cstring> #include <cstdio> #include <cmath> using namespace std; int p[220010]; int n; char a[220010]; char c[110010]; void build_p() { int mx=0; int id=0; for(int i=0;i<n;i++) { if(i<mx) { p[i]=min(p[2*id-i],mx-i); } else { p[i]=1; } for(;a[i-p[i]]==a[i+p[i]]&&i-p[i]>=0&&i+p[i]<=n;p[i]++) { } if(p[i]+i>mx) { id=i; mx=p[i]+i; } } } int main() { while(scanf("%s",&c)!=EOF) { int m=strlen(c); n=m*2+1; for(int i=0;i<n;i++) { if(i%2) a[i]=c[(i-1)/2]; else a[i]='#'; } build_p(); int ma=0; for(int i=0;i<n;i++) { if(p[i]>ma) ma=p[i]; } cout<<(ma-1)<<endl; } return 0; }