标签:最长回文子串 子串 iostream return name hang cstring names cout
题意:求一个字符串
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
char s[200005],a[200005];
int p[200005];
int n;
int change()
{
int i,j,t;
a[0]=‘$‘;
a[1]=‘#‘;
j=2;
for(i=0;i<n;i++)
{
a[j++]=s[i];
a[j++]=‘#‘;
}
a[j]=‘\0‘;
return j;
}
int manacher()
{
int len=change();
int maxlen=-1;
int id;
int mx=0;
for(int i=1;i<len;i++)
{
if(i<mx)
p[i]=min(p[id*2-i],mx-i);
else
p[i]=1;
while(a[i-p[i]]==a[i+p[i]])
p[i]++;
if(mx<p[i]+i)
{
mx=p[i]+i;
id=i;
}
maxlen=max(maxlen,p[i]-1);
}
return maxlen;
}
int main()
{
while(cin>>s)
{
n=strlen(s);
cout<<manacher()<<endl;
}
}
标签:最长回文子串 子串 iostream return name hang cstring names cout
原文地址:https://www.cnblogs.com/huangdao/p/9452206.html