题面
https://www.luogu.org/problem/P3796
题解
#include<bits/stdc++.h> #define N 1000050 using namespace std; queue<int> q; int ans[200]; int oto[N]; struct aczidongji { int c[N][26],val[N],fail[N],cnt; void clear(){ memset(c,0,sizeof(c)); cnt=0; memset(val,0,sizeof(val)); memset(fail,0,sizeof(fail)); } void insert(char *s,int id) { int len=strlen(s); int now=0; for (int i=0;i<len;i++) { int v=s[i]-'a'; if (!c[now][v]) c[now][v]=++cnt; now=c[now][v]; } val[now]++; oto[now]=id; } void build() { for (int i=0;i<26;i++) if (c[0][i]) fail[c[0][i]]=0,q.push(c[0][i]); while (!q.empty()) { int u=q.front(); q.pop(); for (int i=0;i<26;i++) if (c[u][i]) fail[c[u][i]]=c[fail[u]][i],q.push(c[u][i]); else c[u][i]=c[fail[u]][i]; } } void query(char *s){ int len=strlen(s); int now=0; for (int i=0;i<len;i++) { now=c[now][s[i]-'a']; for (int t=now;t;t=fail[t]) ans[oto[t]]++; } } } AC; int n; char pp[200][100]; char p[1000005]; int main(){ while (true){ memset(ans,0,sizeof(ans)); memset(oto,0,sizeof(oto)); AC.clear(); scanf("%d",&n); if (n==0) return 0; for (int i=1;i<=n;i++) scanf("%s",pp[i]),AC.insert(pp[i],i); AC.build(); scanf("%s",p); AC.query(p); int maxd=0; for (int i=1;i<=n;i++) if (ans[i]>maxd) maxd=ans[i]; cout<<maxd<<endl; for (int i=1;i<=n;i++) if (ans[i]==maxd) { int ll=strlen(pp[i]); for (int j=0;j<ll;j++) printf("%c",pp[i][j]); puts(""); } } return 0; }