Luogu3805 【模板】manacher算法
https://www.luogu.com.cn/problem/P3805
\(Manacher\)
首先,为了处理奇偶回文串问题,我们可以在字符之间加入\('\#'\),再把第一个字符定为\('\$'\)
\(r\)表示当前到第\(i\)个字符的回文串最右边界
分\(3\)类讨论
具体看参考\(blog\)
\(C++ Code:\)
#include<cstdio>
#include<iostream>
#include<algorithm>
#define N 22000005
using namespace std;
int n,ans,r,mid,q[N];
char c,s[N];
int main()
{
c=getchar();
while (c<'a'||c>'z')
c=getchar();
s[0]='$';
n=1;
s[n]='#';
while ('a'<=c&&c<='z')
{
n++;
s[n]=c;
n++;
s[n]='#';
c=getchar();
}
for (int i=1;i<=n;i++)
{
if (i<=r)
q[i]=min(q[(mid << 1)-i],r-i+1);
while (s[i-q[i]]==s[i+q[i]])
q[i]++;
if (i+q[i]>r)
r=i+q[i]-1,mid=i;
if (q[i]>ans)
ans=q[i];
}
printf("%d\n",ans-1);
return 0;
}