【模板】后缀自动机

后缀自动机是一个能够接受母串所有后缀的最简自动机。
其中,每个节点代表一个字符串集合,该集合中所有的字符串均有相同的 \(right\) 集合。
各个节点之间根据 \(right\) 集合的关系可以组织成一棵树形结构,称为 \(parent\) 树。
后缀自动机最多有 \(2*n - 1\) 个节点。

代码如下

#include <bits/stdc++.h>

using namespace std;

struct SAM {
    struct state {
        int len, p, cnt;
        map<char, int> go;
        state(int _len = 0, int _p = -1, int _cnt = 0) {
            len = _len;
            p = _p;
            cnt = _cnt;
        }
    };
    int last;
    vector<state> t;
    SAM() {
        last = 0;
        t.push_back({});
    }
    inline state& operator[](int x) {
        return t[x];
    }
    inline int size() {
        return t.size();
    }
    inline void extend(char c) {
        int cur = t.size();
        t.push_back({t[last].len + 1, -1, 1});
        int x = last;
        while (x != -1 && t[x].go.count(c) == 0) {
            t[x].go[c] = cur;
            x = t[x].p;
        }
        if (x == -1) {
            t[cur].p = 0;
        } else {
            int y = t[x].go[c];
            if (t[y].len == t[x].len + 1) {
                t[cur].p = y;
            } else {
                int z = t.size();
                t.push_back(t[y]);
                t[z].len = t[x].len + 1;
                t[z].cnt = 0;
                while (x != -1 && t[x].go[c] == y) {
                    t[x].go[c] = z;
                    x = t[x].p;
                }
                t[cur].p = t[y].p = z;
            }
        }
        last = cur;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int n;
    string s;
    cin >> s;
    n = s.size();
    SAM sam;
    for (int i = 0; i < n; i++) {
        sam.extend(s[i]);
    }
    vector<vector<int>> adj(sam.size());
    for (int i = sam.size() - 1; i >= 1; i--) {
        adj[sam[i].p].push_back(i);
    }
    long long ans = 0;
    function<void(int)> dfs = [&](int u) {
        for (auto v : adj[u]) {
            dfs(v);
            sam[u].cnt += sam[v].cnt;
        }
        if (sam[u].cnt > 1) {
            ans = max(ans, (long long)sam[u].cnt * sam[u].len);
        }
    };
    dfs(0);
    cout << ans << endl;
    return 0;
} 

转载于:https://www.cnblogs.com/wzj-xhjbk/p/11586525.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值