题面
题意:n个串,问每个串有多少个子串在至少k个串中出现
建出广义sam,对于每个状态
求出parent树上的子树有多少个来自不同串的节点
大于k的可以贡献答案
具体实现就是乱搞
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define mmst(a, b) memset(a, b, sizeof(a))
#define mmcp(a, b) memcpy(a, b, sizeof(b))
typedef long long LL;
const int N=400400;
int son[N][26],pre[N],dep[N],cnt=1,w[N],last;
int n,k;
int head[N],nex[N],to[N],cur;
int l[N],r[N],siz[N],a[N],f[N],val[N],by[N],times;
int st[N];
int bit[N],all;
char cc[N];
void update(int x,int ad)
{
for(;x<=cnt+10;x+=x&-x)
bit[x]+=ad;
all+=ad;
}
int sum(int x)
{
int res=0;
for(;x;x-=x&-x)
res+=bit[x];
return res;
}
void add(int u,int v)
{
to[++cur]=v;
nex[cur]=head[u];
head[u]=cur;
}
void dfs(int x)
{
l[x]=++times;
if(a[w[x]])
f[a[w[x]]]=times;
if(w[x])
a[w[x]]=times;
by[times]=w[x];
for(int h=head[x];h;h=nex[h])
dfs(to[h]);
r[x]=times;
}
void dfs2(int x)
{
siz[x]-=(all-sum(r[x]));
if(f[l[x]])
update(f[l[x]],1);
for(int h=head[x];h;h=nex[h])
dfs2(to[h]);
siz[x]+=(all-sum(r[x]));
}
void dfs3(int x)
{
if(siz[x]>=k)
val[x]+=dep[x]-dep[pre[x]];
for(int h=head[x];h;h=nex[h])
{
val[to[h]]+=val[x];
dfs3(to[h]);
}
}
void Insert(int x,int ops)
{
dep[++cnt]=dep[last]+1;
int np=cnt,p=last;
last=np;
w[np]=ops;
for(;!son[p][x];p=pre[p])
son[p][x]=np;
if(!p)
pre[np]=1;
else
{
int q=son[p][x];
if(dep[q]==dep[p]+1)
pre[np]=q;
else
{
dep[++cnt]=dep[p]+1;
int nq=cnt;
pre[nq]=pre[q];
pre[q]=pre[np]=nq;
mmcp(son[nq],son[q]);
for(;son[p][x]==q;p=pre[p])
son[p][x]=nq;
}
}
}
int main()
{
cin>>n>>k;
st[1]=1;
for(int i=1;i<=n;i++)
{
last=1;
scanf("%s",cc+st[i]);
st[i+1]=strlen(cc+1)+1;
for(int j=st[i];j<st[i+1];j++)
Insert(cc[j]-'a',i);
}
for(int i=2;i<=cnt;i++)
add(pre[i],i);
dfs(1);
for(int i=1;i<=n;i++)
if(a[i])
f[a[i]]=cnt+1;
dfs2(1);
dfs3(1);
for(int i=1;i<=n;i++)
{
int now=1;
LL ans=0;
for(int j=st[i];j<st[i+1];j++)
{
now=son[now][cc[j]-'a'];
ans+=val[now];
}
printf("%d ",ans);
}
cout<<endl;
return 0;
}