HDU 3068 回文串--Manacher

//2009 Multi-University Training Contest 16 - Host by NIT
//最长回文
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 100000 + 10;

char Ma[maxn*2];
int Mp[maxn*2];

void Manacher(char s[],int len)
{
	int l=0;
	Ma[l++]='$';
	Ma[l++]='#';
	for(int i=0;i<len;i++)
	{
		Ma[l++]=s[i];
		Ma[l++]='#';
	}
	Ma[l]=0;
	int mx=0,id=0;
	for(int i=0;i<l;i++)
	{
		Mp[i]=mx>i?min(Mp[2*id-i],mx-i):1;
		while(Ma[i+Mp[i]]==Ma[i-Mp[i]]) Mp[i]++;
		if(i+Mp[i]>mx)
		{
			mx=i+Mp[i];
			id=i;
		}
	}
}

char s[maxn];

int main()
{
	while(~scanf("%s",s))
	{
		int len=strlen(s);
		Manacher(s,len);
		int ans=0;
		for(int i=0;i<2*len+2;i++)
			ans=max(ans,Mp[i]-1);
		printf("%d\n",ans);
	}
	return 0;
}


posted on 2013-12-02 15:15  我的小人生  阅读(155)  评论(0编辑  收藏  举报