【bzoj3676】[Apio2014]回文串

来源:互联网 发布:沪金td数据从哪能下载 编辑:程序博客网 时间:2024/06/09 17:45

*题目描述:
考虑一个只包含小写拉丁字母的字符串s。我们定义s的一个子串t的“出现值”为t在s中的出现次数乘以t的长度。请你求出s的所有回文子串中的最大出现值。
*输入:
输入只有一行,为一个只包含小写字母(a -z)的非空字符串s。
*输出:
输出一个整数,为逝查回文子串的最大出现值。
*样例输入:
【样例输入l】
abacaba
【样例输入2】
www
*样例输出:
【样例输出l】
7
【样例输出2】
4
*提示:
一个串是回文的,当且仅当它从左到右读和从右到左读完全一样。
在第一个样例中,回文子串有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。
*题解:
回文自动机。把回文自动机构出来然后cmax一下每个节点的len*cnt就好了。
推荐一篇好的回文自动机的blog。
*代码:

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#ifdef WIN32    #define LL "%I64d"#else    #define LL "%lld"#endif#ifdef CT    #define debug(...) printf(__VA_ARGS__)    #define setfile() #else    #define debug(...)    #define filename ""    #define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);#endif#define R register#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)#define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))#define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)char B[1 << 15], *S = B, *T = B;inline int FastIn(){    R char ch; R int cnt = 0; R bool minus = 0;    while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;    ch == '-' ? minus = 1 : cnt = ch - '0';    while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';    return minus ? -cnt : cnt;}#define maxn 300010char str[maxn];int next[maxn][26], fail[maxn], len[maxn], cnt[maxn], last, tot, n;inline int new_node(R int l){    len[++tot] = l;    return tot;}inline void init(){    tot = -1;    new_node(0);    new_node(-1);    str[0] = -1;    fail[0] = 1;}inline int get_fail(R int x){    while (str[n - len[x] - 1] != str[n]) x = fail[x];    return x;}inline void extend(R int c){    ++n;    R int cur = get_fail(last);    if (!next[cur][c])    {        R int now = new_node(len[cur] + 2);        fail[now] = next[get_fail(fail[cur])][c];        next[cur][c] = now;    }    last = next[cur][c];    ++cnt[last];}long long ans;inline void count(){    for (R int i = tot; i; --i)    {        cnt[fail[i]] += cnt[i];        cmax(ans, 1ll * len[i] * cnt[i]);    }}int main(){//  setfile();    scanf("%s", str + 1);    init();    for (R int i = 1; str[i]; ++i)        extend(str[i] - 'a');    count();    printf("%lld\n", ans );    return 0;}
0 0
原创粉丝点击