@Junlier
2018-08-19T22:25:52.000000Z
字数 1376
阅读 1879
字符串算法——Manacher
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
#include<ctime>
#include<queue>
#include<stack>
#include<vector>
#define rg register
#define il inline
#define lst long long
#define ldb long double
#define N 51000100
using namespace std;
const int Inf=1e9;
int n,ans=1;
int p[N];
char S[N],s[N<<1];
il int read()
{
rg int s=0,m=0;rg char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')m=1;ch=getchar();}
while(ch>='0'&&ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return m?-s:s;
}
il void change()
{
s[0]=s[1]='#';
for(rg int i=0;i<n;++i)
{
s[(i<<1)+2]=S[i];
s[(i<<1)+3]='#';
}
s[n=(n<<1)+2]=0;
}
il void manacher()
{
rg int mx=0,id;
for(rg int i=1;i<n;++i)
{
if(i<mx)p[i]=min(p[(id<<1)-i],mx-i);
else p[i]=1;
while(s[i+p[i]]==s[i-p[i]])p[i]++;
if(p[i]+i>mx)
mx=p[i]+i,id=i;
}
}
int main()
{
cin>>S;
n=strlen(S);
change();
manacher();
for(rg int i=0;i<n;++i)
ans=max(ans,p[i]);
printf("%d\n",ans-1);
return 0;
}