版本比较
关于 SAM 的理解
正文
增加 206 行删除 1 行
116 行未改动全部展开117118119120121121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326116 行未改动全部展开否则设 $to_{p,c}=q$。
如果 $len_q=len_p+1$,说明自己的 $fa$ 已经被表示了,直接设为 $q$。
否则,```cppif (!p) { t[now].fa = 1; return;}int q = t[p].to[w];if (t[q].len == t[p].len + 1) { t[now].fa = q; return;}```
否则我们要新建一个节点表示 $fa_{now}$,然后将 $fa_q$ 和 $fa_{now}$ 全部设为它,当然它的 $to$ 和 $to_q$ 一致。
为什么这个时候不能直接设 $fa_{now}=q$ 呢,因为添加字符 $c$ 前的串有一个后缀长度为 $len_p$,加上字符 $c$ 后会产生一个长度为 $len_p+1$ 的字符串,然而由于 $len_q\ne len_p+1$,这个长度为 $len_q$ 字符串显然不是当前字符串后缀,否则 $len_p$ 就会更大,因此这个新的字符串不属于集合 $q$,而且这个字符串的 $endpos$ 比 $q$ 大,所以 $fa_q$ 也要设为新节点。
由于产生了新的 $endpos$,所以所有 $p$ 及其祖先中 $to_c=q$ 的都要更正为新节点。
```cppt[ ++ idx] = t[q];t[idx].len = t[p].len + 1;t[now].fa = t[q].fa = idx;while (p && t[p].to[w] == q) { t[p].to[w] = idx; p = t[p].fa;}```
至于 $siz$,把字符串遍历一遍,在遍历完每个节点的时候增加当前位置的 $siz$,之后按照 $fa_u\longrightarrow u$ 建树统计子树和即可。
```cppint at = 1;for (char c : s) { int w = c - 'a'; at = t[at].to[w]; ++ t[at].siz; }```
注意,由于 $endpos$ 集合里字符串大小并不一致,所以统计答案的时候子树内非自身节点的可以直接计算和,但是自身的还要二分一下,具体根据个人实现而定。
当需要知道一个字符串的出现位置时,只需要在每个前缀结尾所在的节点存个变量 $at$ 表示结尾下标即可。
至于广义 SAM 就是每个字符串加入一遍,每次加入到一个字符的时候先不建立新的节点,而是看看当前位置 $to_c$ 是不是 $\oslash $,是空再建,否则不建,直接当作已经建立的考虑。
给用这东西解决 AC 自动机模板题的代码,希望增进理解(这些代码中 $son$ 就是 $to$)。
实则 SAM 就相当于是对于所有子串建立了个 AC 自动机然后大力压状态。
```cpp#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n;
string t[N], s;
struct SAM { struct node { int son[26], fa, len, siz; }; node t[2 * N]; vector <int> g[2 * N], L[2 * N]; int lst, idx; SAM() { lst = idx = 1; } void insert (char c) { int w = c - 'a'; if (t[lst].son[w]) { int q = t[lst].son[w]; if (t[q].len == t[lst].len + 1) { lst = q; return; } t[ ++ idx] = t[q]; t[idx].len = t[lst].len + 1; t[q].fa = idx; int p = lst; while (p && t[p].son[w] == q) { t[p].son[w] = idx; p = t[p].fa; } lst = idx; return; } int now = ++ idx; t[now].len = t[lst].len + 1; int p = lst; lst = now; while (p && !t[p].son[w]) { t[p].son[w] = now; p = t[p].fa; } if (!p) { t[now].fa = 1; return; } int q = t[p].son[w]; if (t[q].len == t[p].len + 1) { t[now].fa = q; return; } t[ ++ idx] = t[q]; t[idx].len = t[p].len + 1; t[now].fa = t[q].fa = idx; while (p && t[p].son[w] == q) { t[p].son[w] = idx; p = t[p].fa; } return; } void que (string s) { int at = 1, len = 0; for (char c : s) { int w = c - 'a'; while (at && !t[at].son[w]) at = t[at].fa, len = t[at].len; if (!at) { at = 1; continue; } at = t[at].son[w]; ++ len; ++ t[t[at].fa].siz; L[at].push_back(len); } return; } void dfs1 (int u) { for (int v : g[u]) { t[v].siz += t[u].siz; dfs1(v); } return; } void dfs2 (int u) { for (int v : g[u]) { dfs2(v); t[u].siz += t[v].siz; } return; } void build() { for (int i = 2; i <= idx; ++ i ) g[t[i].fa].push_back(i), sort(L[i].begin(), L[i].end()); // dfs1(1); dfs2(1); } int query (string s) { int at = 1; for (char c : s) { int w = c - 'a'; if (!t[at].son[w]) return 0; at = t[at].son[w]; } return t[at].siz + (L[at].size() - (lower_bound(L[at].begin(), L[at].end(), s.size()) - L[at].begin())); } } T;
signed main() { ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 1; i <= n; ++ i ) { T.lst = 1; cin >> t[i]; for (char c : t[i]) T.insert(c); } cin >> s; T.que(s); T.build(); for (int i = 1; i <= n; ++ i ) cout << T.query(t[i]) << '\n'; return 0;} ```
求区间字符串在 SAM 里的位置,倍增跳到最上面长度。
```cppint w (int l, int r) { int u = at[r]; for (int i = 20; i >= 0; -- i ) if (t[st[u][i]].len >= r - l + 1) u = st[u][i]; return u;}```