3676: [Apio2014]回文串
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2953 Solved: 1325
[ Submit][ Status][ Discuss]
Description
考虑一个只包含小写拉丁字母的字符串s。我们定义s的一个子串t的“出
现值”为t在s中的出现次数乘以t的长度。请你求出s的所有回文子串中的最
大出现值。
Input
输入只有一行,为一个只包含小写字母(a -z)的非空字符串s。
Output
输出一个整数,为逝查回文子串的最大出现值。
Sample Input
abacaba
【样例输入2]
www
Sample Output
7
【样例输出2]
4
HINT
一个串是回文的,当且仅当它从左到右读和从右到左读完全一样。
在第一个样例中,回文子串有7个:a,b,c,aba,aca,bacab,abacaba,其中:
● a出现4次,其出现值为4:1:1=4
● b出现2次,其出现值为2:1:1=2
● c出现1次,其出现值为l:1:l=l
● aba出现2次,其出现值为2:1:3=6
● aca出现1次,其出现值为1=1:3=3
●bacab出现1次,其出现值为1:1:5=5
● abacaba出现1次,其出现值为1:1:7=7
故最大回文子串出现值为7。
【数据规模与评分】
数据满足1≤字符串长度≤300000。
Source
Hfu居然在复习计划上写回文自动机?? hh我就学过后缀自动机而且现在都快忘光了... 赶紧趁上午的大好时光颓了一发回文自动机... 话说看网上的十分钟像看连环画一样看完了但是上面只是直白的讲了构造? 可能是我还不不懂自动机的概念... 不过自己模拟了一下大概明白了回文自动机工作的原理, 还是非常easy的.
裸的回文自动机.... 逆拓扑序统计一下cnt即可. 裸题十分好写, 一发AC.
Upd(2018.1.17):
看完了2017国家候选队论文中翁文涛的《回文树及其应用》, 收益颇多... 大概是会了前后端插入删除的做法但是好像并没有题目拿来做? 于是先把以前的旧题重做...
这种做法是不基于势能分析的做法, 就是新增一个quick数组~ 具体做法和证明详见论文这里就不细讲了(或者加我qq大家一起来讨论哇), 由于还没有见到过这种版本的写法于是强行yy了一波, 搞出了一个板子来, 幸好没在板子上花太长时间~ 代码放在普通版的下方~
#include<stdio.h>
typedef long long lnt;
const int maxn = 3e5 + 5;
lnt ans;
char s[maxn];
int n, last, tot;
int c[maxn][26], len[maxn], fail[maxn], cnt[maxn];
inline lnt max(const lnt &x, const lnt &y) {
return (x > y) ? x : y;
}
inline int newnode(const int &x) {
len[++ tot] = x;
return tot;
}
inline void Palindromic_init() {
s[0] = tot = -1;
newnode(0), newnode(-1);
fail[0] = 1, n = 1;
}
inline int getfail(int x) {
while (s[n - len[x] - 1] != s[n]) x = fail[x];
return x;
}
int main() {
scanf("%s", s + 1);
Palindromic_init();
int ch, cur, now;
while (s[n]) {
ch = s[n] - 'a';
cur = getfail(last);
if (!c[cur][ch]) {
now = newnode(len[cur] + 2);
fail[now] = c[getfail(fail[cur])][ch];
c[cur][ch] = now;
}
cnt[last = c[cur][ch]] ++, ++ n;
}
for (n = tot; n > 1; -- n) {
cnt[fail[n]] += cnt[n];
ans = max(ans, 1ll * len[n] * cnt[n]);
}
printf("%lld\n", ans);
}
给出Quick版本~
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
char ss[maxn];
struct Palindromic_Tree {
long long ans;
int n, tot, last;
int s[maxn], len[maxn], fail[maxn], cnt[maxn], c[maxn][26], quick[maxn][26];
inline void init() {
tot = 0, last = 1;
newnode(0), newnode(-1);
fail[0] = 1, s[0] = -1;
for (int i = 0; i < 26; ++ i) quick[0][i] = 1;
}
inline int newnode(int p) {
len[tot] = p;
return tot ++;
}
inline int getfail(int p, int ch) {
if (s[n - len[p] - 1] == ch) return p;
else return quick[p][ch];
}
inline void add(int ch) {
s[++ n] = ch;
int cur, nw;
cur = getfail(last, ch);
if (!c[cur][ch]) {
int nw = newnode(len[cur] + 2);
fail[nw] = c[getfail(fail[cur], ch)][ch];
memcpy(quick[nw], quick[fail[nw]], sizeof(quick[fail[nw]]));
quick[nw][s[n - len[fail[nw]]]] = fail[nw];
c[cur][ch] = nw;
}
cnt[last = c[cur][ch]] ++;
}
inline void solve() {
for (int i = tot - 1; i > 1; -- i)
cnt[fail[i]] += cnt[i], ans = max(ans, 1ll * cnt[i] * len[i]);
printf("%lld\n", ans);
}
}Ptr;
int main() {
Ptr.init();
scanf("%s", ss);
for (int i = 0; ss[i]; ++ i) Ptr.add(ss[i] - 'a');
Ptr.solve();
return 0;
}