舅舅孩子吧,一晚上了。

P3796 AC 自动机(简单版 II)

@[qinghua999](/user/482584) ```cpp #include <bits/stdc++.h> using namespace std; int n, trie[100005][27], ans[100005], tot, res, fail[100005], is_word[100005]; char str[160][10005]; string t; void insert(string str, int v) { int p = 0; for (int i = 0 ; i < str.size() ; i ++) { if (!trie[p][str[i] - 'a']) { trie[p][str[i] - 'a'] = ++ tot; } p = trie[p][str[i] - 'a']; } is_word[p] = v; } void getfail() { queue<int> que; for (int i = 0 ; i < 26 ; i ++) { if (trie[0][i]) { que.push(trie[0][i]); fail[trie[0][i]] = 0; } } while (!que.empty()) { int f = que.front(); que.pop(); for (int i = 0; i < 26; i ++) { if (trie[f][i]) { fail[trie[f][i]] = trie[fail[f]][i]; que.push(trie[f][i]); } else { trie[f][i] = trie[fail[f]][i]; } } } } void Find(string str) { int p = 0; for (int i = 0 ; i < str.size() ; i ++) { p = trie[p][str[i] - 'a']; for (int j = p; j ; j = fail[j]) { ans[is_word[j]] ++; } } } signed main() { while (1) { memset(is_word, 0, sizeof(is_word)); memset(ans, 0, sizeof(ans)); memset(trie, 0, sizeof(trie)); memset(fail, 0, sizeof(fail)); tot = 0; scanf("%d", &n); if (n == 0) { break; } for (int i = 1; i <= n; i ++) { scanf("%s", str[i]); insert(str[i], i); } getfail(); cin >> t; Find(t); int cnt = 0; for (int i = 1 ; i <= n ; i ++) { if (ans[i] > cnt) { cnt = ans[i]; } } printf("%d\n", cnt); for(int i = 1 ; i <= n ; i ++) { if (ans[i] == cnt) { printf("%s\n", str[i]); } } } return 0; } ```
by qfpjm @ 2021-09-24 21:35:56


主页双帖,危
by qfpjm @ 2021-09-24 21:36:38


|