此题是一个AC自动机,感觉是个裸题,WA了4发才过,只因为它告诉150个模板串,实际却有1000+
代码:
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int size = 26;
const int MAX = 1010 * 150 + 10;
char buf[1000][100];
char T[1000010];
struct Trie{
int next[MAX][size],fail[MAX],end[MAX];
int root, L;
int newnode(){
for(int i=0; i<size; i++)
next[L][i] = -1;
end[L++] = -1;
return L - 1;
}
void init(){
L = 0;
root = newnode();
memset(cnt, 0, sizeof(cnt));
}
void insert(char buf[], int id){
int len = strlen(buf), now = root;
for(int i=0; i<len; i++){
if(next[now][buf[i]-'a'] == -1)
next[now][buf[i]-'a'] = newnode();
now = next[now][buf[i]-'a'];
}
end[now] = id;
}
void build(){
queue <int> Q;
fail[root] = root;
for(int i=0; i<size; i++){
if(next[root][i] == -1) next[root][i] = root;
else{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
}
while(!Q.empty()){
int now = Q.front();
Q.pop();
for(int i=0; i<size; i++){
if(next[now][i] == -1) next[now][i] = next[fail[now]][i];
else{
fail[next[now][i]] = next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
}
int cnt[1010];
void query(char buf[]){
int len = strlen(buf), now = root;
for(int i=0; i<len; i++){
now = next[now][buf[i]-'a'];
int temp = now;
while(temp != root){
if(end[temp] != -1)
cnt[end[temp]]++;
temp = fail[temp];
}
}
}
}AC;
int main(){
int n;
while(scanf("%d",&n) == 1 && n){
AC.init();
for(int i=1; i<=n; i++){
scanf("%s",buf[i]);
AC.insert(buf[i],i);
}
AC.build();
scanf("%s",T);
AC.query(T);
int ans = 0;
for(int i=1; i<=n; i++)
if(ans < AC.cnt[i]) ans = AC.cnt[i];
printf("%d\n",ans);
for(int i=1; i<=n; i++)
if(AC.cnt[i] == ans)
puts(buf[i]);
}
return 0;
}