Manacher

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int N=22000001;
char s[N];
int len,p[N],id=0,maxp=0,ans=0;
int main()
{
scanf("%s",s+1);
len=strlen(s+1);
for(int i=len;i>=1;--i)
s[i<<1]=s[i],s[i<<1|1]='#';//插入#
s[1]='#';s[0]='$';
for(int i=1;i<=2*len;++i)
{
if(maxp>i)p[i]=min(maxp-i,p[id*2-i]);
else p[i]=1;
while(s[i+p[i]]==s[i-p[i]])++p[i];
if(maxp<p[i]+i)maxp=p[i]+i,id=i;
ans=max(ans,p[i]-1);
}
printf("%d\n",ans);
return 0;
}
显示 Gitment 评论