《SAM学习总结》

· · 个人记录

之前没学懂,补一下。

阅前说明:大量抄袭了 command\_block 的博客。(没问过原主人,如果涉及侵权可以让我删除)

原理

首先我们需要的是一个可以识别一个串的所有子串的自动机,这个自动机就是 SAM 。

一些定义:

0. 不知道叫什么

我们的 SAM 建出来是一个 DAG 图,然后从源点都其他点的所有路径的并恰好就是这个字符串的所有子串,很厉害。

然后这个东西可以保证做到点数 O(n) ,边数 O(n) ,建自动机的过程均摊 O(n)

1. endpos

顾名思义,就是结束位置了,对于一个串的每个子串,他都有一个 endpos ,比如 abab 这个串中,b 的 endpos 就是 (2,4) 两个位置,同理 abab 也是 (2,4) 两个位置,我们称两个 endpos 相同的子串为 endpos 等价类。

下面证明一些关于 endpos 的性质。

证明,这个是显然的,如果 s1 为 s2 的后缀的话,那么每个 s2 出现的位置都可以发现有一个 s1 ,所以显然 s1 出现的位置是 s2 的超集。

证明,其实要证的就是如果相交,那么一定是包含关系,我们先假设 |s1|\le |s2| ,倘若 edp(s1),edp(s2) 有交,那么显然一定存在某个位置出现了 s2 同时出现了 s1 ,也就是说 s1 为 s2 的后缀,然后由引理 1 可以得到 edp(s2)\in edp(s1) 。

证明,设最长的串为 s1 ,最短的串为 s2 ,然后根据引理 2 可以知道,一个 epd 等价类中的东西都是后缀关系,所以对于中间每个串 s3 (长度大于 s2 ) ,都有 s2 为 s3 的后缀,所以 edp(s3)\in edp(s2),edp(s1)\in edp(s3) ,又因为 edp(s1)=edp(s2) 所以 edp(s1)=edp(s2)=edp(s3) ,然后长度是连续的,所以自然就是一段连续后缀。

2. parent\ tree

然后要么不交要么包含,这么个条件实际上就是类似于一个树形结构。

我们考虑按等价类来划分节点,记一个节点中长度最长的字符串为 Smax 。

那么只要在这个 Smax 前面增加一个字符,那么新生成的字符串 Snew 一定和原本的字符串不在一个等价类中。

反正都抄了那么多了,图也搬了

对于上面这张图,我们记绿色的这几个节点为终止节点,表示 最后 加入的字符所构成的后缀属于哪些节点,那么我们接下来新加字符就是在这些对应节点中的每个后缀增加一个字符,具体如何实现直接讲代码(这东西不看代码真的懂不了一点)。

\color{red} \text{不过有一个要注意的地方,我们建 SAM ,是通过不断在后缀树上操作从而得到 SAM}
struct SAM {
    int tot=1,lt=1;
    void insert(int x) {
        int u=lt,nw=++tot;
        lt=nw;
        a[nw].len=a[u].len+1;
        while(u&&!a[u].c[x]) {
            a[u].c[x]=nw;
            u=a[u].f;
        }
        if(!u) a[nw].f=1;
        else {
            int t=a[u].c[x];
            if(a[t].len==a[u].len+1) a[nw].f=t;
            else {
                int nt=++tot;
                a[nt]=a[t];
                a[nt].len=a[u].len+1;
                while(u&&a[u].c[x]==t) {
                    a[u].c[x]=nt;
                    u=a[u].f;
                }
                a[nw].f=a[t].f=nt;
            }
        }
    }
}T;

对了 len 表示这个等价类中最长长度的字符串是多长,f 表示当前等价类在 parent\ tree 上的父亲是谁, c[26] 就是和自动机类似,当下一个节点为 x 时,指向哪个节点。

首先是 u=lt,nw=++tot,lt=nw 就是最简单的新建节点,以及更新终止节点。

a[nw].len=a[u].len+1;

这句话就是说我们新建的节点,目前包括了以新增节点为终止节点的若干个后缀,长度为 1\sim a[u].len+1 。

其实 a[u].len+1 就是目前整个串的长度。

然后是下面这段代码:

while(u&&!a[u].c[x]) {
    a[u].c[x]=nw;
    u=a[u].f;
}

我们要往所有终止链上的点往他们的后缀上加一个点,记为 Snew ,如果他们之前没出现过过 Snew 这个字符串的话,那么我们只需要,就令 a[u].c[x]=nw ,相当于把 |Smin_u|+1\sim |Smax_u|+1 (Smin_u 为 u 节点中最短的后缀长度)的后缀分给了这个节点(因为如果走到了这个节点,然后又走了这条边,那么相当于给自己的长度 +1 ,而当前这个节点的后缀长度为 Smin_u\sim Smax_u ,所以就是 |Smin_u|+1\sim |Smax_u|+1),直到 u 为 0 或者存在某一个等价类中加入我们当前需要新加入的字符后,发现之前存在过这样的后缀。

然后我们先考虑最简单的情况。

``` if(!u) a[nw].f=1; ``` $case\ 2:$ 跳到发现某个节点他存在过和当前新增的字母的后缀,如果指向的那个节点,记为 $t$ ,的 $len==a[u].len+1$ 就说明那个节点中没有比,当前 $1\sim a[u].len+1$ 的后缀要长的后缀,所以可以直接令后缀链接 $=t$ ,那么这时候你就完成了 $Smin+1\sim Smax+1$ 的后缀的下放,而对于 $1\sim Smin$ 的后缀不用管,因为那些后缀在 $t$ 的祖先节点都已经被放过了。 ``` if(a[t].len==a[u].len+1) a[nw].f=t; ``` $case\ 3:$ 但是当发现 $t$ 的后缀最大值要大于 $a[u].len+1$ 时,情况就有所不同了。 ![](https://cdn.luogu.com.cn/upload/image_hosting/cszs0kyd.png?x-oss-process=image/resize,m_lfit,h_170,w_225) 假如我们在一个点他最大的 $len$ 为 $5$ ,指向的下一个点的 $len$ 的区间范围为 $5\sim 7$ ,那么我们就要把 $5\sim a[u].len+1=6$ 的后缀拎出来,新开一个节点,然后让原本的那个节点的后缀链接连向新开的节点,那么这样就完成了 $endpos$ 的分裂,顺便的,要把原本节点在 $u$ 的后缀链接上连着原本节点的那些边全部发给新的节点,这个看上去也是比较显然的。 ``` int nt=++tot; a[nt]=a[t]; a[nt].len=a[u].len+1; while(u&&a[u].c[x]==t) { a[u].c[x]=nt; u=a[u].f; } a[nw].f=a[t].f=nt; ``` 知道了上面这些之后,想做到理解性默写就不难了,其实就是一个动态维护 $endpos$ 的一个过程,仅此而已。 # $实战演练 ~~靠,手滑,把我的博客删掉了,生气,得重写一次~~ ## 不同字串个数 [P2408 不同子串个数](https://www.luogu.com.cn/problem/P2408) 两种求法 - 利用 $parent$ 树上的性质,所有节点不交或包含,所以统计 $\sum\limits_{t\in son_u}a[t].len-a[u].len
#include<bits/stdc++.h>
typedef long long LL;

using namespace std;
const int MAXN=2e6+10;
char ch[MAXN];
int n;
struct ar {
    int f,c[26],len;
};
ar a[MAXN];
vector<int> e[MAXN];
int sz[MAXN];
struct SAM {
    int tot=1,lt=1;
    void insert(int x) {
        int u=lt,nw=++tot;
        lt=nw;
        a[nw].len=a[u].len+1;
        while(u&&!a[u].c[x]) {
            a[u].c[x]=nw;
            u=a[u].f;
        }
        if(!u) a[nw].f=1;
        else {
            int t=a[u].c[x];
            if(a[t].len==a[u].len+1) a[nw].f=t;
            else {
                int nt=++tot;
                a[nt]=a[t];
                a[nt].len=a[u].len+1;
                while(u&&a[u].c[x]==t) {
                    a[u].c[x]=nt;
                    u=a[u].f;
                }
                a[nw].f=a[t].f=nt;
            }
        }
    }
}T;
LL ans;
void dfs(int u) {
    for(auto t:e[u]) {
        dfs(t);
        sz[u]+=sz[t];
        ans+=a[t].len-a[u].len;
    }
}
int main () {
    scanf("%d",&n);
    scanf("%s",ch+1);
    for(int i=1;i<=n;++i) T.insert(ch[i]-'a');
    int u=1;
    for(int i=1;i<=n;++i) {
        u=a[u].c[ch[i]-'a'];
        ++sz[u];
    }
    for(int i=2;i<=T.tot;++i) {
        e[a[i].f].push_back(i);
    }
    dfs(1);
    printf("%lld\n",ans);
    return 0;
}

众所周知, SAM 建出来之后是一个 DAG 图,且所有本质不同的子串个数其实就是他到其他节点的所有路径和, DAG 图统计路径即可。

#include<bits/stdc++.h>
typedef long long LL;

using namespace std;
const int MAXN=2e6+10;
char ch[MAXN];
int n;
struct ar {
    int f,c[26],len;
};
ar a[MAXN];
vector<int> e[MAXN];
int sz[MAXN];
struct SAM {
    int tot=1,lt=1;
    void insert(int x) {
        int u=lt,nw=++tot;
        lt=nw;
        a[nw].len=a[u].len+1;
        while(u&&!a[u].c[x]) {
            a[u].c[x]=nw;
            u=a[u].f;
        }
        if(!u) a[nw].f=1;
        else {
            int t=a[u].c[x];
            if(a[t].len==a[u].len+1) a[nw].f=t;
            else {
                int nt=++tot;
                a[nt]=a[t];
                a[nt].len=a[u].len+1;
                while(u&&a[u].c[x]==t) {
                    a[u].c[x]=nt;
                    u=a[u].f;
                }
                a[nw].f=a[t].f=nt;
            }
        }
    }
}T;
LL ans,f[MAXN];
void dfs(int u) {
    if(f[u]) return ;
    for(int i=0;i<26;++i) {
        int t=a[u].c[i];
        if(!t) continue;
        dfs(t);
        f[u]+=f[t];
    }
    ++f[u];
}
int main () {
    scanf("%d",&n);
    scanf("%s",ch+1);
    for(int i=1;i<=n;++i) T.insert(ch[i]-'a');
    int u=1;
    for(int i=1;i<=n;++i) {
        u=a[u].c[ch[i]-'a'];
        ++sz[u];
    }
    for(int i=2;i<=T.tot;++i) {
        e[a[i].f].push_back(i);
    }
    dfs(1);
    printf("%lld\n",f[1]-1);
    return 0;
}

P4070 [SDOI2016] 生成魔咒

动态维护本质不同的子串数量,显然用法 2 不太行,用法 1 ,直接维护 a[u].len-a[a[u].f].len 即可。

#include<bits/stdc++.h>
typedef long long LL;

using namespace std;
const int MAXN=2e5+10;
char ch[MAXN];
int n;
struct ar {
    int f,len;
    map<int,int> c;
};
ar a[MAXN];
vector<int> e[MAXN];
int sz[MAXN];
LL ans;
struct SAM {
    int tot=1,lt=1;
    void insert(int x) {
        int u=lt,nw=++tot;
        lt=nw;
        a[nw].len=a[u].len+1;
        while(u&&!a[u].c[x]) {
            a[u].c[x]=nw;
            u=a[u].f;
        }
        if(!u) a[nw].f=1,ans+=a[nw].len;
        else {
            int t=a[u].c[x];
            if(a[t].len==a[u].len+1) a[nw].f=t,ans+=a[nw].len-a[t].len;
            else {
                int nt=++tot;
                a[nt]=a[t];
                a[nt].len=a[u].len+1;
                ans+=a[nt].len-a[a[nt].f].len;
                while(u&&a[u].c[x]==t) {
                    a[u].c[x]=nt;
                    u=a[u].f;
                }
                ans-=(a[t].len-a[a[t].f].len);
                ans+=(a[nw].len+a[t].len-a[nt].len*2);
                a[nw].f=a[t].f=nt;
            }
        }
    }
}T;
int main () {
    scanf("%d",&n);
    for(int i=1;i<=n;++i) {
        int x;
        scanf("%d",&x);
        T.insert(x);
        printf("%lld\n",ans);
    }
    return 0;
}

子串出现次数

P3804 【模板】后缀自动机(SAM)

一个子串的出现次数其实就是他的 edp 个数,对于每个前缀 i ,我们只需要直接暴力对于他们对应的节点 ++sz ,然后其他节点统计一下子树中的 sz 和即可。

#include<bits/stdc++.h>
typedef long long LL;

using namespace std;
const int MAXN=2e6+10;
char ch[MAXN];
int n;
struct ar {
    int f,c[26],len;
};
ar a[MAXN];
vector<int> e[MAXN];
int sz[MAXN];
struct SAM {
    int tot=1,lt=1;
    void insert(int x) {
        int u=lt,nw=++tot;
        lt=nw;
        a[nw].len=a[u].len+1;
        while(u&&!a[u].c[x]) {
            a[u].c[x]=nw;
            u=a[u].f;
        }
        if(!u) a[nw].f=1;
        else {
            int t=a[u].c[x];
            if(a[t].len==a[u].len+1) a[nw].f=t;
            else {
                int nt=++tot;
                a[nt]=a[t];
                a[nt].len=a[u].len+1;
                while(u&&a[u].c[x]==t) {
                    a[u].c[x]=nt;
                    u=a[u].f;
                }
                a[nw].f=a[t].f=nt;
            }
        }
    }
}T;
LL ans;
void dfs(int u) {
    for(auto t:e[u]) {
        dfs(t);
        sz[u]+=sz[t];
    }
    if(sz[u]>1) {
        ans=max(ans,(LL)sz[u]*a[u].len);
    }
}
int main () {
    scanf("%s",ch+1);
    n=strlen(ch+1);
    for(int i=1;i<=n;++i) T.insert(ch[i]-'a');
    int u=1;
    for(int i=1;i<=n;++i) {
        u=a[u].c[ch[i]-'a'];
        ++sz[u];
    }
    for(int i=2;i<=T.tot;++i) {
        e[a[i].f].push_back(i);
    }
    dfs(1);
    printf("%lld\n",ans);
    return 0;
}

第 k 小串

P3975 [TJOI2015] 弦论

这个东西也不难搞。

对于询问本质不同的,直接在 SAM 的 DAG 图上走路径,然后类似的搞搞就好了。

对于询问可重的,我们求出每个子串的出现次数,然后也是直接走就好了。

#include<bits/stdc++.h>
typedef long long LL;

using namespace std;
const int MAXN=1e6+10;
char ch[MAXN];
int n;
struct ar {
    int f,len;
    int c[26];
};
ar a[MAXN];
vector<int> e[MAXN];
LL f[MAXN],sz[MAXN];
struct SAM {
    int tot=1,lt=1;
    void insert(int x) {
        int u=lt,nw=++tot;
        lt=nw;
        a[nw].len=a[u].len+1;
        while(u&&!a[u].c[x]) {
            a[u].c[x]=nw;
            u=a[u].f;
        }
        if(!u) a[nw].f=1;
        else {
            int t=a[u].c[x];
            if(a[t].len==a[u].len+1) a[nw].f=t;
            else {
                int nt=++tot;
                a[nt]=a[t];
                a[nt].len=a[u].len+1;
                while(u&&a[u].c[x]==t) {
                    a[u].c[x]=nt;
                    u=a[u].f;
                }
                a[nw].f=a[t].f=nt;
            }
        }
    }
}T;
int opt,k;
void dfs(int u) {
    for(auto t:e[u]) {
        dfs(t);
        sz[u]+=sz[t];
    }
}
void DFS(int u) {
    if(f[u]) return ;
    for(int i=0;i<26;++i) {
        int t=a[u].c[i];
        if(!t) continue;
        DFS(t);
        f[u]+=f[t];
    }
    if(!opt) ++f[u];
    else f[u]+=sz[u];
}
vector<char> ans;
void dfs1(int u) {
    if(!opt) --k;
    else k-=sz[u];
    if(k<=0) return ;
    for(int i=0;i<26;++i) {
        int t=a[u].c[i];
        if(!t) continue;
        if(f[t]<k) {
            k-=f[t];
            continue;
        }
        ans.push_back(char('a'+i));
        dfs1(t);
        break;
    }
}
int main () {
    scanf("%s",ch+1);
    n=strlen(ch+1);
    scanf("%d%d",&opt,&k);
    for(int i=1;i<=n;++i) T.insert(ch[i]-'a');
    int u=1;
    for(int i=1;i<=n;++i) {
        u=a[u].c[ch[i]-'a'];
        ++sz[u];
    }
    for(int i=2;i<=T.tot;++i) {
        e[a[i].f].push_back(i);
    }
    dfs(1);
    DFS(1);
    if(!opt) ++k;
    else k+=sz[1];
    if(f[1]<k) {
        puts("-1");
        return 0;
    }
    dfs1(1);
    for(auto t:ans) {
        putchar(t);
    }
    return 0;
}

一类匹配问题

LCS - Longest Common Substring

首先对于一个串建 SAM 然后用另一个串在上面条,如果 !a[u].c[t] ,那么就跳父亲,然后令 len=a[u].len (更新过的),因为贪心肯定是想让他尽可能大,然后就没了。

LCS2 - Longest Common Substring II

选择一个串作为匹配串,将其他串轮流建 SAM ,然后将匹配串丢到所有串的 SAM 上跑一下,对于某一个前缀 i ,他的贡献是在其他所有串的 SAM 上跑完之后,最小的那个值。

#include<bits/stdc++.h>
typedef long long LL;

using namespace std;
const int MAXN=1e6+10;
string st[3];
int n;
struct ar {
    int f,len;
    int c[26];
};
ar a[MAXN];
struct SAM {
    int tot=1,lt=1;
    void insert(int x) {
        int u=lt,nw=++tot;
        lt=nw;
        a[nw].len=a[u].len+1;
        while(u&&!a[u].c[x]) {
            a[u].c[x]=nw;
            u=a[u].f;
        }
        if(!u) a[nw].f=1;
        else {
            int t=a[u].c[x];
            if(a[t].len==a[u].len+1) a[nw].f=t;
            else {
                int nt=++tot;
                a[nt]=a[t];
                a[nt].len=a[u].len+1;
                while(u&&a[u].c[x]==t) {
                    a[u].c[x]=nt;
                    u=a[u].f;
                }
                a[nw].f=a[t].f=nt;
            }
        }
    }
    void clear() {
        memset(a,0,sizeof(a));
        tot=lt=1;
    }
}T;
int Ans[MAXN];
int main () {
    int ans=0;
    memset(Ans,127,sizeof(Ans));
    cin>>st[1];
    st[1]='1'+st[1];
    while(cin>>st[2]) {
        st[2]="1"+st[2];
        T.clear(); 
        n=st[2].size()-1;
        for(int i=1;i<=n;++i) T.insert(st[2][i]-'a');
        int u=1;
        n=st[1].size();
        int ls=0;
        for(int i=1;i<=n;++i) {
            int t=st[1][i]-'a';
            while(u!=1&&!a[u].c[t]) {
                u=a[u].f;
                ls=a[u].len;
            }
            if(a[u].c[t]) {
                ++ls;
                u=a[u].c[t];
            }
            else {
                ls=0;
            }
            Ans[i]=min(Ans[i],ls);
        }
    }
    for(int i=1;i<=n;++i) {
        ans=max(ans,Ans[i]);
    }
    printf("%d\n",ans);
    return 0;
}

Cyclical Quest

比较有意思的一个小应用,显然看到循环同构,可以想到破坏成链然后去匹配,但是这时候可能长度会超过 n ,强制让长度 -1 ,如果 -1 后不在当前节点中了,就跳父亲即可。

然后成功后,贡献就是对应节点的出现次数,不过循环同构可能会出现重复字符串,所以需要打个标记,表示这个字符串贡献过了。

#include<bits/stdc++.h>
typedef long long LL;

using namespace std;
const int MAXN=2e6+10;
char ch[MAXN];
int n;
struct ar {
    int f,len;
    int c[26];
};
ar a[MAXN];
vector<int> e[MAXN];
int sz[MAXN];
struct SAM {
    int tot=1,lt=1;
    void insert(int x) {
        int u=lt,nw=++tot;
        lt=nw;
        a[nw].len=a[u].len+1;
        while(u&&!a[u].c[x]) {
            a[u].c[x]=nw;
            u=a[u].f;
        }
        if(!u) a[nw].f=1;
        else {
            int t=a[u].c[x];
            if(a[t].len==a[u].len+1) a[nw].f=t;
            else {
                int nt=++tot;
                a[nt]=a[t];
                a[nt].len=a[u].len+1;
                while(u&&a[u].c[x]==t) {
                    a[u].c[x]=nt;
                    u=a[u].f;
                }
                a[nw].f=a[t].f=nt;
            }
        }
    }
}T;
void dfs(int u) {
    for(auto t:e[u]) {
        dfs(t);
        sz[u]+=sz[t];
    }
}
bool vis[MAXN];
int bh[MAXN],tot;
int main () {
    scanf("%s",ch+1);
    n=strlen(ch+1);
    for(int i=1;i<=n;++i) {
        T.insert(ch[i]-'a');
    }
    for(int i=2;i<=T.tot;++i) {
        e[a[i].f].push_back(i);
    }
    int u=1;
    for(int i=1;i<=n;++i) {
        u=a[u].c[ch[i]-'a'];
        ++sz[u];
    }
    dfs(1);
    int m;
    scanf("%d",&m);
    for(int i=1;i<=m;++i) {
        scanf("%s",ch+1);
        n=strlen(ch+1);
        for(int j=1;j<n;++j) {
            ch[j+n]=ch[j];
        }
        int u=1,ls=0;
        LL ans=0; tot=0;
        for(int j=1;j<=2*n-1;++j) {
            if(ls==n) {
                --ls;
                if(ls<=a[a[u].f].len) u=a[u].f;
            }
            int t=ch[j]-'a';
            while(u!=1&&!a[u].c[t]) {
                u=a[u].f;
                ls=a[u].len;
            }
            if(a[u].c[t]) {
                ++ls;
                u=a[u].c[t];
            }
            else ls=0;
            if(ls==n&&!vis[u]) {
                ans+=sz[u];
                vis[u]=1;
                bh[++tot]=u;
            }
        }
        for(int j=1;j<=tot;++j) {
            vis[bh[j]]=0;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

P6640 [BJOI2020] 封印

好题。

首先显然对于 t 建出 SAM ,然后对于 s 的每个前缀 i ,令他匹配一下 t ,看一下最多匹配多少,记为 sp_i 。

然后对于某一个询问,相当于问 \max\limits_{i=l}^r \min(sp_i,i-l+1)

遇到这种问题,我们要么考虑钦定选择某一边,要么二分直接确定下界。

这里考虑二分,那么相当于询问 \max\limits_{i=l+mid-1}^r sp_i 是否 \ge mid ,如果是就说明成功了。

这个东西可以用 st 表维护,时间复杂度 O(n\log n)

#include<bits/stdc++.h>
typedef long long LL;

using namespace std;
const int MAXN=2e6+10;
char ch1[MAXN],ch2[MAXN];
int n;
struct ar {
    int f,len;
    int c[26];
};
ar a[MAXN];
struct SAM {
    int tot=1,lt=1;
    void insert(int x) {
        int u=lt,nw=++tot;
        lt=nw;
        a[nw].len=a[u].len+1;
        while(u&&!a[u].c[x]) {
            a[u].c[x]=nw;
            u=a[u].f;
        }
        if(!u) a[nw].f=1;
        else {
            int t=a[u].c[x];
            if(a[t].len==a[u].len+1) a[nw].f=t;
            else {
                int nt=++tot;
                a[nt]=a[t];
                a[nt].len=a[u].len+1;
                while(u&&a[u].c[x]==t) {
                    a[u].c[x]=nt;
                    u=a[u].f;
                }
                a[nw].f=a[t].f=nt;
            }
        }
    }
}T;
int logg[MAXN];
const int N=30;
int st[N][MAXN];
int query(int x,int y) {
    int k=logg[y-x+1];
    return max(st[k][x],st[k][y-(1<<k)+1]);
}
int main () {
    scanf("%s%s",ch1+1,ch2+1);
    n=strlen(ch2+1);
    for(int i=1;i<=n;++i) {
        T.insert(ch2[i]-'a');
    }
    int ls=0,u=1;
    n=strlen(ch1+1);
    for(int i=1;i<=n;++i) {
        int t=ch1[i]-'a';
        while(!a[u].c[t]) {
            u=a[u].f;
            ls=a[u].len;
        }
        if(a[u].c[t]) {
            ++ls;
            u=a[u].c[t];
        }
        else ls=0;
        st[0][i]=ls;
    }
    for(int i=2;i<=n;++i) logg[i]=logg[i/2]+1;
    for(int i=1;i<=logg[n];++i) {
        int R=n-(1<<i)+1;
        for(int j=1;j<=R;++j) {
            st[i][j]=max(st[i-1][j],st[i-1][j+(1<<(i-1))]);
        }
    }
    int q;
    scanf("%d",&q);
    for(int i=1;i<=q;++i) {
        int x,y;
        scanf("%d%d",&x,&y);
        int l=0,r=y-x+2,mid;
        while(l+1<r) {
            mid=(l+r)/2;
            if(query(x+mid-1,y)>=mid) l=mid;
            else r=mid;
        }
        printf("%d\n",l);
    }
    return 0;
}

SAM和后缀树的关系

虽然我不知道他们有什么关系,不过反正是一种简单应用。

首先由一个结论:两个串的 lcs 为他们在 parent 树上 lca 的最长长度,而当题目询问我们 lcp 的时候可以把串倒过来,这样就可以求了。

P4248 [AHOI2013] 差异

要我们求 lcp ,把串倒过来,然后做,可以发现其实就是 parent 树上两两点之间的距离和,我一开始写了一个 dp ,但是不知道为什么不对,然后贺题用了拆贡献方法

\sum\limits_{u=1}^n \sum\limits_{t\in son_u}sz_t\times (n-sz_t)\times (a[t].len-a[u].len)

然后就做完了。

#include<bits/stdc++.h>
typedef long long LL;

using namespace std;
const int MAXN=1e6+10;
char ch[MAXN];
int n;
struct ar {
    int f,len;
    int c[26];
};
ar a[MAXN];
struct SAM {
    int tot=1,lt=1;
    void insert(int x) {
        int u=lt,nw=++tot;
        lt=nw;
        a[nw].len=a[u].len+1;
        while(u&&!a[u].c[x]) {
            a[u].c[x]=nw;
            u=a[u].f;
        }
        if(!u) a[nw].f=1;
        else {
            int t=a[u].c[x];
            if(a[t].len==a[u].len+1) a[nw].f=t;
            else {
                int nt=++tot;
                a[nt]=a[t];
                a[nt].len=a[u].len+1;
                while(u&&a[u].c[x]==t) {
                    a[u].c[x]=nt;
                    u=a[u].f;
                }
                a[nw].f=a[t].f=nt;
            }
        }
    }
}T;
LL ans;
LL sz[MAXN];
vector<int> e[MAXN];
void dfs(int u) {
    for(auto t:e[u]) {
        dfs(t);
        sz[u]+=sz[t];
        ans+=sz[t]*(n-sz[t])*(a[t].len-a[u].len);
    }
}
int main () {
    scanf("%s",ch+1);
    n=strlen(ch+1);
    for(int i=n;i>=1;--i) T.insert(ch[i]-'a');
    for(int i=2;i<=T.tot;++i) {
        e[a[i].f].push_back(i);
    }
    int u=1;
    for(int i=n;i>=1;--i) {
        u=a[u].c[ch[i]-'a'];
        ++sz[u];
    }
    dfs(1);
    printf("%lld\n",ans);
    return 0;
}

广义 SAM

识别一些串的所有子串,每次做完之后将 lt=1 ,然后再做一次,看上去很显然,但是貌似有 trie 树上的正统做法,反正我不学,无所谓。

JZPGYZ - Sevenk Love Oimaster

对于所有串建出 SAM 后还要静态区间数颜色,不过可以很暴力的做。

void update(int u,int x) {
    for(int i=u;i;i=a[i].f) {
        if(vis[i]==x) break;
        ++sz[i];
        vis[i]=x;
    }
}
for(int i=1;i<=n;++i) {
    int u=1;
    for(int j=0;j<len[i];++j) {
        u=a[u].c[ch[i][j]-'a'];
        update(u,i);
    }
}

然后剩下的就是直接匹配了

#include<bits/stdc++.h>
typedef long long LL;

using namespace std;
const int MAXN=2e5+10;
int n,Q;
struct ar {
    int f,c[26],len;
}a[MAXN];
struct SAM {
    int lt=1,tot=1;
    void insert(int x) {
        int u=lt,nw=++tot;
        lt=tot;
        a[nw].len=a[u].len+1;
        while(u&&!a[u].c[x]) {
            a[u].c[x]=nw;
            u=a[u].f;
        }
        if(!u) a[nw].f=1;
        else {
            int t=a[u].c[x];
            if(a[t].len==a[u].len+1) a[nw].f=t;
            else {
                int nt=++tot;
                a[nt]=a[t];
                a[nt].len=a[u].len+1;
                while(u&&a[u].c[x]==t) {
                    a[u].c[x]=nt;
                    u=a[u].f;
                }
                a[nw].f=a[t].f=nt;
            }
        }
    }
}T;
string ch[MAXN];
int len[MAXN],sz[MAXN],vis[MAXN];
void update(int u,int x) {
    for(int i=u;i;i=a[i].f) {
        if(vis[i]==x) break;
        ++sz[i];
        vis[i]=x;
    }
}
int main () {
    scanf("%d%d",&n,&Q);
    for(int i=1;i<=n;++i) {
        T.lt=1;
        cin>>ch[i];
        len[i]=ch[i].size();
        for(int j=0;j<len[i];++j) {
            T.insert(ch[i][j]-'a');
        }
    }
    for(int i=1;i<=n;++i) {
        int u=1;
        for(int j=0;j<len[i];++j) {
            u=a[u].c[ch[i][j]-'a'];
            update(u,i);
        }
    }
    for(int i=1;i<=Q;++i) {
        int u=1;
        string ls;
        cin>>ls;
        int le=ls.size();
        bool sf=0;
        for(int j=0;j<le;++j) {
            if(a[u].c[ls[j]-'a']) u=a[u].c[ls[j]-'a'];
            else sf=1;
        }
        if(!sf) printf("%d\n",sz[u]);
        else puts("0");
    }
    return 0;
}

Match & Catch

板子题没什么好说的。

线段树合并 edp

这个也不难,主要是询问涉及区间的时候可能就要用到这个。

和求子串出现次数是类似的,对于每个前缀 edp 的出现位置给他的节点 edp 加上 i 然后线段树合并就好了。

CF1037H Security

首先我们肯定是尽量匹配上 T ,然后从后往前看一下能不能找到一个可替换位置。

先考虑 L=1,R=n 的情况。

显然就是找到最后一个可替换位置,然后就没什么好说的了。

考虑一般情况,倘若现在匹配了长度为 l 的东西,那么一个位置如果是可替换的,那么那个 t 节点的 edp 中就要存在 \ge L+l-1 的位置,那么这个位置就是可替换的,所以就是维护一个 edp 就好了。

时间复杂度 O(|S|\log n+26|T|\log n)

#include<bits/stdc++.h>
typedef long long LL;

using namespace std;
string st;
const int MAXN=2e5+10;
int n,Q;
struct ar {
    int f,c[26],len,rt,edp;
}a[MAXN];
struct SAM {
    int lt=1,tot=1;
    void insert(int x) {
        int u=lt,nw=++tot;
        lt=tot;
        a[nw].len=a[u].len+1;
        while(u&&!a[u].c[x]) {
            a[u].c[x]=nw;
            u=a[u].f;
        }
        if(!u) a[nw].f=1;
        else {
            int t=a[u].c[x];
            if(a[t].len==a[u].len+1) a[nw].f=t;
            else {
                int nt=++tot;
                a[nt]=a[t];
                a[nt].len=a[u].len+1;
                while(u&&a[u].c[x]==t) {
                    a[u].c[x]=nt;
                    u=a[u].f;
                }
                a[nw].f=a[t].f=nt;
            }
        }
    }
}T;
vector<int> e[MAXN];
int cnt;
struct segment_tree {
    int L[MAXN*30],R[MAXN*30];
    void update(int &u,int l,int r,int x) {
        if(!u) u=++cnt;
        if(l==r) return ;
        int mid=(l+r)/2;
        if(x<=mid) update(L[u],l,mid,x);
        else update(R[u],mid+1,r,x);
    }
    int merge(int x,int y,int l,int r) {
        if(!x||!y) return x^y;
        int nw=++cnt;
        int mid=(l+r)/2;
        L[nw]=merge(L[x],L[y],l,mid);
        R[nw]=merge(R[x],R[y],mid+1,r);
        return nw;
    }
    bool query(int u,int l,int r,int x,int y) {
        if(!u) return false;
        if(l>=x&&r<=y) return true;
        int mid=(l+r)/2;
        int ans=0;
        if(x<=mid) ans|=query(L[u],l,mid,x,y);
        if(y>mid) ans|=query(R[u],mid+1,r,x,y);
        return ans;
    }
}T1;
void dfs(int u) {
    if(a[u].edp) {
        T1.update(a[u].rt,1,n,a[u].edp);
    }
    for(auto t:e[u]) {
        dfs(t);
        a[u].rt=T1.merge(a[u].rt,a[t].rt,1,n);
    }
}
int ind[MAXN];
char ls[MAXN];
int main () {
    cin>>st;
    n=st.size();
    for(int i=0;i<n;++i) {
        T.insert(st[i]-'a');
    }
    int u=1;
    for(int i=0;i<n;++i) {
        u=a[u].c[st[i]-'a'];
        a[u].edp=i+1;
    }
    for(int i=2;i<=T.tot;++i) {
        e[a[i].f].push_back(i);
    }
    dfs(1);
    int Q;
    scanf("%d",&Q);
    while(Q--) {
        int l,r;
        scanf("%d%d%s",&l,&r,ls);
        int ls_len=strlen(ls);
        for(int i=0;i<ls_len;++i) ls[i]-='a';
        int u=1,p=0; ind[0]=1;
        for(int i=0;i<ls_len;++i) {
            u=a[u].c[ls[i]];
            if(u&&T1.query(a[u].rt,1,n,l+i,r)) {
                p=i+1;
                ind[i+1]=u;
            }
            else {
                break;
            }
        }
        ls[ls_len]=-1;
        int ans=11451499,ss=0;
        for(int i=p;i>=0;--i) {
            u=ind[i];
            for(int j=ls[i]+1;j<26;++j) {
                int t=a[u].c[j];
                if(t&&T1.query(a[t].rt,1,n,l+i,r)) {
                    ans=j; ss=i;
                    break;
                }
            }
            if(ans!=11451499) break;
        }
        if(ans==11451499) {
            puts("-1");
            continue;
        }
        for(int i=0;i<ss;++i) putchar(ls[i]+'a');
        putchar(ans+'a');
        putchar('\n'); 
    }
    return 0;
}

P4094 [HEOI2016/TJOI2016] 字符串

首先要求 lcp ,将串倒过来求 lcs 。

然后容易想到的是要二分答案,二分一个长度,然后我们考虑让 [a,b] 去匹配 [c,d] ,那么也就是说一定是在 d 前缀的祖先中的某个节点有 lca ,所以考虑倍增,然后判一下 edp 即可。

时间复杂度 O(n\log^2 n)

#include<bits/stdc++.h>
typedef long long LL;

using namespace std;
string st;
const int MAXN=2e5+10;
int n,Q;
struct ar {
    int f,c[26],len,rt,edp;
}a[MAXN];
struct SAM {
    int lt=1,tot=1;
    void insert(int x) {
        int u=lt,nw=++tot;
        lt=tot;
        a[nw].len=a[u].len+1;
        while(u&&!a[u].c[x]) {
            a[u].c[x]=nw;
            u=a[u].f;
        }
        if(!u) a[nw].f=1;
        else {
            int t=a[u].c[x];
            if(a[t].len==a[u].len+1) a[nw].f=t;
            else {
                int nt=++tot;
                a[nt]=a[t];
                a[nt].len=a[u].len+1;
                while(u&&a[u].c[x]==t) {
                    a[u].c[x]=nt;
                    u=a[u].f;
                }
                a[nw].f=a[t].f=nt;
            }
        }
    }
}T;
vector<int> e[MAXN];
int cnt;
struct segment_tree {
    int L[MAXN*30],R[MAXN*30];
    void update(int &u,int l,int r,int x) {
        if(!u) u=++cnt;
        if(l==r) return ;
        int mid=(l+r)/2;
        if(x<=mid) update(L[u],l,mid,x);
        else update(R[u],mid+1,r,x);
    }
    int merge(int x,int y,int l,int r) {
        if(!x||!y) return x^y;
        int nw=++cnt;
        int mid=(l+r)/2;
        L[nw]=merge(L[x],L[y],l,mid);
        R[nw]=merge(R[x],R[y],mid+1,r);
        return nw;
    }
    bool query(int u,int l,int r,int x,int y) {
        if(!u||x>y) return false;
        if(l>=x&&r<=y) return true;
        int mid=(l+r)/2;
        int ans=0;
        if(x<=mid) ans|=query(L[u],l,mid,x,y);
        if(y>mid) ans|=query(R[u],mid+1,r,x,y);
        return ans;
    }
}T1;
int Fa[MAXN][20],dep[MAXN];
void dfs(int u) {
    if(a[u].edp) {
        T1.update(a[u].rt,1,n,a[u].edp);
    }
    for(int i=1;i<=19;++i) {
        Fa[u][i]=Fa[Fa[u][i-1]][i-1];
    }
    for(auto t:e[u]) {
        Fa[t][0]=u; dep[t]=dep[u]+1;
        dfs(t);
        a[u].rt=T1.merge(a[u].rt,a[t].rt,1,n);
    }
}
int ind[MAXN],id[MAXN];
char ls[MAXN];
bool check(int x,int A,int B,int C,int D) {
    int t=id[D];
    for(int i=19;i>=0;--i) {
        if(Fa[t][i]&&a[Fa[t][i]].len>=x) {
            t=Fa[t][i];
        }
    }
    return T1.query(a[t].rt,1,n,A+x-1,B);
}
int main () {
    scanf("%d%d",&n,&Q);
    cin>>st;
    st='1'+st;
    for(int i=1;i<=n/2;++i) swap(st[i],st[n-i+1]);
    for(int i=1;i<=n;++i) {
        T.insert(st[i]-'a');
    }
    int u=1;
    for(int i=1;i<=n;++i) {
        u=a[u].c[st[i]-'a'];
        a[u].edp=i; id[i]=u;
    }
    for(int i=2;i<=T.tot;++i) {
        e[a[i].f].push_back(i);
    }
    dep[1]=1;
    dfs(1);
    while(Q--) {
        int A,B,C,D;
        scanf("%d%d%d%d",&A,&B,&C,&D);
        A=n-A+1;
        B=n-B+1;
        C=n-C+1;
        D=n-D+1;
        swap(A,B); swap(C,D);
        int l=0,r=D-C+2,mid;
        while(l+1<r) {
            mid=(l+r)/2;
            if(check(mid,A,B,C,D)) l=mid;
            else r=mid;
        }
        printf("%d\n",l);
    }
    return 0;
}

REPEATS - Repeats

好题。

考虑重复,其实就是周期,那么根据 border 论可知。

当存在两段相交区间时,红色部分就是周期。

所以实际上两个串的结尾位置相越少,周期越短,可重复次数越多,对于一个 edp 来说,找到两个最近的 edp ,记他们之间的距离为 x ,当前节点最长后缀长度为 len ,那么贡献就是 1+\lfloor\frac {len}{x}\rfloor

然后取个 \max 就行了,容易发现,这样一定是不漏的。

时间复杂度 O(n\log n)

总结:

比如很多树上数据结构可以和 $SAM$ 结合使用。 # 坑 [P5284 [十二省联考 2019] 字符串问题](https://www.luogu.com.cn/problem/P5284) [loj #6071. 「2017 山东一轮集训 Day5」字符串](https://loj.ac/p/6071) [P4482 [BJWC2018] Border 的四种求法](https://www.luogu.com.cn/problem/P4482) [P4081 [USACO17DEC] Standing Out from the Herd P](https://www.luogu.com.cn/problem/P4081) [P4770 [NOI2018] 你的名字](https://www.luogu.com.cn/problem/P4770) [Ж-function](https://www.luogu.com.cn/problem/CF1098F) [P5576 [CmdOI2019] 口头禅](https://www.luogu.com.cn/problem/P5576)