正文
增加 351 行删除 1 行
11123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351# AC自动机AC自动机是用于计算多个模式串在一个文本串里分别出现了几次的一种算法.AC自动机是在trie上通过失配指针$fail$(以下写作$f$)实现的.---### 建树用模式串建树就不说了.### 构建失配指针失配指针指向当前匹配失败时回到哪里继续匹配. 此时显然要找到在树上的非本身的最长后缀.这里设$i$为当前要找失配指针的节点, $p_i$为$i$的父节点, 此时$p_i$对应的字符串的最长后缀为$f_{p_i}$.只需要判断$f_{p_i}$是否有相同字符的儿子.若有, 则连一条失配指针, 否则失配指针指向根(因为此时没有后缀).放一张图(绿色表示失配指针)然而有反例, 因为根节点不存在在树上的非本身的最长后缀(空串没有非本身的后缀), 所以深度为$1$的串不能正确找到失配指针(根),而找到自己.所以进行预处理深度为$1$的串.同时**更深的点要比更浅的点更晚更新**.(记住这句话)所以用bfs.同时为了方便实现, 让父亲帮儿子找失配指针.考虑到在$i$失配时还要往$f_i$退再匹配, 所以从$i$连一条边到$f_i$的儿子.放一张图(绿色表示失配指针, 红色表示新连的边)### 查询如果走到了一个点就直接暴力跳失配指针统计答案.### 代码这里给出 @2huk 大蛇的[P3796](https://www.luogu.com.cn/problem/P3796)代码 orz```c++ line-numbers#include <bits/stdc++.h>using namespace std;const int N = 70 * 150 * 26 + 10;int n, res[N];string s[N], t;struct AC {int idx;int tr[N][26];int f[N];int st[N];void clear() {for (int i = 0; i <= idx; ++ i )for (int j = 0; j < 26; ++ j ) tr[i][j] = 0;idx = 0;memset(f, 0, sizeof f);memset(st, 0, sizeof st);}void modify(string s, int id) {int p = 0;for (char c : s) {int w = c - 'a';if (!tr[p][w]) tr[p][w] = ++ idx;p = tr[p][w];}st[p] = id;}void build() {queue<int> q;for (int i = 0; i < 26; ++ i )if (tr[0][i]) {q.push(tr[0][i]);f[tr[0][i]] = 0;}while (q.size()) {int u = q.front();q.pop();for (int i = 0; i < 26; ++ i ) {if (tr[u][i]) {f[tr[u][i]] = tr[f[u]][i];q.push(tr[u][i]);} else {tr[u][i] = tr[f[u]][i];}}}}void query(string t) {int p = 0;for (char c : t) {int w = c - 'a';p = tr[p][w];for (int q = p; q; q = f[q])res[st[q]] ++ ;}}}T;void solve() {T.clear();for (int _ = 1; _ <= n; ++ _ ) {cin >> s[_];T.modify(s[_], _);}memset(res, 0, sizeof res);cin >> t;T.build();T.query(t);vector<int> ans;int mx = -1;for (int i = 1; i <= n; ++ i ) {if (res[i] > mx) {mx = res[i];ans = {i};} else if (res[i] == mx) {ans.push_back(i);}}cout << mx << '\n';for (int v : ans) cout << s[v] << '\n';}signed main() {ios::sync_with_stdio(0);cin.tie(0), cout.tie(0);while (cin >> n, n) solve();return 0;}```---### 优化显然, 暴力跳失配指针会被类似一条链卡成$\text{O}\left(n^2\right)$, 所以打一个tag存储起来.最后跑一遍拓扑排序统一统计, 复杂度$\text{O}\left(n\right)$.### 代码依旧给出 @2huk 大蛇的[P5357](https://www.luogu.com.cn/problem/P5357)代码 orz```c++ line-numbers#include <bits/stdc++.h>using namespace std;const int N = 2e5 + 10;int n, res[N];string s[N], t;struct AC {int tr[N][26], idx, cnt[N];vector<int> id[N]; // 哪些模式串以这个点结尾int f[N];vector<int> g[N];int d[N], tag[N];void modify(string s, int i) {int p = 0;for (char _ : s) {int w = _ - 'a';if (!tr[p][w]) tr[p][w] = ++ idx;p = tr[p][w];}id[p].push_back(i);}void build() {queue<int> q;for (int i = 0; i < 26; ++ i )if (tr[0][i]) {f[tr[0][i]] = 0;q.push(tr[0][i]);}while (q.size()) {int u = q.front();q.pop();for (int i = 0; i < 26; ++ i ) {if (tr[u][i]) {f[tr[u][i]] = tr[f[u]][i];d[f[tr[u][i]]] ++ ;q.push(tr[u][i]);} else {tr[u][i] = tr[f[u]][i];}}}}void query(string t) {int p = 0;for (char _ : t) {int w = _ - 'a';p = tr[p][w];tag[p] ++ ;}}void topo() {queue<int> q;for (int i = 1; i <= idx; ++ i )if (!d[i]) q.push(i);while (q.size()) {int u = q.front();q.pop();int v = f[u];tag[v] += tag[u];if (! -- d[v]) q.push(v);}for (int i = 1; i <= idx; ++ i )for (int j : id[i]) res[j] += tag[i];}}T;signed main() {ios::sync_with_stdio(0);cin.tie(0), cout.tie(0);cin >> n;for (int i = 1; i <= n; ++ i ) {cin >> s[i];T.modify(s[i], i);}T.build();cin >> t;T.query(t);T.topo();for (int i = 1; i <= n; ++ i ) cout << res[i] << '\n';return 0;}```### 优化你没看错, 还能优化, 失配指针一定**从更深的点指向更浅的点**, 而在「构建失配指针」里, 我们提到了**更深的点要比更浅的点更晚更新**.所以将bfn翻转其实就是拓扑序. 常数显然比 queue 小.### 代码idea bye Gyf117 code by 2huk```c++ line-numbers#include <bits/stdc++.h>using namespace std;const int N = 2e5 + 10;int n, res[N];string s[N], t;struct AC {int tr[N][26], idx;int f[N];int d[N], tag[N];int q[N], l = 1, r;vector<int> id[N];void modify(string s, int i) {int p = 0;for (char _ : s) {int w = _ - 'a';if (!tr[p][w]) tr[p][w] = ++ idx;p = tr[p][w];}id[p].push_back(i);}void build() {for (int i = 0; i < 26; ++ i )if (tr[0][i]) {q[ ++ r] = tr[0][i];}while (l <= r) {int u = q[l ++ ];for (int i = 0; i < 26; ++ i ) {if (tr[u][i]) {f[tr[u][i]] = tr[f[u]][i];q[ ++ r] = tr[u][i];} else {tr[u][i] = tr[f[u]][i];}}}}void query(string t) {int p = 0;for (char _ : t) {int w = _ - 'a';p = tr[p][w];tag[p] ++ ;}}void work() {for (int i = r; i; -- i ) {int x = q[i], y = f[x];tag[y] += tag[x];}for (int i = 1; i <= idx; ++ i )for (int j : id[i]) res[j] += tag[i];}} T;signed main() {ios::sync_with_stdio(0);cin.tie(0), cout.tie(0);cin >> n;for (int i = 1; i <= n; ++ i ) {cin >> s[i];T.modify(s[i], i);}T.build();cin >> t;T.query(t);T.work();for (int i = 1; i <= n; ++ i ) cout << res[i] << '\n';return 0;}```