题解
2023-08-25 14:33:27
发布于:广东
3阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
typedef int LL;
const LL maxn=200009;
LL n,m,nod=1,lst;
LL len[maxn],fail[maxn],son[maxn][26],visit[maxn],ans[maxn],mark[maxn];
string s;
inline void Insert(LL c,LL id){
LL np=++nod,p=lst; lst=np; mark[np]=id;
len[np]=len[p]+1;
while(p&&!son[p][c]){
son[p][c]=np;
p=fail[p];
}
if(!p)
fail[np]=1;
else{
LL q=son[p][c];
if(len[q]==len[p]+1)
fail[np]=q;
else{
LL nq=++nod; len[nq]=len[p]+1; mark[nq]=mark[q];
memcpy(son[nq],son[q],sizeof(son[q]));
fail[nq]=fail[q];
fail[np]=fail[q]=nq;
while(p&&son[p][c]==q){
son[p][c]=nq;
p=fail[p];
}
}
}
}
struct node{
LL to,next;
}dis[maxn];
LL num,head[maxn];
inline void Add(LL u,LL v){
dis[++num]=(node){v,head[u]},head[u]=num;
}
void Dfs(LL u){
for(LL i=head[u];i;i=dis[i].next)
Dfs(dis[i].to);
for(LL i=head[u];i;i=dis[i].next){
LL v(dis[i].to);
if(mark[v]==-1||mark[u]!=mark[v]){
mark[u]=-1;
break;
}
}
}
int main(){
scanf("%d%d",&n);
for(LL i=1;i<=n;++i){
cin>>s;
lst=1;
for(LL j=0,Len=s.length();j<Len;++j)
Insert(s[j]-'a',i);
}
for(LL i=1;i<=nod;++i)
Add(fail[i],i);
Dfs(1);
for(LL i=1;i<=nod;++i)
if(mark[i]!=-1)
ans[mark[i]]+=len[i]-len[fail[i]];
for(LL i=1;i<=n;++i)
cout<<ans[i]<<'\n';
return 0;
}
这里空空如也
有帮助,赞一个