之前没学懂,补一下。
阅前说明:大量抄袭了 的博客。(没问过原主人,如果涉及侵权可以让我删除)
原理
首先我们需要的是一个可以识别一个串的所有子串的自动机,这个自动机就是 。
一些定义:
不知道叫什么
我们的 建出来是一个 图,然后从源点都其他点的所有路径的并恰好就是这个字符串的所有子串,很厉害。
然后这个东西可以保证做到点数 ,边数 ,建自动机的过程均摊
顾名思义,就是结束位置了,对于一个串的每个子串,他都有一个 ,比如 这个串中, 的 就是 两个位置,同理 也是 两个位置,我们称两个 相同的子串为 等价类。
下面证明一些关于 的性质。
- 引理 :如果 为 的后缀 ,那么
证明,这个是显然的,如果 为 的后缀的话,那么每个 出现的位置都可以发现有一个 ,所以显然 出现的位置是 的超集。
- 引理 :对于两个 要么不交,要么包含。
证明,其实要证的就是如果相交,那么一定是包含关系,我们先假设 ,倘若 有交,那么显然一定存在某个位置出现了 同时出现了 ,也就是说 为 的后缀,然后由引理 可以得到 。
- 引理 :对于一个 等价类中的东西,最长的那个串到最短的串一定是连续一段的字符串。
证明,设最长的串为 ,最短的串为 ,然后根据引理 可以知道,一个 等价类中的东西都是后缀关系,所以对于中间每个串 (长度大于 ) ,都有 为 的后缀,所以 ,又因为 所以 ,然后长度是连续的,所以自然就是一段连续后缀。
然后要么不交要么包含,这么个条件实际上就是类似于一个树形结构。
我们考虑按等价类来划分节点,记一个节点中长度最长的字符串为 。
那么只要在这个 前面增加一个字符,那么新生成的字符串 一定和原本的字符串不在一个等价类中。
反正都抄了那么多了,图也搬了
对于上面这张图,我们记绿色的这几个节点为终止节点,表示 最后 加入的字符所构成的后缀属于哪些节点,那么我们接下来新加字符就是在这些对应节点中的每个后缀增加一个字符,具体如何实现直接讲代码(这东西不看代码真的懂不了一点)。
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;对了 表示这个等价类中最长长度的字符串是多长, 表示当前等价类在 上的父亲是谁, 就是和自动机类似,当下一个节点为 时,指向哪个节点。
首先是 就是最简单的新建节点,以及更新终止节点。
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;跳到发现某个节点他存在过和当前新增的字母的后缀,如果指向的那个节点,记为 ,的 就说明那个节点中没有比,当前 的后缀要长的后缀,所以可以直接令后缀链接 ,那么这时候你就完成了 的后缀的下放,而对于 的后缀不用管,因为那些后缀在 的祖先节点都已经被放过了。
if(a[t].len==a[u].len+1) a[nw].f=t;但是当发现 的后缀最大值要大于 时,情况就有所不同了。
假如我们在一个点他最大的 为 ,指向的下一个点的 的区间范围为 ,那么我们就要把 的后缀拎出来,新开一个节点,然后让原本的那个节点的后缀链接连向新开的节点,那么这样就完成了 的分裂,顺便的,要把原本节点在 的后缀链接上连着原本节点的那些边全部发给新的节点,这个看上去也是比较显然的。
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;知道了上面这些之后,想做到理解性默写就不难了,其实就是一个动态维护 的一个过程,仅此而已。
有比较多的应用,下面会一一讲解。
靠,手滑,把我的博客删掉了,生气,得重写一次
不同字串个数
两种求法
- 利用 树上的性质,所有节点不交或包含,所以统计
#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;
}- 在 上做
众所周知, 建出来之后是一个 图,且所有本质不同的子串个数其实就是他到其他节点的所有路径和, 图统计路径即可。
#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;
}动态维护本质不同的子串数量,显然用法 不太行,用法 ,直接维护 即可。
#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;
}子串出现次数
一个子串的出现次数其实就是他的 个数,对于每个前缀 ,我们只需要直接暴力对于他们对应的节点 ,然后其他节点统计一下子树中的 和即可。
#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;
}第 小串
这个东西也不难搞。
对于询问本质不同的,直接在 的 图上走路径,然后类似的搞搞就好了。
对于询问可重的,我们求出每个子串的出现次数,然后也是直接走就好了。
#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
首先对于一个串建 然后用另一个串在上面条,如果 ,那么就跳父亲,然后令 (更新过的),因为贪心肯定是想让他尽可能大,然后就没了。
LCS2 - Longest Common Substring II
选择一个串作为匹配串,将其他串轮流建 ,然后将匹配串丢到所有串的 上跑一下,对于某一个前缀 ,他的贡献是在其他所有串的 上跑完之后,最小的那个值。
#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;
}比较有意思的一个小应用,显然看到循环同构,可以想到破坏成链然后去匹配,但是这时候可能长度会超过 ,强制让长度 ,如果 后不在当前节点中了,就跳父亲即可。
然后成功后,贡献就是对应节点的出现次数,不过循环同构可能会出现重复字符串,所以需要打个标记,表示这个字符串贡献过了。
#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;
}好题。
首先显然对于 建出 ,然后对于 的每个前缀 ,令他匹配一下 ,看一下最多匹配多少,记为 。
然后对于某一个询问,相当于问
遇到这种问题,我们要么考虑钦定选择某一边,要么二分直接确定下界。
这里考虑二分,那么相当于询问 是否 ,如果是就说明成功了。
这个东西可以用 表维护,时间复杂度
#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和后缀树的关系
虽然我不知道他们有什么关系,不过反正是一种简单应用。
首先由一个结论:两个串的 为他们在 树上 的最长长度,而当题目询问我们 的时候可以把串倒过来,这样就可以求了。
要我们求 ,把串倒过来,然后做,可以发现其实就是 树上两两点之间的距离和,我一开始写了一个 ,但是不知道为什么不对,然后贺题用了拆贡献方法
然后就做完了。
#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;
}广义
识别一些串的所有子串,每次做完之后将 ,然后再做一次,看上去很显然,但是貌似有 树上的正统做法,反正我不学,无所谓。
对于所有串建出 后还要静态区间数颜色,不过可以很暴力的做。
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;
}板子题没什么好说的。
线段树合并
这个也不难,主要是询问涉及区间的时候可能就要用到这个。
和求子串出现次数是类似的,对于每个前缀 的出现位置给他的节点 加上 然后线段树合并就好了。
首先我们肯定是尽量匹配上 ,然后从后往前看一下能不能找到一个可替换位置。
先考虑 的情况。
显然就是找到最后一个可替换位置,然后就没什么好说的了。
考虑一般情况,倘若现在匹配了长度为 的东西,那么一个位置如果是可替换的,那么那个 节点的 中就要存在 的位置,那么这个位置就是可替换的,所以就是维护一个 就好了。
时间复杂度
#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;
}首先要求 ,将串倒过来求 。
然后容易想到的是要二分答案,二分一个长度,然后我们考虑让 去匹配 ,那么也就是说一定是在 前缀的祖先中的某个节点有 ,所以考虑倍增,然后判一下 即可。
时间复杂度
#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;
}好题。
考虑重复,其实就是周期,那么根据 论可知。
当存在两段相交区间时,红色部分就是周期。
所以实际上两个串的结尾位置相越少,周期越短,可重复次数越多,对于一个 来说,找到两个最近的 ,记他们之间的距离为 ,当前节点最长后缀长度为 ,那么贡献就是
然后取个 就行了,容易发现,这样一定是不漏的。
时间复杂度
总结:
本身知识点不难,更多的是一些应用与性质的利用, 性质和应用也不多就那些,所以考点重心还是题目本身,而不是 ,不过使用了 的性质转化了题目而已。
比如很多树上数据结构可以和 结合使用。
坑
loj #6071. 「2017 山东一轮集训 Day5」字符串


