大神链接:http://blog.csdn.net/dyx404514/article/details/42061017
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<limits.h>
#include<algorithm>
using namespace std;
const int maxn=1000010;
char s[maxn];
char s1[maxn<<2];
int len[maxn<<2];
int n,m,length;
int change(char a[]);
int operat(char b[]);
int main()
{
int cases=0,i,j,maxs;
while(scanf("%s",s),s[0]!='E')
{
length=change(s);
maxs=operat(s1);
printf("Case %d: %d\n",++cases,maxs);
}
}
int change(char a[])
{
int i,j,lens=strlen(a);
s1[0]='@';
for(i=1;i<=lens*2;i+=2)
{
s1[i]='#';
s1[i+1]=a[i/2];
}
s1[lens*2+1]='#';
s1[lens*2+2]='$';
s1[lens*2+3]=0;
return lens*2+1;
}
int operat(char b[])
{
int mx=0,ans=0,po=0;//mx即为当前计算回文串最右边字符的最大值
for(int i=1;i<=length;i++)
{
if(mx>i)
len[i]=min(mx-i,len[2*po-i]);
else
len[i]=1;
while(b[i-len[i]]==b[i+len[i]])
len[i]++;
if(len[i]+i>mx)
{
mx=len[i]+i;
po=i;
}
ans=max(ans,len[i]);
}
return ans-1;
}