回文自动机
时间复杂度T(n):O(log2n);
空间复杂度S(n):O(n);
最近去补了一下回文自动机,感觉还是非常的厉害的,首先一个回文自动机可以维护的东西:
1、求串S的前缀中本质不同的回文串个数
2、求串S的前缀中每一个回文串的出现次数
3、求串S的前缀中的回文串个数
4、求以下标i为结尾的回文串的个数
实现:主要直接见代码注释。
注意:建立的-1的节点为奇数长度的回文串的根,0为偶数长度的回文串的根,暴力建立fail指针的过程中,如果当前字符是单个字符,那么最后一步判断为s[cur_len – len[x] - 1] == s[cur_len],如果len[x] = -1, 就是判断s[cur_len] == s[cur_len]边界很方便,len[x] = 0时,s[cur_len - 1] == s[cur_len]也很方便。
例题:bzoj3676
Source:
/*
created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
inline char read() {
static const int IN_LEN = 1024 * 1024;
static char buf[IN_LEN], *s, *t;
if (s == t) {
t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
if (s == t) return -1;
}
return *s++;
}
///*
template<class T>
inline void R(T &x) {
static char c;
static bool iosig;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return ;
if (c == '-') iosig = true;
}
for (x = 0; isdigit(c); c = read())
x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
*oh++ = c;
}
template<class T>
inline void W(T x) {
static int buf[30], cnt;
if (x == 0) write_char('0');
else {
if (x < 0) write_char('-'), x = -x;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
while (cnt) write_char(buf[cnt--]);
}
}
inline void flush() {
fwrite(obuf, 1, oh - obuf, stdout);
}
/*
template<class T>
inline void R(T &x) {
static char c;
static bool iosig;
for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
if (c == '-') iosig = true;
for (x = 0; isdigit(c); c = getchar())
x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
const int MAXN = 300000 + 10;
const int ALP = 26;
struct PAM {
int next[MAXN][26], fail[MAXN], num[MAXN], cnt[MAXN], len[MAXN], s[MAXN];
int p, n, now, last;
/*
next:下一个字符
fail: 上一个最长的回文后缀所在位置
num:以当前的这个节点所代表的原串位置为结尾的不同的回文串个数
cnt:当前节点代表的回文串的出现次数(需要最后用fail指针跑一次)
len:当前节点代表的回文串的长度
s:当前节点的字符
*/
inline int new_node(int l) {
for (int i = 0; i < ALP; ++i) next[p][i] = 0;
len[p] = l, num[p] = 0, cnt[p] = 0;
return p++;
}
inline void init() {
p = 0, n = 0, last = 0, s[0] = -1;
new_node(0), new_node(-1);
fail[0] = 1, fail[1] = 0;
/*方便起见,-1节点和0节点互为fail,方便奇偶回文串的分类和处理
-1为奇数长度的回文串的根,0位偶数长度的回文串的根*/
}
inline int get_fail(int cur) {
while (s[n - len[cur] - 1] != s[n]) cur = fail[cur];
/*暴力求fail指针,不用解释了吧*/
return cur;
}
inline void add(int c) {
s[++n] = c;
int cur = get_fail(last);
if (next[cur][c] == 0) {
now = new_node(len[cur] + 2);
fail[now] = next[get_fail(fail[cur])][c];
/*找最长的回文后缀(前缀),因为这个回文串已经存在了,所以找到的上
一个位置的回文串一定存在,e.g:abcbabcba, 找到的上一个
位置为第5个字符a, 组成的回文串为abcba,在前缀abcba一定存在*/
num[now] = num[fail[now]] + 1, next[cur][c] = now;
/*fail指针出现的回文串在当前节点一定都出现过*/
}
last = next[cur][c], cnt[last]++;
/*当前节点代表的回文串个数 + 1*/
}
inline void get_ans() {
long long ans = 0;
for (int i = p - 1; i >= 0; --i) {
cnt[fail[i]] += cnt[i];
/*当前回文串出现的位置,fail所对应的标号一定也出现过*/
ans = std::max(ans, (long long)cnt[i] * len[i]);
}
std::cout << ans;
}
} pam;
int len;
char s[MAXN];
int main() {
scanf("%s", s + 1), len = strlen(s + 1), pam.init();
for (int i = 1; i <= len; ++i) pam.add(s[i] - 'a');
pam.get_ans();
return 0;
}