Manacher
Tags:字符串
作业部落
评论地址
极易理解,挂一个dalao的博客
贴个代码吧。
//luogu P3805【模板】manacher算法
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
int l,C,R,p[31000000],Ans;
char s[31000000];
int main()
{
s[++l]='#';char ch=getchar();
while(ch>'z'||ch<'a') ch=getchar();
while(ch>='a'&&ch<='z')
{
s[++l]=ch;s[++l]='#';
ch=getchar();
}
for(int i=1;i<=l;i++)
{
p[i]=i<R?min(p[C*2-i],R-i):1;
while(s[i+p[i]]==s[i-p[i]]&&i-p[i]>=1&&i+p[i]<=l) p[i]++;
if(i+p[i]>R) {R=i+p[i];C=i;}
Ans=max(Ans,p[i]-1);
}
printf("%d\n",Ans); return 0;
}
挂个题单
- [x] [luogu3805]【模板】manacher算法 https://www.luogu.org/problemnew/show/P3805
- [x] [BZOJ3790]神奇项链 http://172.40.25.127:5283/problem/113790
- [x] [SHOI2011]双倍回文 https://www.luogu.org/problemnew/show/P4287
- [x] [ZJOI2009]对称的正方形 https://www.luogu.org/problemnew/show/P2601
- [x] [luogu1659][国家集训队]拉拉队排练 https://www.luogu.org/problemnew/show/P1659
- [ ] [luogu4555]最长双回文串 https://www.luogu.org/problemnew/show/P4555
- [x] [BZOJ3160]万径人踪灭 https://www.luogu.org/problemnew/show/P4199