版本比较
【学习笔记】AC 自动机
正文
增加 12 行删除 12 行
6 行未改动789101112131415161718192018 行未改动394041424344454647484950515216 行未改动697071727374757677787926 行未改动10610710810911011111211311411511611719 行未改动13713813914014114214314414543 行未改动18919019119219319419519619741 行未改动6 行未改动首先建出 trie 树,下面是代码:```cpp line-numbersinline void insert(string s) {int n = s.size(), now = 0;for (int i = 0; i < n; i++) {char c = s[i] - 'a';if (!a[now][c]) a[now][c] = ++ts;now = a[now][c];} cnt[now]++;}```然后求 $fail$ 数组。对于节点 $u$,$fail_u=v$ 的定义是 trie 中 $u$ 对应的字符串的最长真后缀使得其对应的节点是 $v$,和 KMP 一样,$fail$ 指针都是为了在失配时可以快速转移。18 行未改动inline void build() {queue <int> q;for (auto i : A[0]) q.push(i.se);while (!q.empty()) {int t = q.front(); q.pop();for (auto i : A[t]) {int j = fail[t];while (j && !A[j].count(i.fi)) j = fail[j];if (A[j].count(i.fi)) fail[i.se] = A[j][i.fi];q.push(i.se);}}```16 行未改动while (!q.empty()) {int t = q.front(); q.pop();// do somethingfor (int i = 0; i < M; i++) {if (!a[t][i]) a[t][i] = a[fail[t]][i];else fail[a[t][i]] = a[fail[t]][i], q.push(a[t][i]);}}}```26 行未改动```cpp line-numbersinline int find(string s) {int n = s.size(), now = 0, ans = 0;for (int i = 0; i < n; i++) {now = a[now][s[i] - 'a'];for (int j = now; j && cnt[j] != -1; j = fail[j])ans += cnt[j], cnt[j] = -1;} return ans;}```19 行未改动void find(string s) {int now = 0;for (int i = 0; i < s.size(); i++) {now = a[now][s[i] - 'a'];for (int j = now; j; j = lst[j]) ans[id[j]]++;}}```43 行未改动queue <int> q;for (int i = 0; i < M; i++)if (a[0][i]) q.push(a[0][i]);while (!q.empty()) {int t = q.front(); q.pop();for (int i = 0; i < M; i++) {if (!a[t][i]) a[t][i] = a[fail[t]][i];else fail[a[t][i]] = a[fail[t]][i], q.push(a[t][i]);} cnt[t] += cnt[fail[t]];41 行未改动6 行未改动789101112131415161718192018 行未改动394041424344454647484950515216 行未改动697071727374757677787926 行未改动10610710810911011111211311411511611719 行未改动13713813914014114214314414543 行未改动18919019119219319419519619741 行未改动6 行未改动首先建出 trie 树,下面是代码:```cpp line-numbersinline void insert(string s) {int n = s.size(), now = 0;for (int i = 0; i < n; i++) {char c = s[i] - 'a';if (!a[now][c]) a[now][c] = ++ts;now = a[now][c];} cnt[now]++;}```然后求 $fail$ 数组。对于节点 $u$,$fail_u=v$ 的定义是 trie 中 $u$ 对应的字符串的最长真后缀使得其对应的节点是 $v$,和 KMP 一样,$fail$ 指针都是为了在失配时可以快速转移。18 行未改动inline void build() {queue <int> q;for (auto i : A[0]) q.push(i.se);while (!q.empty()) {int t = q.front(); q.pop();for (auto i : A[t]) {int j = fail[t];while (j && !A[j].count(i.fi)) j = fail[j];if (A[j].count(i.fi)) fail[i.se] = A[j][i.fi];q.push(i.se);}}```16 行未改动while (!q.empty()) {int t = q.front(); q.pop();// do somethingfor (int i = 0; i < M; i++) {if (!a[t][i]) a[t][i] = a[fail[t]][i];else fail[a[t][i]] = a[fail[t]][i], q.push(a[t][i]);}}}```26 行未改动```cpp line-numbersinline int find(string s) {int n = s.size(), now = 0, ans = 0;for (int i = 0; i < n; i++) {now = a[now][s[i] - 'a'];for (int j = now; j && cnt[j] != -1; j = fail[j])ans += cnt[j], cnt[j] = -1;} return ans;}```19 行未改动void find(string s) {int now = 0;for (int i = 0; i < s.size(); i++) {now = a[now][s[i] - 'a'];for (int j = now; j; j = lst[j]) ans[id[j]]++;}}```43 行未改动queue <int> q;for (int i = 0; i < M; i++)if (a[0][i]) q.push(a[0][i]);while (!q.empty()) {int t = q.front(); q.pop();for (int i = 0; i < M; i++) {if (!a[t][i]) a[t][i] = a[fail[t]][i];else fail[a[t][i]] = a[fail[t]][i], q.push(a[t][i]);} cnt[t] += cnt[fail[t]];41 行未改动