P3805 【模板】manacher
题目描述
给出一个只由小写英文字符 组成的字符串 ,求 中最长回文串的长度 。
字符串长度为 。
输入格式
一行小写英文字符 组成的字符串 。
输出格式
一个整数表示答案。
输入输出样例 #1
输入 #1
aaa输出 #1
3说明/提示
。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 11000010;
int n, p[2 * N + 2];
char s[N + 2], t[2 * N + 3];
void manacher() {
n = strlen(s + 1);
int m = 0;
t[++m] = '$';
for (int i = 1; i <= n; i++)
t[++m] = s[i], t[++m] = '$';
int M = 0, R = 0;
for (int i = 1; i <= m; i++) {
if (i > R)
p[i] = 1;
else
p[i] = min(p[2 * M - i], R - i + 1);
while (i - p[i] > 0 && i + p[i] <= m && t[i - p[i]] == t[i + p[i]])
++p[i];
if (i + p[i] - 1 > R)
M = i, R = i + p[i] - 1;
}
int ans = 0;
for (int i = 1; i <= m; i++)
ans = max(ans, p[i]);
printf("%d\n", ans - 1);
}
int main() {
scanf("%s", s + 1);
manacher();
}