版本比较
浅谈 AC 自动机 / ACAM
正文
增加 343 行删除 7 行
18 行未改动192021222323242526277 行未改动3536373839394041424323 行未改动67686970717172737475132 行未改动全部展开208209210211212212213214215216167 行未改动全部展开38438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352438852552652752852986 行未改动61661761861948362062162262362486 行未改动711712713714578715716717718719127 行未改动全部展开8478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104918 行未改动AC 自动机可以解决一类**多模匹配**问题。
AC 自动机求解多模匹配问题的思想为:
>把模式串插入到 Trie 中,通过构建类似 KMP 的失配指针($fali$ 指针),然后扫描文本串在 AC 自动机上匹配得到结果。>把模式串插入到 Trie 中,通过构建类似 KMP 的失配指针($fail$ 指针),然后扫描文本串在 AC 自动机上匹配得到结果。
### 1 如何构建 AC 自动机
#### 1.1 构建字典树7 行未改动#### 1.2 如何求解 $fail$ 指针
和 KMP 中的失配指针相比,AC 自动机的失配指针 $fail_u$ 指向的为 Trie 树上的一个状态 $v$,满足 $v$ 是 $u$ 的最长后缀,也就是 $u$ 在 Trie 树上的后缀状态中最长的那个。
考虑当前我们需要求解的位置为 $u$,父亲为 $fa$,有一条边 $(fa, c)$ 指向 u$$,其中 $c$ 是字符集中的字符,并且满足深度 $< u$ 的节点已经求解完成。考虑当前我们需要求解的位置为 $u$,父亲为 $fa$,有一条边 $(fa, c)$ 指向 $u$,其中 $c$ 是字符集中的字符,并且满足深度 $< u$ 的节点已经求解完成。
首先,因为状态 $u$ 相当于状态 $fa$ 加上了一个字符 $c$,所以 $u$ 的后缀都相当于:状态 $fa$ 的后缀加上字符 $c$。
由于我们需要求的是最长后缀,那么最先尝试的肯定是 $(fail_{fa}, c)$,接下来考虑 $(fail_{fa}, c)$ 是否存在。23 行未改动$$(fa, c) = (fail_{fa}, c)$$
对于状态 $(fail_{fa}, c)$,它如果缺失转移,会被压缩为第一个存在的 $(fail^k(fail_{fa}), c)$,那么,这样压缩后,$(fa, c)$ 相当于第一个存在的 $(fail^{k + 1}(fail_fa), c)$,于是我们成功完成了一层转移。对于状态 $(fail_{fa}, c)$,它如果缺失转移,会被压缩为第一个存在的 $(fail^k(fail_{fa}), c)$,那么,这样压缩后,$(fa, c)$ 相当于第一个存在的 $(fail^{k + 1}(fail_{fa}), c)$,于是我们成功完成了一层转移。
::::success[构建 AC 自动机代码]```cpp//字典树插入132 行未改动全部展开我们发现直接套用上一题会 TLE,这里介绍一种**拓扑排序优化**。
我们发现,AC 自动机复杂度瓶颈在于匹配时跳 $fail$。
因为 $fail$ 显然不会有环,并且深度一定越来越低,所以我们如果只保留 AC 自动机中的 $fail$ 边,会形成一棵树。因为 $fail$ 显然不会有环,并且深度一定越来越低,所以我们如果只保留 AC 自动机中的 $fail$ 边,会形成一棵树,这棵树称为 $fail$ 树。
那么,我可以考虑,预先记录要跳那些,然后最后求和,即可优化效率。
那么,我们考虑按照 $fail$ 树,做一次拓扑排序,一次性求出匹配次数。167 行未改动全部展开}```::::
#### 3.2 [P14363 [CSP-S 2025] 谐音替换 / replacee](https://www.luogu.com.cn/problem/P14363)
>自己去原题面看()
我们设当前 $t1, t2$ 的最长公共前缀为 $L$,最长公共后缀为 $R$。
那么替换一定需要覆盖 $L, R$ 中间的那块区域,还可能覆盖 $L, R$,但是一定会与原来相同。
也就是一定形如:$\texttt{LAR}\to \texttt{LBR}$。
那么我们考虑把 $t1, t2$ 合并成一个这样的字符串:$\texttt{L?A?B?R}$。
对于每个 $s1, s2$ 也合并成这样的形式。
那么,我们发现,合法的替换中一定会满足 $s1, s2$ 合并成的字符串是 $t1, t2$ 合并成的字符串的子串。
这时候就变成了一个多模匹配问题,使用 AC 自动机解决。
注意需要特殊处理 $s1, s2$ 相等和 $t1, t2$ 相等,需要预处理 $fail$ 链的和。
::::success[完整代码]```cpp#include <bits/stdc++.h>using namespace std;const int MAXN = 5e6 + 10;int ch[MAXN][30];int pos[MAXN];int fail[MAXN];int n, q, tot = 1;int cnt[2][MAXN];int get(char c) { if (c >= 'a' && c <= 'z') return c - 'a'; else if (c == '&') return 26; else if (c == '^') return 27; else return 28; }void insert(int type, string s, int P) { int u = 1; for (int i = 0; i < s.size(); i++) { int c = get(s[i]); if (ch[u][c] == 0) ch[u][c] = ++tot; u = ch[u][c]; } pos[u] = P; cnt[type][u]++;}void get_fail() { for (int i = 0; i < 29; i++) ch[0][i] = 1; queue <int> q; q.push(1); while (!q.empty()) { int u = q.front(); q.pop(); cnt[0][u] += cnt[0][fail[u]]; cnt[1][u] += cnt[1][fail[u]]; for (int i = 0; i < 29; i++) { int v = ch[u][i]; if (v == 0) { ch[u][i] = ch[fail[u]][i]; continue; } fail[v] = ch[fail[u]][i]; q.push(v); } }}int query(int type, string s) { int u = 1; int ans = 0; for (int i = 0; i < s.size(); i++) { int c = get(s[i]); u = ch[u][c]; ans += cnt[type][u]; } return ans;}string get(string a, string b) { if (a == b) return a; int n = a.size(); int L = 0, R = 0; for (int i = 0; i < n; i++) { if (a[i] != b[i]) { L = i; break; } } for (int i = n - 1; i >= 0; i--) { if (a[i] != b[i]) { R = i; break; } } string res = ""; for (int i = 0; i < L; i++) res += a[i]; res += '&'; for (int i = L; i <= R; i++) res += a[i]; res += '^'; for (int i = L; i <= R; i++) res += b[i]; res += '|'; for (int i = R + 1; i < n; i++) res += b[i];// cout << res << '\n'; return res;}signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> q; for (int i = 1; i <= n; i++) { string s1, s2; cin >> s1 >> s2; if (s1 == s2) { insert(1, s1, i); continue; } string qwq = get(s1, s2); insert(0, qwq, i); } get_fail(); for (int i = 1; i <= q; i++) { string t1, t2; cin >> t1 >> t2; if (t1.size() != t2.size()) { cout << "0\n"; continue; } if (t1 == t2) { cout << query(1, t1) << '\n'; continue; } string awa = get(t1, t2); cout << query(0, awa) << '\n'; } return 0;}```::::
#### 3.2 [P4052 [JSOI2007] 文本生成器](https://www.luogu.com.cn/problem/P4052)#### 3.3 [P4052 [JSOI2007] 文本生成器](https://www.luogu.com.cn/problem/P4052)>给定 $n$ 个模式串 $s_1\sim s_n$,求出长度为 $m$,且至少包含一个模式串的文本串个数。>>$1\le n\le 60, 1\le m\le 100, 1\le |s_i|\le 100$
86 行未改动}```::::
#### 3.3 [P2292 [HNOI2004] L 语言](https://www.luogu.com.cn/problem/P2292)#### 3.4 [P2292 [HNOI2004] L 语言](https://www.luogu.com.cn/problem/P2292)
>一段文章 $T$ 是由若干小写字母构成。一个单词 $W$ 也是由若干小写字母构成。一个字典 $D$ 是若干个单词的集合。我们称一段文章 $T$ 在某个字典 $D$ 下是可以被理解的,是指如果文章 $T$ 可以被分成若干部分,且每一个部分都是字典 $D$ 中的单词。>>给定一个字典 $D$,其中有 $n$ 个单词 $s_1\sim s_n$,你的程序需要判断 $m$ 个文章 $t_1\sim t_m$ 在字典 $D$ 下是否能够被理解。并给出其在字典 $D$ 下能够被理解的最长前缀的位置。86 行未改动}```::::
#### 3.4 [P2414 [NOI2011] 阿狸的打字机](https://www.luogu.com.cn/problem/P2414)#### 3.5 [P2414 [NOI2011] 阿狸的打字机](https://www.luogu.com.cn/problem/P2414)
>有一个操作序列,有三种操作,且当前有一个字符串:>- 输入一个小写字母,假如在字符串最后。>- 输入一个 `B`,删除字符串最后一个字符。127 行未改动全部展开 return 0;}```::::
#### 3.6 [P2336 [SCOI2012] 喵星球上的点名](https://www.luogu.com.cn/problem/P2336)
>有 $n$ 只喵,每一只有姓和名两个字符串,另外有 $m$ 个点名串。>>你需要求出对于每个点名串,有多少只喵的姓或名中包含这个串(每只喵只算一次);对每只喵,求出其被多少个点名串点过(只要点名串出现在它的姓或名里就算一次)。>>$1\le n\le 5\times 10^4, 1\le m\le 10^5$,名字总长和点名串的总长分别不超过 $10^5$,字符集大小不超过 $10^4$。
~~喵喵喵~~
考虑把姓和名合并成一个字符串,在中间加上一个特殊字符,这样只要是点名串的子串,即可匹配成功。
根据上一题的结论,包含一个串的显然是 $fail$ 树上这个串结尾的状态的子树中。
>以下中的“树”都是指 $fail$ 树。
首先考虑第一问,对于每个名字串,从尾开始,沿着在字典树的状态往上跳,对于覆盖了这些点的子树,都需要统计。
由于子树的 dfs 序是连续的,所以可以使用树状数组维护每个点被覆盖的次数。
但是我们发现,这样显然会算重。
这里有一个 trick,我们把覆盖到的每个点,按 dfs 序从小到大排序,然后在相邻两个点的 $\operatorname{LCA}$ 处减 $1$,即可算出正确的覆盖到的点。
::::info[证明]子树 $\text{subtree}(u)$ 在 DFS 序中对应连续区间 $[dfn_u, dfn_u + siz_u - 1]$。因此 $S$ 中落入该区间的节点形成连续子序列:
$$S_u = \{ v_l, v_{l + 1}, \dots, v_r \}, \quad r \ge l$$
子树内每个节点贡献 $+ 1$,共 $r - l + 1$ 个。相邻 $\operatorname{LCA}$ 的 $- 1$ 贡献中:- 内部相邻对 $(v_l, v_{l + 1}), \dots, (v_{r - 1}, v_r)$ 共 $r - l$ 个,其 $\operatorname{LCA}$ 均在 $\text{subtree}(u)$ 内;- 边界相邻对 $(v_{l - 1}, v_l)$ 和 $(v_r, v_{r + 1})$ 均有一个节点在子树外,其 $\operatorname{LCA}$ 不在 $\text{subtree}(u)$ 内。
因此子树内总和为:
$$(r - l + 1) - (r - l) = 1$$
证毕。::::
对于第二问,和上面类似,我们对于每个点名串,树状数组加差分求出每个点被覆盖多少次。
然后枚举名字串,看从尾开始,沿着在字典树的状态往上跳,跳到了那些点。然后跳到的点依旧按 dfs 序排序,和上面一样差分,对于相邻两个点,减去 $\operatorname{LCA}$ 的答案。
注意由于字符集太大,不能直接建立字典图,而是直接暴力跳 $fail$。
::::success[完整代码]```cpp#include <bits/stdc++.h>using namespace std;const int MAXN = 3e5 + 10;map <int, int> ch[MAXN];int fail[MAXN];int n, m, tot = 1;vector <int> G[MAXN];int siz[MAXN], dfn[MAXN], cntt, dep[MAXN], son[MAXN], top[MAXN], fa[MAXN];int fat[MAXN];int Pos1[MAXN], Pos2[MAXN];int c[MAXN];int lowbit(int x) { return x & (-x);}void add(int x, int y) { for (int i = x; i <= cntt; i += lowbit(i)) c[i] += y; }int sum(int x) { int res = 0; for (int i = x; i; i -= lowbit(i)) res += c[i]; return res;}int insert(vector <int> s) { int u = 1; for (auto c : s) { if (ch[u].find(c) == ch[u].end()) ch[u][c] = ++tot, fat[ch[u][c]] = u; u = ch[u][c]; } return u;}void dfs1(int u, int f) { fa[u] = f; dep[u] = dep[f] + 1; siz[u] = 1; for (auto v : G[u]) { if (v == f) continue; dfs1(v, u); siz[u] += siz[v]; if (siz[son[u]] < siz[v]) son[u] = v; }}void dfs2(int u, int Top) { top[u] = Top; dfn[u] = ++cntt; if (son[u]) dfs2(son[u], Top); for (auto v : G[u]) { if (v == fa[u] || v == son[u]) continue; dfs2(v, v); }}int lca(int x, int y) { while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) swap(x, y); x = fa[top[x]]; } return dep[x] < dep[y] ? x : y;}void get_fail() { queue <int> q; fail[1] = 0; G[0].push_back(1); for (auto qwq : ch[1]) { int v = qwq.second; fail[v] = 1; q.push(v); } while (!q.empty()) { int u = q.front(); q.pop(); G[fail[u]].push_back(u); for (auto qwq : ch[u]) { int c = qwq.first, v = qwq.second; int f = fail[u]; while (f && f != 1 && ch[f].find(c) == ch[f].end()) f = fail[f]; if (f && ch[f].find(c) != ch[f].end()) fail[v] = ch[f][c]; else fail[v] = 1; q.push(v); } } dfs1(1, 0); dfs2(1, 1);}bool cmp(int a, int b) { return dfn[a] < dfn[b];}signed main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { vector <int> vec; int l; cin >> l; while (l--) { int c; cin >> c; vec.push_back(c); } vec.push_back(10001); cin >> l; while (l--) { int c; cin >> c; vec.push_back(c); } Pos1[i] = insert(vec); } for (int i = 1; i <= m; i++) { vector <int> vec; int l; cin >> l; while (l--) { int c; cin >> c; vec.push_back(c); } Pos2[i] = insert(vec); } get_fail(); for (int i = 1; i <= n; i++) { int u = Pos1[i]; vector <int> vec; while (u) { vec.push_back(u); add(dfn[u], 1); u = fat[u]; } sort(vec.begin(), vec.end(), cmp); for (int i = 1; i < vec.size(); i++) add(dfn[lca(vec[i], vec[i - 1])], -1); } for (int i = 1; i <= m; i++) cout << sum(dfn[Pos2[i]] + siz[Pos2[i]] - 1) - sum(dfn[Pos2[i]] - 1) << '\n'; memset(c, 0, sizeof(c)); for (int i = 1; i <= m; i++) { add(dfn[Pos2[i]], 1); add(dfn[Pos2[i]] + siz[Pos2[i]], -1); } for (int i = 1; i <= n; i++) { int u = Pos1[i]; vector <int> vec; int ans = 0; while (u) { vec.push_back(u); ans += sum(dfn[u]); u = fat[u]; } sort(vec.begin(), vec.end(), cmp); for (int i = 1; i < vec.size(); i++) ans -= sum(dfn[lca(vec[i], vec[i - 1])]); cout << ans << ' '; } return 0;}```::::