POJ 3974 Palindrome

MANACHER模板题。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[1000500],now[2000500];
int l,num[2000500];
void make_table()
{
now[0]='$';
for (int i=1;i<=2*l;i++)
{
if (i%2==1) now[i]='#';
else now[i]=s[i/2-1];
}
now[2*l+1]='#';
l=2*l+1;
}
int found()
{
int mx=0,ans=0,pos=0;
for (int i=1;i<=l;i++)
{
if (i<mx) num[i]=min(num[2*pos-i],mx-i);
else num[i]=1;
while (now[i-num[i]]==now[i+num[i]])
num[i]++;
if (i+num[i]>mx)
{
mx=i+num[i];
pos=i;
}
ans=max(ans,num[i]-1);
}
return ans;
}
void work(int x)
{
l=strlen(s);
make_table();
printf("Case %d: %d ",x,found());
return;
}
int main()
{
int cnt=0;
while (scanf("%s",s)!=EOF)
{
cnt++;
if ((s[0]=='E') && (s[1]=='N') && (s[2]=='D'))
break;
else work(cnt);
}
return 0;
}

原文地址:https://www.cnblogs.com/ziliuziliu/p/5272734.html