题面
https://www.luogu.org/problem/SP8093
题解
#include<cstdio> #include<iostream> #include<cstring> #define ri register int #define N 100050 #define M 360050 using namespace std; int n,m; string s[M]; char y[M]; int vis[M<<1]; int siz[M<<1]; struct SAM{ int len[M<<1],ff[M<<1]; int ch[M<<1][26]; int las,tot; void init() { las=tot=1; } void extend(int c) { int np=++tot,p=las; las=np; len[np]=len[p]+1; while (p && !ch[p][c]) ch[p][c]=np,p=ff[p]; if (!p) ff[np]=1; else { int q=ch[p][c]; if (len[q]==len[p]+1) ff[np]=q; else { int nq=++tot; for (ri i=0;i<26;i++) ch[nq][i]=ch[q][i]; ff[nq]=ff[q]; len[nq]=len[p]+1; ff[q]=ff[np]=nq; while (p && ch[p][c]==q) ch[p][c]=nq,p=ff[p]; } } } } sam; int main(){ cin>>n>>m; sam.init(); for (ri i=1;i<=n;i++) { sam.las=1; cin>>s[i]; int n=s[i].size(); for (ri j=0;j<n;j++) sam.extend(s[i][j]-'a'); } for (ri i=1;i<=n;i++) { int n=s[i].size(); int now=1; for (ri j=0;j<n;j++) { now=sam.ch[now][s[i][j]-'a']; int tn=now; while (tn && vis[tn]!=i) vis[tn]=i,siz[tn]++,tn=sam.ff[tn]; } } for (ri i=1;i<=m;i++) { scanf("%s",y+1); int n=strlen(y+1); int now=1; for (ri j=1;j<=n;j++) { now=sam.ch[now][y[j]-'a']; } printf("%d ",siz[now]); } return 0; }