usingnamespacestd; usingnamespace FAST_IO; const ll mod = 1e9 + 7; constint INF = 0x3f3f3f3f; const ll INF_LL = 0x3f3f3f3f3f3f3f3f; constdouble eps = 1e-5; constint maxn = 1e6 + 10; constint maxm = 7e1 + 10; int t, n, m, k; structACTrie { conststaticint M = 26; conststaticint maxn = 1e6 + 10; int next[maxn][M] = {}, fail[maxn] = {}, cnt = 0; int exist[maxn] = {}; int id[maxn]; // 以u结尾的字符串对应的输入id int times[maxn] = {}; queue<int> q;
voidinit() { for (int i = 0; i < maxn; ++i) { for (int j = 0; j < M; ++j) { next[i][j] = 0; } fail[i] = 0; exist[i] = 0; id[i] = -1; times[i] = 0; } cnt = 0; } voidinsert(char *s, int index) { int now = 0, len = strlen(s); for (int i = 0; i < len; ++i) { int c = s[i] - 'a'; if (!next[now][c]) next[now][c] = ++cnt; now = next[now][c]; } exist[now]++; id[now] = index; }
voidbuild() {
for (int i = 0; i < M; ++i) { if (next[0][i]) { fail[next[0][i]] = 0; q.push(next[0][i]); } } while (q.size()) { int u = q.front(); q.pop(); for (int i = 0; i < M; ++i) { if (next[u][i]) { fail[next[u][i]] = next[fail[u]][i]; q.push(next[u][i]); } else next[u][i] = next[fail[u]][i]; } } }
intquery(char *s) { int now = 0, len = strlen(s), ans = 0; for (int i = 0; i < len; ++i) { int c = s[i] - 'a'; now = next[now][c]; for (int j = now; j; j = fail[j]) { if (id[j] != -1) { times[id[j]]++; ans = max(ans, times[id[j]]); } } } return ans; } }; char a[maxn][maxm]; char s[maxn]; intmain() { // #define COMP_DATA #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(0); ACTrie trie; while (cin >> n && n) { trie.init(); for (int i = 0; i < n; ++i) { cin >> a[i]; trie.insert(a[i], i); } cin >> s; trie.build(); int ans = trie.query(s); cout << ans << endl; for (int i = 0; i < n; ++i) { if (trie.times[i] == ans) { cout << a[i] << endl; } } } return0; }