博主链接
求一个串中最长回文串的长度
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
char s[111111];
char s_new[111111*2];
int p[111111*2];
int Init()
{
int len = strlen(s);
s_new[0] = '$';
s_new[1] = '#';
int j = 2;
for (int i = 0; i < len; i++)
{
s_new[j++] = s[i];
s_new[j++] = '#';
}
s_new[j] = '\0'; //别忘了哦
//printf("%s\n",s_new);
return j; //返回s_new的长度
}
int Manacher()
{
int len = Init(); //取得新字符串长度并完成向s_new的转换
int maxLen = -1; //最长回文长度
int id;
int mx = 0;
for (int i = 1; i < len; i++)
{
if (i < mx)
p[i] = min(p[2 * id - i], mx - i); //需搞清楚上面那张图含义, mx和2*id-i的含义
else
p[i] = 1;
while (s_new[i - p[i]] == s_new[i + p[i]]) //不需边界判断,因为左有'$',右有'\0'
p[i]++;
//我们每走一步i,都要和mx比较,我们希望mx尽可能的远,这样才能更有机会执行if (i < mx)这句代码,从而提高效率
if (mx < i + p[i])
{
id = i;
mx = i + p[i];
}
maxLen = max(maxLen, p[i] - 1);
// printf("%d %d %d\n",mx,id,maxLen);
}
/*for(int i=1;i<=len;i++)printf("%d ",p[i]);
printf("\n");*/
return maxLen;
}
int main()
{
while (~scanf("%s", s))
{
printf("%d\n", Manacher());
}
return 0;
}