【回文自动机】 BZOJ 3676 回文串
题意:定义一个字符串的子串的值为该子串的长度乘以数量,求这个最大值
思路:直接统计即可。
代码:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int MAX_N = 600005; const int SIG = 26 ; struct PTree { int nxt[MAX_N][SIG], fail[MAX_N], cnt[MAX_N], num[MAX_N], len[MAX_N], S[MAX_N]; int last, n, p; int newNode (int l) { memset(nxt[p], 0, sizeof nxt[p]); cnt[p] = num[p] = 0; len[p] = l; return p++; } void init() { p = last = n = 0; newNode(0), newNode(-1); S[n] = -1; fail[0] = 1; } int getFail(int x) { while (S[n - len[x] - 1] != S[n]) x = fail[x]; return x; } void add(int c) { c -= 'a'; S[++n] = c; int cur = getFail(last); if (!nxt[cur][c]) { int now = newNode(len[cur] + 2); fail[now] = nxt[getFail(fail[cur])][c]; nxt[cur][c] = now; num[now] = num[fail[now]] + 1; } last = nxt[cur][c]; ++cnt[last]; } void count() { for (int i = p - 1; i >= 0; --i) cnt[fail[i]] += cnt[i]; } }; PTree T; char s[MAX_N]; int main() { while (1 == scanf("%s", s)) { T.init(); for (int i = 0; s[i]; ++i) T.add(s[i]); T.count(); ll ans = 0; for (int i = 2; i < T.p; ++i) ans = max(ans, (ll)T.cnt[i] * T.len[i]); printf("%lld\n", ans); } return 0; }