天天看点

BZOJ3676: [Apio2014]回文串 (回文树模板)

题意:求串s中每个回文串的长度与出现次数乘积的最大值。

思路:回文树裸题,求最大的cnt*len即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <cstdlib>
#include <set>
#include <map>
#include <vector>
#include <string>
 
using namespace std;
 
typedef long long ll;
const ll linf = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int maxn = 300005;
const int mod = 51123987;
const int N = 26; // 字符集大小
 
struct Palindromic_Tree {
    int nxt[maxn][N];//nxt指针,nxt指针和字典树类似,指向的串为当前串两端加上同一个字符构成
    int fail[maxn];//fail指针,失配后跳转到fail指针指向的节点
    int cnt[maxn];//cnt[i]表示i代表的本质不同的串的个数 //结点i代表的回文串在原串中出现的次数
    int num[maxn];//以节点i表示的最长回文串的最右端点为回文串结尾的回文串个数
    int len[maxn];//len[i]表示节点i表示的回文串的长度
    int S[maxn];//存放添加的字符
    int last;//指向上一个字符所在的节点,方便下一次add
    int n;//字符数组指针
    int p;//节点指针/节点数
    int newnode(int l) {//新建节点
        for(int i = 0; i < N; ++i) nxt[p][i] = 0;
        cnt[p] = 0;
        num[p] = 0;
        len[p] = l;
        return p++;
    }
    void init() {//初始化
        p = 0;
        newnode(0);
        newnode(-1);
        last = 0;
        n = 0;
        S[0] = -1;//开头放一个字符集中没有的字符,减少特判
        fail[0] = 1;
    }
    int get_fail(int x) {//和KMP一样,失配后找一个尽量最长的
        while (S[n - len[x] - 1] != S[n]) x = fail[x];
        return x;
    }
    int add(int c) {
        //c -= 'a';
        S[++n] = c;
        int cur = get_fail(last);//通过上一个回文串找这个回文串的匹配位置
        if (!nxt[cur][c]) {//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
            int now = newnode(len[cur] + 2);//新建节点
            fail[now] = nxt[get_fail(fail[cur])][c];//和AC自动机一样建立fail指针,以便失配后跳转
            nxt[cur][c] = now;
            num[now] = num[fail[now]] + 1;
        }
        last = nxt[cur][c];
        cnt[last]++;
        return last; //以添加的字符为后缀构成的最大回文串所在的节点
    }
    void cont() {
        for (int i = p - 1; i >= 0; --i) cnt[fail[i]] += cnt[i];
        //父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
    }
}pt;
 
char s[maxn];
int main() {
    scanf("%s", s);
    int len = strlen(s);
    pt.init();
    for (int i = 0; i < len; ++i) {
        pt.add(s[i] - 'a');
    }
    pt.cont();
    ll ans = 1;
    for (int i = 2; i < pt.p; ++i) {
        ans = max(ans, (ll)pt.cnt[i] * pt.len[i]);
    }
    printf("%lld\n", ans);
    return 0;
}