题意:按照顺序输出在文本串中出现的次数最多的模式串.
水题,直接跑AC自动机,记录每一个模式串的次数就好了.
#include <bits/stdc++.h>
using namespace std;
#define maxn 511111
#define maxm 1111111
char str[155][77];
int n;
char a[maxm];
struct trie {
int next[maxn][26], fail[maxn], end[maxn];
int num[maxn];
int f[maxn];
int root, cnt;
int new_node () {
memset (next[cnt], -1, sizeof next[cnt]);
end[cnt++] = 0;
return cnt-1;
}
void init () {
cnt = 0;
root = new_node ();
memset (num, 0, sizeof num);
memset (f, -1, sizeof f);
}
void insert (char *buf, int pos) {
int len = strlen (buf);
int now = root;
for (int i = 0; i < len; i++) {
int id = buf[i]-'a';
if (next[now][id] == -1) {
next[now][id] = new_node ();
}
now = next[now][id];
}
end[now]++;
f[now] = pos;
}
void build () {
queue <int> q;
fail[root] = root;
for (int i = 0; i < 26; 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 < 26; 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 query (char *buf) {
int len = strlen (buf);
int now = root;
for (int i = 0; i < len; i++) {
int id = buf[i]-'a';
now = next[now][id];
int tmp = now;
while (tmp != root) {
if (end[tmp]) {
num[f[tmp]] += end[tmp];
}
tmp = fail[tmp];
}
}
int Max = 0;
for (int i = 1; i <= n; i++) {
Max = max (Max, num[i]);
}
printf ("%d\n", Max);
for (int i = 1; i <= n; i++) if (num[i] == Max) {
printf ("%s\n", str[i]);
}
return 0;
}
}ac;
int main () {
while (scanf ("%d", &n) == 1 && n) {
ac.init ();
for (int i = 1; i <= n; i++) {
scanf ("%s", str[i]);
ac.insert (str[i], i);
}
ac.build ();
scanf ("%s", a);
ac.query (a);
}
return 0;
}