求最长回文字符串:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define MAX_NUM 210010
char str[MAX_NUM];
char str1[MAX_NUM];
int next[MAX_NUM];
int min(int a,int b);
int main()
{
while(scanf("%s",str1)!=EOF)
{
int i=0,len=0,max=0,id=0;
str[0]='$';
str[1]='#';
len=2;
while(str1[i])
{
str[len++]=str1[i++];
str[len++]='#';
}
str[len]=0;
memset(next,0,sizeof(next));
for(i=1;i<len;i++)
{
next[i]=max>i? min(next[2*id-i],max-i):1;
while(str[i+next[i]]==str[i-next[i]]) next[i]++;
if(i+next[i]>max)
{
max=next[i]+i;
id=i;
}
}
max=-1;
for(i=1;i<len;i++)
if(max<next[i])
max=next[i];
printf("%d
",max-1);
}
}
int min(int a,int b)
{
return a>b? b:a;
}