后缀自动机学习笔记
xtx1092515503 · · 个人记录
这里是SAM学习笔记。
SAM是Suffix Automaton 萨菲克斯自动马桶的缩写,其中文翻译是后缀自动机。
顾名思义,其是一个自动机。SAM接受一个串,当且仅当其是母串
这里我们给出一些定义:
所有使用黑板粗体格式的字符
所有使用大写格式的字符
所有使用黑体格式的字符
所有使用正体格式的字符
所有使用小写格式的字符
所有被代码框框起来的字符 s,表示 s 这个字符。ss 之类,可能表示字符串,也可能表示一段代码,视具体情形而定。
所有无任何格式的字符 s,表示一种专有名词。
定义字符串的下标从
定义
定义
定义母串
定义
O.后缀自动机
模板的讲解。
首先,若我们暴力地想建出SAM,明显点数是
唯一的优化方式即为缩减状态数。
定义 ab,abab,则
我们发现,在 ab 和 b 的
我们把两个串
我们发现,通过上述的
则有如下定理:
这是非常显然的——更长的串都匹配上了,更短的串还能匹配不上吗?
由
实际上是
由
于是我们定义
考虑往
这也意味着这样的操作是一个划分操作——每个
而这就意味着,总节点数不会超过
但是,知道有parent tree这种东西,又有什么用呢?
我们将看到,parent tree中所有节点,刚好可以被看作是自动机上节点! 空串节点可被看作是DAG的源点,所有包含母串的集合
在自动机中,我们需要保证,一条从源点到某个点
我们已经保证自动机的点数是
事实上,其边数也是
我们考虑母串的全部后缀。明显,只需要保证每个后缀都能有一条从根到其代表节点的路径,即可拍胸脯保证这是一个合法的SAM了(因为这就是SAM的定义呀)。与此同时,我们不仅可以从源点出发正着跑路径,也可以从终止节点出发倒着跑路径——二者是等价的。
我们考虑对自动机求出其任意一棵生成树,并通过往生成树中加入新边来复原出自动机。然后,我们考虑遍历每个终止节点,并且按照某种顺序遍历该终止节点对应集合内所有的字符串。
如果对于其中一个串,存在一条从根到该终止节点的路径来表示该串,显然该串已经被check过合法了,可以跳过;
否则,即不存在一条这样的路径(尚未被添加入生成树张成的自动机中)。我们考虑将这条路径上尚未被添加的边(因为已经有一棵生成树,所以最多只有一条缺失的边)加入自动机中。这时,其parent tree中父亲、祖父,乃至所有祖先的集合中,当前字符串的后缀字符串,其也有一条路径同时被铺出来了(尽管不一定是原本自动机中的路径,但是只要有一条路径就行了)。之后,考虑再check下一个串即可。可以发现,此过程添加的边不会超过
后缀自动机的构造是在线的,增量的。这意味着可以在任意时刻,往任意节点后面添加字符(也就意味着可以很容易用它来实现树上SAM,或是多串SAM之类)。
一个典型的SAM长这样:
如图,底下一溜节点是原串的前缀,它们是一定存在于自动机里的(即parent tree中一整条终止节点路径)。而上面的节点,以及那些虚线的边,是不一定存在,也不一定是这么连接的。
我们定义一个集合
以下,会照着程序解释。
struct Suffix_Automaton{int ch[26],len,fa;}t[N<<1];//a SAM has 2n nodes!!!
int cnt=1;
int Add(int x,int c){//add a character c after node x,return the index.
int xx=++cnt;//the index of the newly-added node
t[xx].len=t[x].len+1;//the maximal length of strings in xx.
for(;x&&!t[x].ch[c];x=t[x].fa)t[x].ch[c]=xx;
if(!x){t[xx].fa=1;return xx;}
int y=t[x].ch[c];
if(t[y].len==t[x].len+1){t[xx].fa=y;return xx;}
int yy=++cnt;t[yy]=t[y];
t[yy].len=t[x].len+1;
t[y].fa=t[xx].fa=yy;
for(;x&&t[x].ch[c]==y;x=t[x].fa)t[x].ch[c]=yy;
return xx;
}
首先,记原来的串为
在添加完一个
然后,不断地跳
如果一路跳最终跳到了根,显然整个串中都没有出现过
否则,即其存在一个
若 t[y].len==t[x].len+1 成立,则应有
于是,下面就有 t[y].len!=t[x].len+1。而因为parent tree中的长度是随着深度增加而递增的,故实际上是t[y].len>t[x].len+1。
此时,考虑
我们选择,另开一个新节点
那问题来了,为什么我们不交换
于是以上就是模板的全部内容。
可以发现,后缀自动机的复杂度是 map<int,int> 来储存儿子们,可以将复杂度优化至
I.【模板】后缀自动机 (SAM)
虽说是模板但也不是纯粹板子!
SAM应用1:求一个子串的出现次数(相当于字符串匹配)。
因为同一个
那么我们如何求出一个
Parent Tree的实质是往一个集合中的串的开头添加新字符的过程。于是,我们考虑DP。设
时间复杂度
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1001000;
struct Suffix_Automaton{int ch[26],len,fa;}t[N<<1];//a SAM has 2n nodes!!!
int f[N<<1];
int cnt=1;
int Add(int x,int c){//add a character c after node x,return the index.
int xx=++cnt;//the index of the newly-added node
f[xx]=1;//stands that x is a finishing point
t[xx].len=t[x].len+1;//the maximal length of strings in xx.
for(;x&&!t[x].ch[c];x=t[x].fa)t[x].ch[c]=xx;
if(!x){t[xx].fa=1;return xx;}
int y=t[x].ch[c];
if(t[y].len==t[x].len+1){t[xx].fa=y;return xx;}
int yy=++cnt;t[yy]=t[y];
t[yy].len=t[x].len+1;
t[y].fa=t[xx].fa=yy;
for(;x&&t[x].ch[c]==y;x=t[x].fa)t[x].ch[c]=yy;
return xx;
}
int n;
char s[N];
vector<int>v[N<<1];
void dfs(int x){for(auto y:v[x])dfs(y),f[x]+=f[y];}
ll res;
int main(){
scanf("%s",s),n=strlen(s);
for(int i=0,las=1;i<n;i++)las=Add(las,s[i]-'a');
for(int i=2;i<=cnt;i++)v[t[i].fa].push_back(i);
dfs(1);
for(int i=1;i<=cnt;i++)if(f[i]!=1)res=max(res,1ll*f[i]*t[i].len);
printf("%lld\n",res);
return 0;
}
II.【模板】广义后缀自动机(广义 SAM)
我们之前提到过一句话:“后缀自动机的构造是在线的,增量的。”而这题,便是其应用之一。
首先,有一种暴力的解法,是直接将所有东西全都依次插入SAM,每次插入从
我们重新翻出SAM的代码:
int Add(int x,int c){
int xx=++cnt;t[xx].len=t[x].len+1;
for(;x&&!t[x].ch[c];x=t[x].fa)t[x].ch[c]=xx;
if(!x){t[xx].fa=1;return xx;}
int y=t[x].ch[c];
if(t[y].len==t[x].len+1){t[xx].fa=y;return xx;}
int yy=++cnt;t[yy]=t[y];
t[yy].len=t[x].len+1;
t[y].fa=t[xx].fa=yy;
for(;x&&t[x].ch[c]==y;x=t[x].fa)t[x].ch[c]=yy;
return xx;
}
在插入的时候,有可能发生如下一些情况:
当然,尽管有着这样那样的错误,暴力插串的构建SAM方式,在大多数题中都是可以通过的!
自然,学还是要学正解的。其解决的一种方式是,只要使得任何被插入的
我们考虑一下为什么会出现有 a,那若我们又来了一个同样的串 a,插在根节点后面,此时根节点就已经有 a 儿子了。而解决办法就是,若两个串具有相同的
然后这又涉及到一个疑问——trie树上的边到底应该按照何种顺序加入呢?你可能会直接说“当然是dfs一遍加入啦”,但是dfs序加入就会使得我们第二次跳父亲的那重循环保证不了每条边最多只会被重定向一次,导致复杂度最多是
但是,这种建trie的方式是离线的。有没有在线的方式呢?
有!只需要克服我们前面讲的两个问题就行了。
这里是正确的在线做法:
int Add(int x,int c){
if(t[x].ch[c]){
int y=t[x].ch[c];
if(t[y].len==t[x].len+1)return y;//(x,c) has been added into the tree already.
int yy=++cnt;t[yy]=t[y];
t[yy].len=t[x].len+1,t[y].fa=yy;
for(;x&&t[x].ch[c]==y;x=t[x].fa)t[x].ch[c]=yy;
return yy;
}
int xx=++cnt;t[xx].len=t[x].len+1;
for(;x&&!t[x].ch[c];x=t[x].fa)t[x].ch[c]=xx;
if(!x){t[xx].fa=1;return xx;}
int y=t[x].ch[c];
if(t[y].len==t[x].len+1){t[xx].fa=y;return xx;}
int yy=++cnt;t[yy]=t[y];
t[yy].len=t[x].len+1;
t[y].fa=t[xx].fa=yy;
for(;x&&t[x].ch[c]==y;x=t[x].fa)t[x].ch[c]=yy;
return xx;
}
可以看到,二者唯一的差别就是第二个前面加的一坨特判。这一坨特判刚好解决了上述两个问题,应该很容易理解(毕竟,这个特判是我自己yy出来的——足以发现它有多么清晰)
无论是在线还是离线,只要是合法的做法,都不会出现孤立点、到达不了的点或是其它奇奇怪怪的东西。可以直接当作普通SAM使用。
SAM应用2:求不同子串数量
不同子串数量可以由两种思路求得。
一种是从自动机角度想,从原点出发所有路径都是子串;而因为自动机的性质,任意一条子串的路径唯一,所以只需要统计自原点出发的路径数量即可。下文中错误的解法使用的是这种思路。
另一种是从parent tree角度想。parent tree所包含的过程是不断往串开头加东西的过程,而endpos相同的串(换句话说,所有相同的子串)被归到同一个点上。这也意味着只需统计所有点上不同串的数量即可。不同串的数量,就是
错误代码(事实上可以AC):
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1001000;
int cnt=1;
struct Suffix_Automaton{int ch[26],fa,len;}t[N<<1];
int Add(int x,int c){
int xx=++cnt;t[xx].len=t[x].len+1;
for(;x&&!t[x].ch[c];x=t[x].fa)t[x].ch[c]=xx;
if(!x){t[xx].fa=1;return xx;}
int y=t[x].ch[c];
if(t[y].len==t[x].len+1){t[xx].fa=y;return xx;}
int yy=++cnt;t[yy]=t[y];
t[yy].len=t[x].len+1;
t[y].fa=t[xx].fa=yy;
for(;x&&t[x].ch[c]==y;x=t[x].fa)t[x].ch[c]=yy;
return xx;
}
char s[N];
int n,S,in[N<<1];
queue<int>q;
ll f[N<<1],res;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%s",s),S=strlen(s);
for(int j=0,las=1;j<S;j++)las=Add(las,s[j]-'a');
}
for(int i=1;i<=cnt;i++)for(int j=0;j<26;j++)if(t[i].ch[j])in[t[i].ch[j]]++;
for(int i=1;i<=cnt;i++)if(!in[i])q.push(i);
f[1]=1;
while(!q.empty()){
int x=q.front();q.pop();
res+=f[x];
for(int i=0;i<26;i++){
if(!t[x].ch[i])continue;
f[t[x].ch[i]]+=f[x];
if(!--in[t[x].ch[i]])q.push(t[x].ch[i]);
}
}
printf("%lld\n",res-1);
return 0;
}
离线代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1001000;
namespace TR{
int cnt=1;
struct Trie{int ch[26],id;}t[N];
void Add(char *s,int n){
int x=1;
for(int i=0;i<n;i++){
if(!t[x].ch[s[i]-'a'])t[x].ch[s[i]-'a']=++cnt;
x=t[x].ch[s[i]-'a'];
}
}
}
namespace SAM{
int cnt=1;
struct Suffix_Automaton{int ch[26],fa,len;}t[N<<1];
int Add(int x,int c){
int xx=++cnt;t[xx].len=t[x].len+1;
for(;x&&!t[x].ch[c];x=t[x].fa)t[x].ch[c]=xx;
if(!x){t[xx].fa=1;return xx;}
int y=t[x].ch[c];
if(t[y].len==t[x].len+1){t[xx].fa=y;return xx;}
int yy=++cnt;t[yy]=t[y];
t[yy].len=t[x].len+1;
t[y].fa=t[xx].fa=yy;
for(;x&&t[x].ch[c]==y;x=t[x].fa)t[x].ch[c]=yy;
return xx;
}
}
using namespace SAM;
queue<int>q;
void build(){
q.push(1),TR::t[1].id=1;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=0;i<26;i++)if(TR::t[x].ch[i])TR::t[TR::t[x].ch[i]].id=Add(TR::t[x].id,i),q.push(TR::t[x].ch[i]);
}
}
char s[N];
int n,S;
ll res;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%s",s),S=strlen(s),TR::Add(s,S);
build();
for(int i=1;i<=cnt;i++)res+=t[i].len-t[t[i].fa].len;
printf("%lld\n",res);
return 0;
}
在线做法:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1001000;
int cnt=1;
struct Suffix_Automaton{int ch[26],fa,len;}t[N<<1];
int Add(int x,int c){
if(t[x].ch[c]){
int y=t[x].ch[c];
if(t[y].len==t[x].len+1)return y;//(x,c) has been added into the tree already.
int yy=++cnt;t[yy]=t[y];
t[yy].len=t[x].len+1,t[y].fa=yy;
for(;x&&t[x].ch[c]==y;x=t[x].fa)t[x].ch[c]=yy;
return yy;
}
int xx=++cnt;t[xx].len=t[x].len+1;
for(;x&&!t[x].ch[c];x=t[x].fa)t[x].ch[c]=xx;
if(!x){t[xx].fa=1;return xx;}
int y=t[x].ch[c];
if(t[y].len==t[x].len+1){t[xx].fa=y;return xx;}
int yy=++cnt;t[yy]=t[y];
t[yy].len=t[x].len+1;
t[y].fa=t[xx].fa=yy;
for(;x&&t[x].ch[c]==y;x=t[x].fa)t[x].ch[c]=yy;
return xx;
}
char s[N];
int n,S,in[N<<1];
ll res;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%s",s),S=strlen(s);
for(int j=0,las=1;j<S;j++)las=Add(las,s[j]-'a');
}
for(int i=1;i<=cnt;i++)for(int j=0;j<26;j++)if(t[i].ch[j])in[t[i].ch[j]]++;
for(int i=1;i<=cnt;i++)res+=t[i].len-t[t[i].fa].len;
printf("%lld\n",res);
return 0;
}
III.[ZJOI2015]诸神眷顾的幻想乡
假如这棵树是定根的,那么其就可以被看作一棵trie,trie上本质不同子串数可以直接被建立广义SAM解决;但是这棵树不定根,路径可能拐弯,咋办呢?
发现,其保证叶子数量
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m;
namespace TR{//both for ordinary tree and for trie
vector<int>v[100100];
int cnt=1;
int col[100100],in[100100];
struct Trie{
int ch[10],id;
}t[2000100];
void dfs(int x,int fa,int FA){
if(!t[FA].ch[col[x]])t[FA].ch[col[x]]=++cnt;
for(auto y:v[x])if(y!=fa)dfs(y,x,t[FA].ch[col[x]]);
}
void init(){
for(int i=1;i<=n;i++)scanf("%d",&col[i]);
for(int i=1,x,y;i<n;i++)scanf("%d%d",&x,&y),v[x].push_back(y),v[y].push_back(x),in[x]++,in[y]++;
for(int i=1;i<=n;i++)if(in[i]==1)dfs(i,0,1);
}
}
namespace SAM{
int cnt=1;
struct Suffix_Automaton{
int ch[10],len,fa;
}t[4000100];
int Add(int x,int c){
int xx=++cnt;t[xx].len=t[x].len+1;
for(;x&&!t[x].ch[c];x=t[x].fa)t[x].ch[c]=xx;
if(!x){t[xx].fa=1;return xx;}
int y=t[x].ch[c];
if(t[y].len==t[x].len+1){t[xx].fa=y;return xx;}
int yy=++cnt;t[yy]=t[y];
t[yy].len=t[x].len+1;
t[xx].fa=t[y].fa=yy;
for(;x&&t[x].ch[c]==y;x=t[x].fa)t[x].ch[c]=yy;
return xx;
}
queue<int>q;
void build(){
q.push(1);
TR::t[1].id=1;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=0;i<m;i++)if(TR::t[x].ch[i])TR::t[TR::t[x].ch[i]].id=Add(TR::t[x].id,i),q.push(TR::t[x].ch[i]);
}
}
}
using namespace SAM;
ll res;
int main(){
scanf("%d%d",&n,&m);
TR::init();
build();
for(int i=1;i<=cnt;i++)res+=t[i].len-t[t[i].fa].len;
printf("%lld\n",res);
return 0;
}
IV.[TJOI2019]甲苯先生和大中锋的字符串
判断一个子串出现几次,我们仍然可以采取之前提到的遍历parent tree的方法。
考虑一个等价类。明显,这个等价类中所有串都出现且仅出现了(等价类出现的次数)。若这个次数恰好为
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int T,cnt,n,m;
struct Suffix_Automaton{int ch[26],len,fa;}t[200100];
int sz[200100];
int Add(int x,int c){
int xx=++cnt;t[xx].len=t[x].len+1;
sz[xx]=1;
for(;x&&!t[x].ch[c];x=t[x].fa)t[x].ch[c]=xx;
if(!x){t[xx].fa=1;return xx;}
int y=t[x].ch[c];
if(t[y].len==t[x].len+1){t[xx].fa=y;return xx;}
int yy=++cnt;t[yy]=t[y];
t[yy].len=t[x].len+1;
t[y].fa=t[xx].fa=yy;
for(;x&&t[x].ch[c]==y;x=t[x].fa)t[x].ch[c]=yy;
return xx;
}
vector<int>v[200100];
ll d[100100],mx;
void dfs(int x){for(auto y:v[x])dfs(y),sz[x]+=sz[y];}
char s[100100];
int main(){
scanf("%d",&T);
while(T--){
scanf("%s%d",s,&m),n=strlen(s),cnt=1,mx=0;
for(int i=0,las=1;i<n;i++)las=Add(las,s[i]-'a');
for(int i=2;i<=cnt;i++)v[t[i].fa].push_back(i);
dfs(1);
for(int i=2;i<=cnt;i++)if(sz[i]==m)d[t[t[i].fa].len]--,d[t[i].len]++;
for(int i=n;i;i--)d[i]+=d[i+1],mx=max(mx,d[i]);
if(!mx)puts("-1");
else for(int i=n;i;i--)if(d[i]==mx){printf("%d\n",i);break;}
for(int i=1;i<=cnt;i++){memset(t[i].ch,0,sizeof(t[i].ch)),t[i].len=t[i].fa=sz[i]=0,v[i].clear();}
for(int i=1;i<=n;i++)d[i]=0;
}
return 0;
}
V.[APIO2014]回文串
具体分析详见本人的SA题解,这里主要是讲解使用SAM求子串出现次数的方法。
SAM应用3:查询一个子串的出现次数。
这个思想很简单,只需要找到该子串对应的
我们考虑记录
使用马拉车求出所有极大回文串,复杂度
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=300002;
typedef long long ll;
int cnt=1,ch[N<<1][26],fa[N<<1],len[N<<1],sz[N<<1];
int Add(int x,int c){
int xx=++cnt;len[xx]=len[x]+1;
sz[xx]=1;
for(;x&&!ch[x][c];x=fa[x])ch[x][c]=xx;
if(!x){fa[xx]=1;return xx;}
int y=ch[x][c];
if(len[y]==len[x]+1){fa[xx]=y;return xx;}
int yy=++cnt;fa[yy]=fa[y];
for(int i=0;i<26;i++)ch[yy][i]=ch[y][i];
len[yy]=len[x]+1;
fa[xx]=fa[y]=yy;
for(;x&&ch[x][c]==y;x=fa[x])ch[x][c]=yy;
return xx;
}
vector<int>v[N<<1];
void dfs(int x){for(auto y:v[x])dfs(y),sz[x]+=sz[y];}
int n,id[N];
ll val(int l,int r){
l=max(l,0),r=min(r,n-1);
int x=id[r];
for(int i=18;i>=0;i--)if(ch[x][i]&&len[ch[x][i]]>=r-l+1)x=ch[x][i];
return 1ll*sz[x]*(r-l+1);
}
char s[N],ss[N<<1];
int rad[N<<1];
ll res;
void Manacher(){
int T=0,ret=0;
ss[T++]='$';
for(int i=0;i<n;i++)ss[T++]=s[i],ss[T++]='$';
int Centre=-1,Rpos=-1;
for(int i=0;i<T;i++){
if(i<=Rpos)rad[i]=min(Rpos-i,rad[2*Centre-i]);
while(i-rad[i]>=0&&i+rad[i]<T&&ss[i-rad[i]]==ss[i+rad[i]])rad[i]++;
if(Rpos<i+rad[i])Centre=i,Rpos=i+rad[i];
res=max(res,val((i-rad[i]+1)>>1,(i+rad[i]-2)>>1));
}
}
int main(){
scanf("%s",s),n=strlen(s);
for(int i=0,las=1;i<n;i++)id[i]=las=Add(las,s[i]-'a');
for(int i=1;i<=cnt;i++)ch[i][0]=fa[i],v[fa[i]].push_back(i);
for(int j=1;j<=18;j++)for(int i=1;i<=cnt;i++)ch[i][j]=ch[ch[i][j-1]][j-1];
dfs(1);
Manacher();
printf("%lld\n",res);
return 0;
}
VI.[TJOI2015]弦论
SAM应用4:求字典序第
前面说过,自动机部分接受且仅接受原串后缀,但实际上自动机中节点都是后缀的前缀,即子串。于是在自动机上先倒着拓扑DP一下,然后正着扫一遍即可求出第
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int cnt=1;
struct Suffix_Automaton{
int ch[26],fa,len;
}t[1000100];
int sz[1000100];
int Add(int x,int c){
int xx=++cnt;t[xx].len=t[x].len+1;
sz[xx]=1;
for(;x&&!t[x].ch[c];x=t[x].fa)t[x].ch[c]=xx;
if(!x){t[xx].fa=1;return xx;}
int y=t[x].ch[c];
if(t[y].len==t[x].len+1){t[xx].fa=y;return xx;}
int yy=++cnt;t[yy]=t[y];
t[yy].len=t[x].len+1;
t[xx].fa=t[y].fa=yy;
for(;x&&t[x].ch[c]==y;x=t[x].fa)t[x].ch[c]=yy;
return xx;
}
int n,m,tp,in[1000100];
char s[500100];
ll f[1000100];
queue<int>q;
vector<int>v[1000100],u[1001000];
void dfs(int x){for(auto y:u[x])dfs(y),sz[x]+=sz[y];}
int main(){
scanf("%s%d%d",s,&tp,&m),n=strlen(s);
for(int i=0,las=1;i<n;i++)las=Add(las,s[i]-'a');
for(int i=1;i<=cnt;i++)for(int j=0;j<26;j++)if(t[i].ch[j])v[t[i].ch[j]].push_back(i),in[i]++;
for(int i=1;i<=cnt;i++)u[t[i].fa].push_back(i);
dfs(1);
for(int i=1;i<=cnt;i++)if(!in[i])q.push(i);
while(!q.empty()){
int x=q.front();q.pop();
for(auto y:v[x]){
f[y]+=f[x]+(tp?sz[x]:1);
if(!--in[y])q.push(y);
}
}
if(m>f[1]){puts("-1");return 0;}
for(int x=1;m>0;)for(int i=0,y;i<26;i++){
if(!(y=t[x].ch[i]))continue;
if(m>f[y]+(tp?sz[y]:1))m-=f[y]+(tp?sz[y]:1);else{putchar('a'+i),m-=(tp?sz[y]:1),x=y;break;}
}
return 0;
}
VII.[BZOJ2555]SubString
如果要在动态建SAM的过程中同时维护parent tree中的子树和,明显需要一种支持修改树的数据结构来维护。显然,这里应该使用LCT。
维护子树和,可以考虑LCT中经典的记录虚子树和的trick。然后剩下就是俩模板的拼接了。
不知道为什么,交上去会MLE(尽管空间经计算是不会爆炸的),但是与正解对拍是OK的(
MLE代码:
#include<bits/stdc++.h>
using namespace std;
const int N=600100;
#define lson t[x].ch[0]
#define rson t[x].ch[1]
struct Link_Cut_Tree{int ch[2],fa,sumi,sum;bool rev;}t[N<<1];
void REV(int x){t[x].rev^=1,swap(lson,rson);}
void ADD(int x){t[x].sumi++,t[x].sum++;}
void pushup(int x){t[x].sum=t[x].sumi;if(lson)t[x].sum+=t[lson].sum;if(rson)t[x].sum+=t[rson].sum;}
void pushdown(int x){if(!t[x].rev)return;if(lson)REV(lson);if(rson)REV(rson);t[x].rev=false;}
int identify(int x){if(x==t[t[x].fa].ch[0])return 0;if(x==t[t[x].fa].ch[1])return 1;return -1;}
void rotate(int x){
register int y=t[x].fa,z=t[y].fa,dirx=identify(x),diry=identify(y),b=t[x].ch[!dirx];
if(diry!=-1)t[z].ch[diry]=x;t[x].fa=z;
if(b)t[b].fa=y;t[y].ch[dirx]=b;
t[y].fa=x,t[x].ch[!dirx]=y;
pushup(y),pushup(x);
}
void pushall(int x){if(identify(x)!=-1)pushall(t[x].fa);pushdown(x);}
void splay(int x){for(pushall(x);identify(x)!=-1;rotate(x))if(identify(t[x].fa)!=-1)rotate(identify(x)==identify(t[x].fa)?t[x].fa:x);}
void access(int x){for(register int y=0;x;x=t[y=x].fa)splay(x),t[x].sumi+=t[rson].sum-t[y].sum,rson=y,pushup(x);}
void makeroot(int x){access(x),splay(x),REV(x);}
void split(int x,int y){makeroot(x),access(y),splay(y);}
bool link(int x,int y){split(x,y),t[x].fa=y,t[y].sumi+=t[x].sum,pushup(y);}
void cut(int x,int y){split(x,y),t[y].ch[0]=t[x].fa=0,pushup(y);}
struct Suffix_Automaton{int fa,len,ch[2];}sam[N<<1];
int cnt=1,las=1;
void Setfa(int x,int y){//set fa[x] to y.
if(sam[x].fa)cut(x,sam[x].fa);
sam[x].fa=y,link(x,y);
}
int Add(int x,int c){
int xx=++cnt;sam[xx].len=sam[x].len+1;
for(;x&&!sam[x].ch[c];x=sam[x].fa)sam[x].ch[c]=xx;
if(!x){Setfa(xx,1);return xx;}
int y=sam[x].ch[c];
if(sam[y].len==sam[x].len+1){Setfa(xx,y);return xx;}
int yy=++cnt;
for(int i=0;i<2;i++)sam[yy].ch[i]=sam[y].ch[i];Setfa(yy,sam[y].fa),sam[yy].len=sam[x].len+1;
Setfa(xx,yy),Setfa(y,yy);
for(;x&&sam[x].ch[c]==y;x=sam[x].fa)sam[x].ch[c]=yy;
return xx;
}
char s[N];
int S;
void Insert(){for(int i=0;i<S;i++)las=Add(las,s[i]-'A'),split(1,las),ADD(las);}
int Query(){
int x=1;
for(int i=0;i<S;i++)if(sam[x].ch[s[i]-'A'])x=sam[x].ch[s[i]-'A'];else return 0;
split(x,sam[x].fa);
return t[x].sum;
}
int q;
void deco(int msk){scanf("%s",s),S=strlen(s);for(int i=0;i<S;i++)msk=(msk*131+i)%S,swap(s[i],s[msk]);}
int msk;
char ord[10];
int main(){
scanf("%d",&q);
scanf("%s",s),S=strlen(s),Insert();
for(int tp;q--;){
scanf("%s",ord),tp=(ord[0]=='A'?0:1);
deco(msk);
if(tp)printf("%d\n",tp=Query()),msk^=tp;
else Insert();
}
return 0;
}
下面我们考虑另一种思路,即把子树求和,单点加变为路径加,单点求值。路径加是LCT经典操作。
但是,我们考虑在建parent tree的过程中我们有哪些操作:
- 为一个节点新增儿子
明显此时该儿子的答案是
- 在一条边中塞一个新节点
这时候,新添加的节点的答案就不是
写带 link 和 cut 的LCT是可以的。但是,我们这里可以使用定根LCT,即不使用 makeroot 的LCT。具体而言,显然情形1就直接连父亲即可,而情形2,假设我们往边 access(z) 再 splay(x),这下子
代码(可以通过):
#include<bits/stdc++.h>
using namespace std;
const int N=600100;
#define lson t[x].ch[0]
#define rson t[x].ch[1]
struct Link_Cut_Tree{int ch[2],fa,val,tag;}t[N<<1];
void ADD(int x,int y=1){t[x].val+=y,t[x].tag+=y;}
void pushdown(int x){if(lson)ADD(lson,t[x].tag);if(rson)ADD(rson,t[x].tag);t[x].tag=0;}
int identify(int x){if(x==t[t[x].fa].ch[0])return 0;if(x==t[t[x].fa].ch[1])return 1;return -1;}
void rotate(int x){
register int y=t[x].fa,z=t[y].fa,dirx=identify(x),diry=identify(y),b=t[x].ch[!dirx];
if(diry!=-1)t[z].ch[diry]=x;t[x].fa=z;
if(b)t[b].fa=y;t[y].ch[dirx]=b;
t[y].fa=x,t[x].ch[!dirx]=y;
}
void pushall(int x){if(identify(x)!=-1)pushall(t[x].fa);pushdown(x);}
void splay(int x){for(pushall(x);identify(x)!=-1;rotate(x))if(identify(t[x].fa)!=-1)rotate(identify(x)==identify(t[x].fa)?t[x].fa:x);}
void access(int x){for(register int y=0;x;x=t[y=x].fa)splay(x),rson=y;}
void linkson(int x,int y){t[y].fa=x;}//make y a son of x
void iterate(int x){
if(!x)return;
iterate(lson);
printf("%d ",x);
iterate(rson);
}
void expandedge(int x,int y,int z){//insert y between x and z
access(z),splay(x);//after this step, x's rson must be z.
t[y].fa=x,t[x].ch[1]=y;
t[z].fa=y,t[y].ch[1]=z;//insert y between x and z
t[y].val=t[z].val;
}
struct Suffix_Automaton{int fa,len,ch[2];}sam[N<<1];
int cnt=1,las=1;
int Add(int x,int c){
int xx=++cnt;sam[xx].len=sam[x].len+1;
for(;x&&!sam[x].ch[c];x=sam[x].fa)sam[x].ch[c]=xx;
if(!x){sam[xx].fa=1,linkson(1,xx);return xx;}
int y=sam[x].ch[c];
if(sam[y].len==sam[x].len+1){sam[xx].fa=y,linkson(y,xx);return xx;}
int yy=++cnt;sam[yy]=sam[y],sam[yy].len=sam[x].len+1;
sam[xx].fa=sam[y].fa=yy;
expandedge(sam[yy].fa,yy,y),linkson(yy,xx);
for(;x&&sam[x].ch[c]==y;x=sam[x].fa)sam[x].ch[c]=yy;
return xx;
}
char s[N];
int S;
void Insert(){for(int i=0;i<S;i++)las=Add(las,s[i]-'A'),access(las),splay(las),ADD(las);}
int Query(){
int x=1;
for(int i=0;i<S;i++)if(sam[x].ch[s[i]-'A'])x=sam[x].ch[s[i]-'A'];else return 0;
splay(x);
return t[x].val;
}
int q;
void deco(int msk){scanf("%s",s),S=strlen(s);for(int i=0;i<S;i++)msk=(msk*131+i)%S,swap(s[i],s[msk]);}
int msk;
char ord[10];
int main(){
scanf("%d",&q);
scanf("%s",s),S=strlen(s),Insert();
for(int tp;q--;){
scanf("%s",ord),tp=(ord[0]=='A'?0:1);
deco(msk);
if(tp)printf("%d\n",tp=Query()),msk^=tp;
else Insert();
}
return 0;
}