Manacher's Algorithm 马拉车算法

作用:求一个字符串中的最长子串,同时还可以求所有子串的长度。

题目链接:

https://vjudge.net/contest/254692#problem/B

代码:

#include<bits/stdc++.h>
using namespace std;
# define maxn 20000+10
char str1[maxn];
char str2[maxn*2];
int p[maxn*2];
int l=0;
int ma()
{
int id=0,mx=0;
int ans=0;
str2[l]='\0';
for(int i=0; i<l+2; i++)p[i]=0;
for(int i=1; i<l; i++)
{
if(i<mx)p[i]=min(p[2*id-i],mx-i);
else p[i]=1;
while(str2[i-p[i]]==str2[i+p[i]])
p[i]++;
if(mx<i+p[i])
{
id=i;
mx=i+p[i];
}
ans+=p[i]/2;
}
return ans;
}
int main()
{
int n;
scanf("%d",&n);
scanf("%s",str1);
str2[l++]='$';
str2[l++]='#';
for(int i=0; i<n; i++)
{
if(str1[i]=='-')
{
if(l-2!=0)l-=2;
}
else
{
str2[l++]=str1[i];
str2[l++]='#';
}
printf("%d",ma());
if(i!=n-1)printf(" ");
}
printf("\n");
return 0;
}
上一篇:element-ui 2.7.2版本使用 表格展开行 功能遇到的奇葩问题?


下一篇:移动web:翻页场景动画