1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| #include<bits/stdc++.h> using namespace std; const int maxn = 155 * 75; const int cc = 26; #define ms(a,b) memset(a,b,sizeof(a)) struct AC{ int ch[maxn][cc], fail[maxn]; int idx[maxn], val[maxn], cnt[maxn]; int tot; void init() { ms(ch, 0), ms(fail, 0); ms(idx, 0), ms(val, 0), ms(cnt, 0); tot = 0; } void insert(char *s,int id) { int u = 0; for (int i = 0; s[i]; i++) { int v = s[i]-'a'; if(!ch[u][v]) { ch[u][v] = ++tot; } u = ch[u][v]; } idx[id] = u; }
queue<int> q; void build(){ for (int i = 0; i < 26; i++) { if (ch[0][i]) q.push(ch[0][i]); } while (q.size()) { int u = q.front(); q.pop(); for (int v = 0; v < 26;v++) { if(ch[u][v]) { fail[ch[u][v]] = ch[fail[u]][v]; q.push(ch[u][v]); } else ch[u][v] = ch[fail[u]][v]; } } }
int query(char *t) { int u = 0, res = 0; for (int i = 0; t[i];i++) { int v = t[i] - 'a'; u = ch[u][v]; for (int j = u; j;j = fail[j]) { val[j]++; } } for (int i = 0; i <= tot; i++) { if (idx[i]) { res = max(res, val[i]); cnt[idx[i]] = val[i]; } } return res; } } ac; char s[200][100]; char t[1000005]; int main() { int n; while(~scanf("%d", &n)&&n!=0) { ac.init(); for (int i = 1; i <= n;i++) { scanf("%s", s[i]); ac.insert(s[i], i); } ac.build(); scanf("%s", t); int mx = ac.query(t); printf("%d\n", mx); for(int i=1;i<=n;i++) { if(ac.cnt[i]==mx) { printf("%s\n", s[i]); } } } }
|