后缀自动机(suffix automation)

字符串abcbc的后缀自动机:

endpos: 表示结束位置的集合。

a b c b c

1 2 3 4 5

b: [2, 4], b: [3, 5] == bc: [3, 5],所以b和bc是等价类

后缀自动机所有结点的数量 ≤ 2 * n - 1。

 

 

 

 abcbc等价类:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 画aababa的等价类图

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

复制代码
void insert(char c)
{
    int p = last, np;
    np = last = ++ tot;
    maxL[np] = maxL[p] + 1;
    for( ; p && !go[p][c]; p = fa[p]) go[p][c] = np;
    if(!p) fa[np] = 1;
    else
    {
        int q = go[p][c];
        if(maxL[p] + 1 == maxL[q]) fa[nq] = q;
        else
        {
            int nq = ++ tot;
            maxL[nq] = maxL[p] + 1;
            for( ; p && go[p][c] == q; p = fa[p]) go[p][c] = nq;
            for(int i = 0; i < 26; i ++) go[nq][i] = go[q][i];
            fa[nq] = fa[q];
            fa[np] = fa[q] = nq;
        }
    }
}

复制代码
void extend(int c)
{
    int p = last, np = last = ++ tot;
    f[tot] = 1;
    node[np].len = node[p].len + 1;
    for (; p && !node[p].ch[c]; p = node[p].fa) node[p].ch[c] = np;
    if (!p) node[np].fa = 1;
    else
    {
        int q = node[p].ch[c];
        if (node[q].len == node[p].len + 1) node[np].fa = q;
        else
        {
            int nq = ++ tot;
            node[nq] = node[q], node[nq].len = node[p].len + 1;
            node[q].fa = node[np].fa = nq;
            for (; p && node[p].ch[c] == q; p = node[p].fa) node[p].ch[c] = nq;
        }
    }
}
复制代码
复制代码
posted @   龙雪可可  阅读(130)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2020-04-17 欧拉函数
2020-04-17 求约数
2020-04-17 筛质数
****************************************** 页脚Html代码 ******************************************
点击右上角即可分享
微信分享提示