标签:define 字符 int htm print while void ase turn
https://subetter.com/algorithm/manacher-algorithm.html
#include<bits/stdc++.h>
#define N 2050001
using namespace std;
int len,p[N],Case,ans;
char ch[N],s[N*2];
void manacher()
{
int cnt=1;s[0]=‘%‘;s[1]=‘#‘;
for(int i=0;i<len;i++)
{
s[++cnt]=ch[i];
s[++cnt]=‘#‘;
}
//在原串的基础上,在字符最开始加一个上"$",然后让每个字符的前后都是"#"
//例如abc----->$#a#b#c#,于是len2的值为2*len1+2
int maxRight=0,id=0;
for(int i=1;i<=cnt;i++)
{
if(i<maxRight)
p[i]=min(p[2*id-i],maxRight-i);
//p[2*id-i]是i关于id对称的另一个点,这个点的P值已算出来过
//maxright-i为i到maxright的距离
//取其两者较小值
else
p[i]=1;
while (i+p[i]<=cnt&&s[i-p[i]]==s[i+p[i]])
p[i]++;
if(i+p[i]>maxRight)
id=i,maxRight=i+p[i];
}
for(int i=1;i<=cnt;i++)
ans=max(ans,p[i]);
ans--;
}
int main(){
while(1){
scanf("%s",ch);len=strlen(ch);
if(len==3&&ch[0]==‘E‘&&ch[1]==‘N‘&&ch[2]==‘D‘)break;
memset(p,0,sizeof(p));
ans=1;manacher();
printf("Case %d: %d\n",++Case,ans);
}
return 0;
}
标签:define 字符 int htm print while void ase turn
原文地址:https://www.cnblogs.com/cutemush/p/12369518.html