题解 P4287 【[SHOI2011]双倍回文】
提供一个可能比较好想的 manacher:
我们枚举双倍回文串右边部分的中心点时,需要找到这个中心点对应的双倍回文串的中心点。根据双倍回文串的性质,这两个点都应该是特殊符号而不是字母(满足长度为 4 的倍数),并且这两个点应该满足
为了动态插入、查找符合条件的 lower_bound() 函数就可以了。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
const int N = 1e6 + 20;
int n, ans;
char s[N];
int p[N];
set<pair<int, int> > s1;
set<int> s2;
void manacher(char *s) {
for (int i = 1, r = 0, id = 0; i < n; i++) {
if (i < r) p[i] = min(p[id * 2 - i], r - i);
else p[i] = 1;
while (s[i + p[i]] == s[i - p[i]]) p[i]++;
if (i + p[i] > r) r = i + p[i], id = i;
if (s[i] == '$') {
s1.insert(make_pair(i + (p[i] - 1) / 2, i));
s2.insert(i);
while (s1.begin()->first < i) s2.erase(s1.begin()->second), s1.erase(s1.begin());
int t = *s2.lower_bound(i - p[i]);
ans = max(ans, (i - t) * 2);
}
}
}
int main() {
s[0] = '#';
scanf("%d", &n);
scanf("%s", s + 1);
for (int i = n * 2 + 1; i >= 1; i--)
if (i & 1) s[i] = '$';
else s[i] = s[i >> 1];
s[n * 2 + 2] = '\0';
n = strlen(s);
manacher(s);
printf("%d", ans);
return 0;
}