这个可以借鉴这篇博客http://blog.csdn.net/u013508213/article/details/29597201
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
#define maxn 1100000
char st[maxn],play[maxn<<1];
int p[maxn<<1],len,cas;
void solve()
{
cas++;
int len=strlen(st);
memset(play,'#',sizeof(play));
for(int i=0;i<len;i++)
{
play[(i+1)<<1]=st[i];
}
play[0]='?';
len=(len+1)<<1;
play[len]='*';
int id=0,maxl=0,maxid=0;
for(int i=0;i<len;i++)
{
if(i<maxid)
{
p[i]=min(p[2*id-i],maxid-i);
}
else
{
p[i]=1;
}
while(play[i-p[i]]==play[i+p[i]])
{
p[i]++;
}
if(p[i]-1>maxl)
{
maxl=p[i]-1;
}
if(p[i]+i>maxid)
{
maxid=i+p[i];
id=i;
}
}
printf("Case %d: %d\n",cas,maxl);
}
int main()
{
while(1)
{
scanf("%s",st);
if (strcmp(st,"END")==0) break;
solve();
}
return 0;
}