《SAM学习总结》
之前没学懂,补一下。
阅前说明:大量抄袭了
原理
首先我们需要的是一个可以识别一个串的所有子串的自动机,这个自动机就是
一些定义:
0. 不知道叫什么
我们的
然后这个东西可以保证做到点数
1. endpos
顾名思义,就是结束位置了,对于一个串的每个子串,他都有一个
下面证明一些关于
- 引理
1 :如果s1 为s2 的后缀,|s1|\le|s2| ,那么edp(s2)\in edp(s1)
证明,这个是显然的,如果
- 引理
2 :对于两个edp 要么不交,要么包含。
证明,其实要证的就是如果相交,那么一定是包含关系,我们先假设
- 引理
3 :对于一个edp 等价类中的东西,最长的那个串到最短的串一定是连续一段的字符串。
证明,设最长的串为
2. parent\ tree
然后要么不交要么包含,这么个条件实际上就是类似于一个树形结构。
我们考虑按等价类来划分节点,记一个节点中长度最长的字符串为
那么只要在这个
反正都抄了那么多了,图也搬了
对于上面这张图,我们记绿色的这几个节点为终止节点,表示 最后 加入的字符所构成的后缀属于哪些节点,那么我们接下来新加字符就是在这些对应节点中的每个后缀增加一个字符,具体如何实现直接讲代码(这东西不看代码真的懂不了一点)。
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;
}
我们要往所有终止链上的点往他们的后缀上加一个点,记为
然后我们先考虑最简单的情况。
#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 上做
众所周知,
#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] 生成魔咒
动态维护本质不同的子串数量,显然用法
#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)
一个子串的出现次数其实就是他的
#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] 弦论
这个东西也不难搞。
对于询问本质不同的,直接在
对于询问可重的,我们求出每个子串的出现次数,然后也是直接走就好了。
#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;
}
Cyclical Quest
比较有意思的一个小应用,显然看到循环同构,可以想到破坏成链然后去匹配,但是这时候可能长度会超过
然后成功后,贡献就是对应节点的出现次数,不过循环同构可能会出现重复字符串,所以需要打个标记,表示这个字符串贡献过了。
#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] 封印
好题。
首先显然对于
然后对于某一个询问,相当于问
遇到这种问题,我们要么考虑钦定选择某一边,要么二分直接确定下界。
这里考虑二分,那么相当于询问
这个东西可以用
#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和后缀树的关系
虽然我不知道他们有什么关系,不过反正是一种简单应用。
首先由一个结论:两个串的
P4248 [AHOI2013] 差异
要我们求
然后就做完了。
#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
识别一些串的所有子串,每次做完之后将
JZPGYZ - Sevenk Love Oimaster
对于所有串建出
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
这个也不难,主要是询问涉及区间的时候可能就要用到这个。
和求子串出现次数是类似的,对于每个前缀
CF1037H Security
首先我们肯定是尽量匹配上
先考虑
显然就是找到最后一个可替换位置,然后就没什么好说的了。
考虑一般情况,倘若现在匹配了长度为
时间复杂度
#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] 字符串
首先要求
然后容易想到的是要二分答案,二分一个长度,然后我们考虑让
时间复杂度
#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
好题。
考虑重复,其实就是周期,那么根据
当存在两段相交区间时,红色部分就是周期。
所以实际上两个串的结尾位置相越少,周期越短,可重复次数越多,对于一个
然后取个
时间复杂度