广义后缀自动机模板题
注意是求在多少个串出现
于是要记一个表示是否被当前串更新过
可以每加入一个字符就暴力跳更新
但这样在新建节点的时候和也要传递
也可以所有串加完之后再遍历自动机更新
#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ob==ib)?EOF:*ib++;
}
#define gc getchar
inline int read(){
char ch=gc();
int res=0,f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
#define ll long long
#define re register
#define pii pair<int,int>
#define pic pair<int,char>
#define fi first
#define se second
#define pb push_back
#define cs const
#define bg begin
#define poly vector<int>
template<class T>inline void chemx(T &a,T b){a<b?a=b:0;}
template<class T>inline void chemn(T &a,T b){a>b?a=b:0;}
namespace Sam{
cs int N=200005;
int fa[N],vis[N],tim,nxt[N][26],len[N],tot,last,siz[N];
inline void init(){
tot=1;
}
inline void reset(){
last=1,tim++;
}
inline void insert(int c){
int cur=++tot,p=last;last=cur;
len[cur]=len[p]+1;
for(;p&&!nxt[p][c];p=fa[p])nxt[p][c]=cur;
if(!p)fa[cur]=1;
else{
int q=nxt[p][c];
if(len[p]+1==len[q])fa[cur]=q;
else{
int clo=++tot;
len[clo]=len[p]+1,fa[clo]=fa[q];
memcpy(nxt[clo],nxt[q],sizeof(nxt[q]));
for(;p&&nxt[p][c]==q;p=fa[p])nxt[p][c]=clo;
vis[clo]=vis[q],siz[clo]=siz[q],fa[cur]=fa[q]=clo;
}
}
for(int u=cur;u;u=fa[u])
if(vis[u]!=tim)vis[u]=tim,siz[u]++;
}
inline int query(char *s){
int p=1;
for(int i=1,len=strlen(s+1);i<=len;i++){
int c=s[i]-'a';
if(!nxt[p][c])return 0;
p=nxt[p][c];
}
return siz[p];
}
}
cs int N=360005;
int n,m;
char s[N];
int main(){
#ifdef Stargazer
freopen("lx.cpp","r",stdin);
#endif
Sam::init();
n=read(),m=read();
for(int i=1;i<=n;i++){
Sam::reset();
scanf("%s",s+1);
for(int j=1,len=strlen(s+1);j<=len;j++){
Sam::insert(s[j]-'a');
}
}
for(int i=1;i<=m;i++){
scanf("%s",s+1);
cout<<Sam::query(s)<<'
';
}
}