边吃边敲,效率就是高
代码如下:
#include<stdio.h>
#include<string.h>
#define MAX 1000050
char s[MAX],ss[MAX*2];
int p[MAX*2];
int min(int a,int b){return a>b?b:a;}
int main()
{
int i,id,mx,max,n,j;
j=1;
while(scanf("%s",s)!=EOF,s[0]!='E')
{
ss[0]='$';
n=strlen(s);
for(i=0;i<n;i++)
{
ss[2*i+1]='#';
ss[2*(i+1)]=s[i];
}
ss[2*i+1]='#';
ss[2*(i+1)]='\0';
id=1;
max=0;
mx=0;
for(i=1;i<2*n+2;i++)
{
if(mx>i)
p[i]=min(p[2*id-i],mx-i);
else
p[i]=1;
while(ss[i+p[i]]==ss[i-p[i]])
{
p[i]++;
}
if(i+p[i]>mx)
{
mx=i+p[i];
id=i;
}
if(p[i]>max) max=p[i];
}
printf("Case %d: %d\n",j++,max-1);
}
return 0;
}