前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >后缀自动机模版

后缀自动机模版

作者头像
用户2965768
发布2019-08-01 10:58:57
3060
发布2019-08-01 10:58:57
举报
文章被收录于专栏:wymwym
代码语言:javascript
复制
struct Suffix_Automaton
{
    int nex[maxn << 1][26], a[maxn << 1], link[maxn << 1];
    int tot, last, root;
    int p, q, np, nq;
    int newnode(int len)
    {
        for (int i = 0; i < 26; ++i) nex[tot][i] = -1;
        link[tot] = -1; a[tot] = len; 
        return tot++;
    }
    void clear()
    {
        tot = last = 0; 
        root = newnode(0);
    }
    void insert(int ch)
    {
        p = last; last = np = newnode(a[p] + 1); 
        for (; ~p && nex[p][ch] == -1; p = link[p]) nex[p][ch] = np;
        if (p == -1) link[np] = root;
        else
        {
            q = nex[p][ch];
            if (a[p] + 1 == a[q]) link[np] = q;
            else
            {
                nq = newnode(a[p] + 1);
                for (int i = 0; i < 26; ++i) nex[nq][i] = nex[q][i];
                link[nq] = link[q];
                link[np] = link[q] = nq;
                for (; ~p && nex[p][ch] == q; p = link[p]) nex[p][ch] = nq;
            }
        }
    }
    long long getSubNum()
    {
        long long ans = 0;
        for (int i = 1; i < tot; ++i) ans += a[i] - a[link[i]];
        return ans;
    }
}sam;
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年07月27日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档