Problem
给定一个S串,有m组询问,每次给定一个T串,询问T串有多少个本质不同的子串没有在S串的区间 [ L , R ] [L,R] [L,R]中出现过。
Solution
看到涉及到子串的题我们就得把S串的SAM乖乖建好,看到本质不同,我们就还得把T串的SAM乖乖建好。
从统计本质不同子串的原理出发,我们只需要减去不合法的子串即可。一个比较显然的想法是,我们用T串去匹配
[
L
,
R
]
[L,R]
[L,R]这个区间所代表的SAM,得到T中长度为
i
i
i的前缀,其后缀最多可以匹配多长,记作
m
x
[
i
]
mx[i]
mx[i]。则不难魔改出得到这样的一个ans的表达式
a
n
s
=
∑
x
∈
S
A
M
T
max
(
0
,
l
[
x
]
−
max
(
l
[
f
a
]
,
m
x
[
p
o
s
[
x
]
]
)
)
ans=\sum_{x\in SAM_T} \max(0,l[x]-\max(l[fa],mx[pos[x]]))
ans=x∈SAMT∑max(0,l[x]−max(l[fa],mx[pos[x]]))
搞一次线段树合并即可得到查询区间内是否出现过子串。
在匹配时注意一点,匹配的左端点不能超过区间,所以线段树中查询的左端点有变化,而正因此,匹配不能与平常一样失配后就直接跳至父亲节点,而应该把当前匹配长度减小,再次匹配。虽然看起来很暴力,但是势能分析是没有问题的。
时间复杂度 O ( ( ∣ S ∣ + ∑ ∣ T ∣ ) log ∣ S ∣ ) O((|S|+\sum|T|)\log |S|) O((∣S∣+∑∣T∣)log∣S∣)。
Code
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
const int maxn=1000010,maxm=40000010;
template <typename Tp> inline int getmin(Tp &x,Tp y){return y<x?x=y,1:0;}
template <typename Tp> inline int getmax(Tp &x,Tp y){return y>x?x=y,1:0;}
template <typename Tp> inline void read(Tp &x)
{
x=0;int f=0;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') f=1,ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
if(f) x=-x;
}
int n,m,p,tot,mx[maxn],rt[maxn],lc[maxm],rc[maxm],cnt[maxm];
char s[maxn];
struct SAM{
int lst,sz,ch[maxn][26],pre[maxn],l[maxn],pos[maxn],t[maxn],tp[maxn];
SAM(){lst=sz=1;}
void clear(int x){l[x]=pre[x]=pos[x]=0;memset(ch[x],0,sizeof(ch[x]));}
void clear(){for(int i=1;i<=sz;i++) clear(i);lst=sz=1;}
void insert(int c)
{
int p=lst,np=++sz;l[np]=l[lst]+1;lst=np;
for(;p&&!ch[p][c];p=pre[p]) ch[p][c]=np;
if(!p) pre[np]=1;
else
{
int q=ch[p][c];
if(l[q]==l[p]+1) pre[np]=q;
else
{
int nq=++sz;l[nq]=l[p]+1;pos[nq]=pos[q];
memmove(ch[nq],ch[q],sizeof(ch[q]));
pre[nq]=pre[q];pre[q]=pre[np]=nq;
for(;ch[p][c]==q;p=pre[p]) ch[p][c]=nq;
}
}
}
void top()
{
for(int i=1;i<=sz;i++) ++t[l[i]];
for(int i=1;i<=sz;i++) t[i]+=t[i-1];
for(int i=1;i<=sz;i++) tp[t[l[i]]--]=i;
}
}S,T;
void update(int l,int r,int pos,int &rt)
{
if(!rt) rt=++tot;
++cnt[rt];
if(l==r) return ;
int m=(l+r)>>1;
if(pos<=m) update(l,m,pos,lc[rt]);
else update(m+1,r,pos,rc[rt]);
}
int merge(int x,int y)
{
if(!x||!y) return x+y;
int nx=++tot;cnt[nx]=cnt[x]+cnt[y];
lc[nx]=merge(lc[x],lc[y]);
rc[nx]=merge(rc[x],rc[y]);
return nx;
}
int query(int l,int r,int L,int R,int rt)
{
if(!rt) return 0;
if(L<=l&&r<=R) return cnt[rt];
int m=(l+r)>>1,res=0;
if(L<=m) res=query(l,m,L,R,lc[rt]);
if(res) return res;
if(m<R) res=query(m+1,r,L,R,rc[rt]);
return res;
}
void input()
{
scanf("%s",s+1);
n=strlen(s+1);
for(int i=1;i<=n;i++)
{
S.insert(s[i]-'a');
update(1,n,i,rt[S.lst]);
}
read(m);S.top();
for(int i=S.sz,j;i>1;i--)
{
j=S.tp[i];
rt[S.pre[j]]=merge(rt[S.pre[j]],rt[j]);
}
}
void work()
{
int L,R,m,c,now=1,len=0;ll ans=0ll;
scanf("%s",s+1);read(L);read(R);
for(m=1;s[m];m++){T.insert(s[m]-'a');T.pos[T.lst]=m;}
for(int i=1;i<m;i++)
{
while(1)
{
c=S.ch[now][s[i]-'a'];
if(L+len-1>R||!query(1,n,L+len,R,rt[c]))
{
if(!len) break;
--len;
if(len==S.l[S.pre[now]]) now=S.pre[now];
}
else{now=c;++len;break;}
}
mx[i]=len;
}
for(int i=2;i<=T.sz;i++) ans+=max(0,T.l[i]-max(T.l[T.pre[i]],mx[T.pos[i]]));
T.clear();
printf("%lld\n",ans);
}
int main()
{
input();
while(m--) work();
return 0;
}