• 【学习笔记】 字符串基础 : 后缀自动机(基础篇)


    本文只介绍关于 SAM" role="presentation">SAM 的基本概念与实现

    后缀自动机是什么

    类似 AC" role="presentation">AC 自动机,后缀自动机(SAM" role="presentation">SAM) 是能且只能接收字符串所有后缀的自动机

    我们首先要知道,SAM" role="presentation">SAM 是只能接收所有后缀的结构而不是只能维护后缀的结构

    事实上 SAM" role="presentation">SAM 的主要作用是维护字符串 S" role="presentation">S每一个子串(因为一个 S" role="presentation">S 的子串一定是一个 S" role="presentation">S 的后缀的前缀)

    在下文中我们将定义 " role="presentation"> 为字符集, ||" role="presentation">||为字符集大小

    后缀自动机的定义

    SAM" role="presentation">SAM 是一张 DAG" role="presentation">DAG,节点被称作状态,边被称作转移

    图中存在一个源点 t0" role="presentation">t0 称作初始节点,其他所有节点均可从 t0" role="presentation">t0 到达

    每个边都标有一些字符,从一个节点出发的所有边都不同(也就是说没有相同的字母作为边)

    存在至少一个终止的状态,从 t0" role="presentation">t0 出发最后转移到一个终止状态,那么路径上所有边链接起来一定是字符串 s" role="presentation">s 的一个后缀,每个字符串 s" role="presentation">s 的后缀都可以用一个 t0" role="presentation">t0 到某个终止状态的路径来构成

    下文中我们定义以下内容

    • endpos" role="presentation">endpos

      代表一个串的结束位置

      image

      这样我们可以把子串根据 endpos" role="presentation">endpos 分为若干种的等价类

      我们可以得到几个重要引理(钦定 |u||w|" role="presentation">|u||w|

      • 当字符串 u" role="presentation">us" role="presentation">s 中的每次出现,都是以 w" role="presentation">w 后缀的形式存在时,字符串 s" role="presentation">s 的两个非空子串 u" role="presentation">uw" role="presentation">wendpos" role="presentation">endpos 相同

        证明:

        • u" role="presentation">uw" role="presentation">wendpos" role="presentation">endpos 相同,则 u" role="presentation">uw" role="presentation">w 的一个只以 s" role="presentation">s 中的一个 w" role="presentation">w 的后缀的形式出现后缀

        • u" role="presentation">uw" role="presentation">w 的一个后缀且在 s" role="presentation">s 中出现时只以后缀的形式,两个子串的 endpos" role="presentation">endpos 相同。

        因此得证

      • u" role="presentation">uw" role="presentation">wendpos" role="presentation">endpos 集合要么不交,要么完全包含

        两者关系为 {endpos(w)endpos(u)u 是 w 的后缀endpos(w)endpos(u)=" role="presentation">{endpos(w)endpos(u)u 是 w 的后缀endpos(w)endpos(u)=

        证明:

        • u" role="presentation">uw" role="presentation">w 的后缀时,类似上图内的 “能够接收 ab" role="presentation">ab 的串的节点一定可以接受 b" role="presentation">b

        • 若集合 endpos(u)" role="presentation">endpos(u)endpos(w)" role="presentation">endpos(w) 有至少一个公共元素,那么由于字符串 u" role="presentation">uw" role="presentation">w 在相同位置结束,u" role="presentation">uw" role="presentation">w 的一个后缀。

        • 所以在每次 w" role="presentation">w 出现的位置,子串 u" role="presentation">u 也会出现。所以 endpos(w)endpos(u)" role="presentation">endpos(w)endpos(u)

        因此得证

      • 将一个 endpos" role="presentation">endpos 等价类中的所有子串按长度非递增的顺序进行排序

        每个子串长度都小于等于它前一个子串且每个子串是它前一个子串的后缀

        因此对于同一等价类的任一两子串,较短者为较长者的后缀且类中的子串长度恰好覆盖整个区间 [x,y]" role="presentation">[x,y]

    • len" role="presentation">len

      代表一个节点能控制的最大长度,比如当一个节点可以控制 a" role="presentation">aab" role="presentation">ab 时其控制的最大长度( len" role="presentation">len )也就是 ab" role="presentation">ab 代表的 2" role="presentation">2

    • fa" role="presentation">fa

      一个节点其连接的 fa" role="presentation">fa 节点是其的一个后缀,因为节点是按照 endpos" role="presentation">endpos 来分的,所以其的 fa" role="presentation">fa 是其后缀内 endpos" role="presentation">endpos 集合不同的最长的后缀

    后缀自动机的构建过程

    假设要建立的串是 S=aabab" role="presentation">S=aabab ,我们先建立一条链

    image

    然后加入一些边来维护后缀自动机,让后缀自动机能够记录所有的后缀

    image

    再比如说要建立的串是 S=kevenhe" role="presentation">S=kevenhe,那么建立出的后缀自动机形态为

    image

    我们发现,在第一个建立完毕的 SAM" role="presentation">SAM 内存在多个 ab" role="presentation">ab 串,而如果我们查询 ab" role="presentation">ab 串就只能查询到第一个出现的 ab" role="presentation">ab 串(也就是先向右再向下)

    因此我们需要引入一个名为 parent" role="presentation">parent 树的数据结构来统计本质相同的子串

    parent" role="presentation">parent 树的建立

    后缀链接 link" role="presentation">link

    对于 parent" role="presentation">parent 树,我们需要一个名为 link" role="presentation">link (后缀链接)的东西来维护

    定义

    考虑 SAM" role="presentation">SAM 中某个不是 t0" role="presentation">t0 的状态 v" role="presentation">vv" role="presentation">v 对应于具有相同 endpos" role="presentation">endpos 的等价类。

    定义 w" role="presentation">w 为这些字符串中最长的一个,则所有其它的字符串都是 w" role="presentation">w 的后缀。

    同时,字符串 w" role="presentation">w 的前几个后缀(按长度降序考虑)全部包含于这个等价类,且所有其它后缀在其它的等价类中。

    记最长的这样的后缀为 t" role="presentation">t,将 v" role="presentation">v 的后缀链接连到 t" role="presentation">t

    一个 后缀链接 link(v)" role="presentation">link(v) 连接到对应于 w" role="presentation">w 的最长后缀的另一个 endpos" role="presentation">endpos 等价类的状态

    通过 endpos" role="presentation">endpos 建立的树和通过 link" role="presentation">link 建立的树形态是相同的
    image

    如上图

    其中这些链接这些节点的边其实和后缀链接 link" role="presentation">link 本质相同

    我们可以发现最后建立的一定是一颗树的形状,其中根节点一定是 t0" role="presentation">t0

    这棵树就是我们的 parent" role="presentation">parent 树了

    定理

    • 设一个 endpos" role="presentation">endpos 等价类为 E" role="presentation">EE" role="presentation">E 中最短的串为 s" role="presentation">s

      那么 endpos(v)endpos(link(v))" role="presentation">endpos(v)endpos(link(v)) (这里因为如果相等会合并为一个 endpos" role="presentation">endpos 所以不能取等)

    • link(E)" role="presentation">link(E) 中最长的串是 s" role="presentation">s 的长度为 |s|1" role="presentation">|s|1 的真后缀。

    SAM" role="presentation">SAM 的建造

    我们把 parent" role="presentation">parent 树和最开始建出的 DAG" role="presentation">DAG 放到一起就形成了 SAM" role="presentation">SAM

    下面的是以 abcbc" role="presentation">abcbc 为例建出的 SAM" role="presentation">SAM

    image

    parent" role="presentation">parent 树与 DAG" role="presentation">DAG 公用结点,但二者的边都是独有的

    构造算法

    下面的是 SAM" role="presentation">SAM 的算法流程

    初始化:len(t0)=0" role="presentation">len(t0)=0link(t0)=1" role="presentation">link(t0)=1

    1. last" role="presentation">last 为添加字符 ch" role="presentation">ch 之前整个字符串 S" role="presentation">S 所对应的结点(从 t0" role="presentation">t0 出发走到 last" role="presentation">last 的路径上边组成的字符串就是 S" role="presentation">S ,初始 last=0" role="presentation">last=0 )。

    2. 创建一个新的状态 cur" role="presentation">cur ,让 len(cur)=len(last)+1" role="presentation">len(cur)=len(last)+1

    3. last" role="presentation">last 开始对 link" role="presentation">link 进行遍历,若当前结点 v" role="presentation">v 没有 vcur" role="presentation">vcur 的边,创建一条 vcur" role="presentation">vcur 的边并标记为 ch" role="presentation">ch

    4. 若遍历到了 t0" role="presentation">t0 则让 link(cur)=0" role="presentation">link(cur)=0 , last=cur" role="presentation">last=cur ,回到第一步

    5. 如果当前结点 v" role="presentation">v 已经有了标记字符 ch" role="presentation">ch 的出边则停止遍历,并把这个结点标记为 p" role="presentation">p ,标记 p" role="presentation">p 沿着标记字符 ch" role="presentation">ch 的出边到达的点为 q" role="presentation">q

    6. 如果 len(p)+1=len(q)" role="presentation">len(p)+1=len(q) ,让 link(cur)=qlast=cur" role="presentation">link(cur)=qlast=cur ,回到第一步。

    7. 复制状态 q" role="presentation">q 到一个新的状态 E" role="presentation">E 里(包括 link(q)" role="presentation">link(q) 以及所有 q" role="presentation">qDAG" role="presentation">DAG 上的出边),将 len(E)" role="presentation">len(E) 赋值为 len(p)+1" role="presentation">len(p)+1

      复制之后,再赋值 link(q)=E,link(cur)=E" role="presentation">link(q)=E,link(cur)=E

      然后从 p" role="presentation">p 遍历后缀链接,设当前遍历到的点为 v" role="presentation">v ,若 v" role="presentation">v 有标记为 ch" role="presentation">ch 的出边 vq" role="presentation">vq ,则重定向这条边为 vE" role="presentation">vE

      v" role="presentation">v 没有标记为 c" role="presentation">c 的出边或者 v" role="presentation">v 的标记为 c" role="presentation">c 的出边所到达的点不是 q" role="presentation">q ,停止遍历,last=cur" role="presentation">last=cur ,回到第一步

    我们一步一步的来看这个算法

    • 找到 last" role="presentation">last 来为后面的更新做准备

    • 加入 ch" role="presentation">ch 后,我们会形成一个新的等价类(因为 endpos" role="presentation">endpos 包含了新字符 ch" role="presentation">ch 的位置),我们定义这个新的等价类为 cur" role="presentation">cur

      根据对 last" role="presentation">last 的定义,我们可以发现 last" role="presentation">last 内最长的字符串一定是 S" role="presentation">S,所以 |S|=len(last)" role="presentation">|S|=len(last)

      我们知道,新加入的字符是 ch" role="presentation">ch ,所以新串是 S+ch" role="presentation">S+ch,我们需要链接一条从 S" role="presentation">Scur" role="presentation">cur 的边,标记为 ch" role="presentation">ch 表示可以从 S" role="presentation">S 转移到 S+ch" role="presentation">S+ch

      那么等价类 cur" role="presentation">cur 内的最长字符串一定是 S+ch" role="presentation">S+ch,长度显然为 len(last)+1" role="presentation">len(last)+1

    • 遍历 link" role="presentation">link 并尝试加入关于 cur" role="presentation">cur 的转移,我们知道 SAM" role="presentation">SAM 是记录的所有后缀,我们只要在后面加入一个 ch" role="presentation">ch

      我们知道,从 last" role="presentation">last 开始跳 link" role="presentation">link 可以跳到所有后缀,只要依次在这些等价类内向 cur" role="presentation">cur 试图加入标记为 ch" role="presentation">ch 的转移即可

      然而我们只能添加与原有转移不冲突的转移。因此我们只要找到已存在的 ch" role="presentation">ch 的转移,我们就必须停止

      下面的图片是对于字符串 S=abcba" role="presentation">S=abcba 加入一个新的字符 d" role="presentation">d 后的结果

      image

      我们发现当从 last" role="presentation">last 一直跳 link" role="presentation">link 跳到了 t0" role="presentation">t0 节点,那么证明 SAM" role="presentation">SAM 内没有出现过 ch" role="presentation">ch 节点

    • 我们到达了虚拟状态 1" role="presentation">1 ,这意味着我们为所有 S" role="presentation">S 的后缀添加了 ch" role="presentation">ch 的转移。这也意味着,字符 ch" role="presentation">ch 从未在字符串 S" role="presentation">S 中出现过。

      因此 cur" role="presentation">cur 的后缀链接为状态 0" role="presentation">0

    • 第二种情况,我们找到了 (p,q)" role="presentation">(p,q) 的状态转移,说明我们正在试图添加一个已经存在的 x+ch" role="presentation">x+ch (x" role="presentation">x为当前这个节点代表的 S" role="presentation">S 的后缀)

      我们似乎不应该在这里加入一个标记为 ch" role="presentation">ch 的转移,所以我们应该把 link(cur)" role="presentation">link(cur) 连到一个 len" role="presentation">lenlen(p)+1" role="presentation">len(p)+1 的状态上

    • 如果 len(q)=len(p)+1" role="presentation">len(q)=len(p)+1,也就是说转移 (p,q)" role="presentation">(p,q) 是连续的

      我们只需要让 link(cur)" role="presentation">link(cur) 连到 q" role="presentation">q 即可

    • 如果 len(q)>len(p)+1" role="presentation">len(q)>len(p)+1,也就是转移 (p,q)" role="presentation">(p,q) 并非连续

      那么这意味着状态 q" role="presentation">q 不只对应于长度为 len(p)+1" role="presentation">len(p)+1 的后缀 S+ch" role="presentation">S+ch ,还对应于 s" role="presentation">s 的更长的子串

      我们就被迫只能把 q" role="presentation">q 一分为二了,让第一个状态 E" role="presentation">Elen" role="presentation">lenlen(p)+1" role="presentation">len(p)+1

      首先我们对于 q" role="presentation">q 复制一个状态 E" role="presentation">E,让 len(E)=len(p)+1" role="presentation">len(E)=len(p)+1,然后把 q" role="presentation">q 的所有转移复制到 E" role="presentation">E

      同时,我们把 link(E)" role="presentation">link(E) 的目标也设为 link(q)" role="presentation">link(q) 的目标,并且让 link(q)" role="presentation">link(q) 的目标设定为 E" role="presentation">E

      接下来我们重定向一些边(因为分开之后我们必须要区分清哪些入边构成了被分出去的 E" role="presentation">E,哪些构成了还剩下在 q" role="presentation">q 中的串),然后继续遍历 link" role="presentation">link 并且分类讨论

      • 找到了一个结点 v" role="presentation">v 拥有一条标记 ch" role="presentation">ch 的出边指向 q" role="presentation">q

        说明 v" role="presentation">v 中的串加上 ch" role="presentation">ch 构成了 E" role="presentation">E 中的串,我们要把这条边重定向到 E" role="presentation">E

      • 找到了一个结点 v" role="presentation">v 拥有一条标记 ch" role="presentation">ch 的出边,但指向结点不是 q" role="presentation">q

        说明 v" role="presentation">v 中的串加上 ch" role="presentation">ch 构成了一个不在 E" role="presentation">E 集合中的 E" role="presentation">E 内最长串的后缀(E" role="presentation">Eparent" role="presentation">parent 树上的某一级祖先中的一个串)。

        因为 v" role="presentation">v 中的串加上 c" role="presentation">c 无法构成 E" role="presentation">E 中的串,则 v" role="presentation">v 的后缀加上 ch" role="presentation">ch 显然也无法构成 E" role="presentation">E 中的一个串。

        也就是说,我们已经把所有要重定向的边完成重定向了,停止遍历即可。

    关于 SAM" role="presentation">SAM 线性复杂度的证明

    SAM" role="presentation">SAM 的复杂度是线性的必要前提是 " role="presentation"> 为常数

    在使用 map 的情况下时间复杂度为 O(nlog||)" role="presentation">O(nlog||),空间复杂度为 O(n)" role="presentation">O(n)

    在不使用 map 的情况下时间复杂度 O(n)" role="presentation">O(n) , 空间复杂度为 O(n||)" role="presentation">O(n||)

    警惕 SAM 题目常见卡 unordered_map 的坏人 QAQ" role="presentation">QAQ

    我们认为字符集的大小为常数,即每次对一个字符搜索转移、添加转移、查找下一个转移的时间复杂度都为 O(1)" role="presentation">O(1)

    在算法中有三处不是明显线性的,我们一一来分析

    • 遍历所有状态 last" role="presentation">last 的后缀链接,添加字符 ch" role="presentation">ch 的转移。

      对于后缀自动机大小,很明显转移数和状态数是线性的,所以这里一定是线性的

    • 当状态 q" role="presentation">q 被复制到一个新的状态 E" role="presentation">E 时复制转移的过程

      同上一条,后缀自动机的转移数和状态数很明显是线性的,所以是线性的

    • 第三处是修改指向 q" role="presentation">q 的转移,将它们重定向到 E" role="presentation">E 的过程。

      结论: longest(link(link(last))" role="presentation">longest(link(link(last)) 的位置单调递增,所以这个循环最多不会执行超过 n" role="presentation">n 次迭代

      证明:

      • 将最初指向 q" role="presentation">q 的转移重定向到 E" role="presentation">E,记 v=longest(p)" role="presentation">v=longest(p) ,这是一个字符串 s" role="presentation">s 的后缀,每次迭代长度都递减(字符串 s" role="presentation">s 的位置每次迭代都单调上升)。

      • 这种情况下,如果在循环的第一次迭代之前,相对应的字符串 v" role="presentation">v 在距离 last" role="presentation">last 的深度为 k(k2)" role="presentation">k(k2) 的位置上(深度记为后缀链接的数量)

      • 那么在最后一次迭代后,字符串 v+ch" role="presentation">v+ch 将会成为路径上第二个从 cur" role="presentation">cur 出发的后缀链接(它会成为新的 last" role="presentation">last 的值)

      • 所以 longest(link(link(last))" role="presentation">longest(link(link(last)) 的位置单调递增

    实现

    下面是基于 map 实现的 SAM,复杂度为 O(|S|log||)" role="presentation">O(|S|log||)

    SAM 跑的不应为字符串即为数字时改变 valueType 即可

    点击查看代码
    #include
    typedef char valueType;
    
    namespace Suffix_AutoMaton{
    #define for_(a,b,ch) for(int a=b;a<=ch;a++)
    #define _for(a,b,ch) for(int a=b;a>=ch;a--)
    
        constexpr int MaxLen=1e5+5;
    
        class SAM{
         public:
            int len, Link;
            std::map<valueType,int> Next;
        }sam[MaxLen<<1];
    
        int Size,Last;
    
        inline void Init() {
            sam[0].len=0;
            sam[0].Link=-1;
            Size++;Last=0;
        }
    
        inline void Insert(valueType ch) {
            int cur=Size++,p=Last;
            sam[cur].len=sam[Last].len+1;
            while((p!=-1)&&(
                !sam[p].Next.count(ch))){
                sam[p].Next[ch]=cur;
                p=sam[p].Link;
            }
            if(p==-1) 
                sam[cur].Link=0;
            else {
                int q=sam[p].Next[ch];
                if(sam[p].len+1==sam[q].len)
                    sam[cur].Link=q;
                else{
                    int E=Size++;
                    sam[E].len=sam[p].len+1;
                    sam[E].Next=sam[q].Next;
                    sam[E].Link=sam[q].Link;
                    while(p!=-1&&sam[p].Next[ch]==q){
                        sam[p].Next[ch]=E;
                        p=sam[p].Link;
                    }
                    sam[q].Link=sam[cur].Link=E;
                }
            }
        }
    
    #undef for_
    #undef _for
    }using namespace Suffix_AutoMaton;
    
    signed main(){
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout.tie(nullptr);
    
        
    }
    
    

    性质

    • 对于字符串 S" role="presentation">S ,它的 SAM" role="presentation">SAM 中的状态数 不会超过 2|S|1" role="presentation">2|S|1

      证明:

      • 算法本身就可以证明,首先一开始我们会自带状态,然后第一轮和第二轮最多只会创建 1" role="presentation">1 个状态

        后面的每轮最多只会创建 2" role="presentation">2 个状态,所以不会超过 2|S|1" role="presentation">2|S|1

    • 对于字符串 S" role="presentation">S ,它的 SAM" role="presentation">SAM 中的转移数 不会超过 3|S|4" role="presentation">3|S|4

    • 分配结束标记

      我们知道,SAM" role="presentation">SAM 是一个能接收所有后缀的最小的 DFA" role="presentation">DFA,因此我们要分配结束标记

      先构建完整颗 SAM" role="presentation">SAM,这样找到构建完毕后的 last" role="presentation">last,然后从 last" role="presentation">last 往根节点跳,路上的所有节点全部打一个结束标记就行了

    应用

    详见【学习笔记】 - 字符串基础 : 后缀自动机(进阶篇)

    例题

    详见【学习笔记】 - 字符串基础 : 后缀自动机(进阶篇)

  • 相关阅读:
    opencv计算图像梯度:Sobel、Scharr、Laplacian详解
    [构造]Build Permutation Codeforces1713C
    【MySQL从入门到精通】【高级篇】(十一)Hash索引、AVL树、B树与B+树对比
    【JavaScript】使用Canvas组件自动生成海报图片
    Python实现自动化网页操作
    IDEA 创建 Servelet 项目
    一文了解 Java 中的构造器
    【备忘录】修复docker环境下的nacos安全漏洞:身份认证绕过漏洞
    1995-2020年全国各省二氧化碳排放量面板数据
    Nvidia GPU 入门教程之 07 TensorFlow MNiST GPU A100(教程含源码)
  • 原文地址:https://www.cnblogs.com/Vsinger-LuoTianYi/p/18136248