零、工具
SAM Drawer:一个画后缀自动机的工具
一、 模板
1. 后缀自动机(单串)
(1)插入(建SAM)
char s[N];
int ch[N*2][26],fa[N*2],len[N*2];
int last=1,tot=1;
void insert(int x)
{
int p=last;
int np=last=++tot;
len[np]=len[p]+1;
while(p&&!ch[p][x]) {
ch[p][x]=np; p=fa[p]; }
if(!p) fa[np]=1;
else
{
int q=ch[p][x];
if(len[q]==len[p]+1) fa[np]=q;
else
{
int nq=++tot;
for(int i=0;i<26;i++) ch[nq][i]=ch[q][i];
fa[nq]=fa[q]; fa[np]=fa[q]=nq;
len[nq]=len[p]+1;
while(p&&ch[p][x]==q)
{
ch[p][x]=nq; p=fa[p]; }
}
}
}
int main()
{
scanf("%s",s+1);
int ls=strlen(s+1);
for(int i=1;i<=ls;i++)
insert(s[i]-'a');
return 0;
}
(2)匹配
int p=1,now=0;
for(int i=1;i<=lt;i++)
{
int x=t[i]-'a';
if(ch[p][x])
{
now++; p=ch[p][x];}
else
{
while(p&&!ch[p][x]) p=fa[p];
if(!p){
p=1;now=0;}
else
{
now=len[p]+1; p=ch[p][x];}
}
}
2. 广义后缀自动机(多串)
广义自动机有在线/离线两种写法。(在线写法是流传比较广的,能通过大部分题目,但是有人说是假的,不知道为什么……)
(模板为洛谷广义后缀自动机)
(1)在线版
每次新加入一个串的时候,将last改为1。如果对应节点存在,进行修改;否则,新建节点。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define LL long long
using namespace std;
const int N=2e6+10;
int ch[N][26],len[N],fa[N];
char s[N];
int last,tot=1;
int insert(int c,int last)
{
int p=last;
if(ch[p][c])
{
int np=ch[p][c];
if(len[p]+1==len[np]) return np;
else
{
int nq=++tot;
len[nq]=len[p]+1;
for(int i=0;i<26;i++) ch[nq][i]=ch[np][i];
while(p&&ch[p][c]==np) ch[p][c]=nq,p=fa[p];
fa[nq]=fa[np],fa[np]=nq;
return nq;
}
}
int q=++tot;
len[q]=len[p]+1;
while(p&&!ch[p][c]) ch[p][c]=q,p=fa[p];
if(!p) fa[q]=1;
else
{
int np=ch[p][c];
if(len[p]+1==len[np]) fa[q]=np;
else
{
int nq=++tot;
len[nq]=len[p]+1;
for(int i=0;i<26;i++) ch[nq][i]=ch[np][i];
while(p&&ch[p][c]==np) ch[p][c]=nq,p=fa[p];
fa[nq]=fa[np],fa[np]=fa[q]=nq;
}
}
return q;
}
int main()
{
int n;
LL ans=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",s+1);
last=1;
int l=strlen(s+1);
for(int j=1;j<=l;j++) last=insert(s[j]-'a',last);
}
for(int i=2;i<=tot;i++) ans+=len[i]-len[fa[i]];
printf("%lld ",ans);
return 0;
}
(2)离线版
先将所有串放在一起建立一棵Trie树,然后按照bfs序建立后缀自动机。(当有两棵树的时候,封进结构体太爽了)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include

最低0.47元/天 解锁文章
1901

被折叠的 条评论
为什么被折叠?



