链接:
https://www.luogu.org/problem/P3805
题意:
给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.
字符串长度为n
思路:
马拉车算法.
代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e7+10;
int hw[MAXN];
char a[MAXN], s[MAXN<<1];
void Manacher(char *ori)
{
int maxr = 0, mid;
int len = strlen(ori);
for (int i = 1;i < len;i++)
{
if (i < maxr)
hw[i] = min(hw[mid*2-i], maxr-i);
else
hw[i] = 1;
while (ori[i+hw[i]] == ori[i-hw[i]])
++hw[i];
if (hw[i]+i > maxr)
{
mid = i;
maxr = hw[i]+i;
}
}
}
void Change(char *ori, char *pst)
{
pst[0] = pst[1] = '#';
int len = strlen(ori);
for (int i = 0;i < len;i++)
pst[i*2+2] = ori[i], pst[i*2+3] = '#';
pst[len*2+2] = 0;
}
int main()
{
scanf("%s", a);
Change(a, s);
Manacher(s);
int ans = 1;
for (int i = 0;i < strlen(s);i++)
ans = max(ans, hw[i]);
printf("%d
", ans-1);
return 0;
}