POJ 3974 Palindrome | 马拉车模板

给一个字符串,求最长回文字串有多长


#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 1000005
using namespace std;
int n,m,T,p[N*2],ans;
char s[2*N],t[N];
void manacher()
{
    int id=0,pos=0,x=0;
    for (int i=1;i<+n;i++)
    {
	if (pos>i) x=min(p[id*2-i],pos-i);
	else x=1;
	while (s[i-x]==s[i+x]) x++;
	if (i+x>pos) pos=i+x,id=i;
	p[i]=x;
    }
}
int main()
{
    while (scanf("%s",t+1) && t[1]!='E')
    {
	ans=0;
	m=strlen(t+1);
	s[n=0]='!';
	for (int i=1;i<=m;i++)
	{
	    s[++n]='#';
	    s[++n]=t[i];
	}
	s[++n]='#';
	s[n+1]='?';
	manacher();
	for (int i=1;i<=n;i++)
	    ans=max(ans,p[i]);
	printf("Case %d: %d
",++T,ans-1);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/mrsheep/p/7976877.html