字符串模板汇总

  1. 字符串哈希

P3370 【模板】字符串哈希

#include<iostream>
#include<cstring>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define LL long long 
#define ULL unsigned long long             
#define P pair<int,int>
const double eps = 1e-8;
const LL INF = 1e18;
const LL N = 1e4 + 10;
ULL h[N];
ULL ha(string s) {
    ULL p = 131, res = 0;
    int len = s.length();
    for (int i = 0; i < len; ++i) {
        res = res * p + s[i] - '0';
    }
    return res;
}
void solve() {
    int n,ans=0;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        string s;
        cin >> s;
        h[i] = ha(s);
    }
    sort(h + 1, h + n + 1);
    for (int i = 1; i <= n; ++i) {
        if (h[i] != h[i + 1]) ans++;
    }
    cout << ans;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _t;
    _t = 1;
    //cin>>_t;
    while (_t--) {
        solve();
    }
    return 0;
}
  1. Manacher算法

P3805 【模板】manacher 算法

#include<iostream>
#include<cstring>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define LL long long 
const LL N = 22e6 + 10;
char b[N];
int R[N];
void solve() {
    string s;
    cin >> s;
    int l = s.length(),len=0;
    b[0] = '~';
    for (int i = 0; i < l; ++i) {
        b[++len] = '|';
        b[++len] = s[i];
    }
    b[++len] = '|';
    int mid = 0, r = 0;
    for (int i = 1; i <= len; ++i) {
        if (i > r) {
            for (int j = 1; i - j > 0 && i + j <= len; ++j) {
                if (b[i - j] == b[i + j]) R[i]++;
                else break;
            }
            mid = i;
            r = mid + R[i];
        }
        else {
            R[i] = min(R[2 * mid - i], r - i);
            for (int j = R[i] + 1; i - j > 0 && i + j <= len; ++j) {
                if (b[i - j] == b[i + j]) R[i]++;
                else break;
            }
            if (i + R[i] > r) {
                mid = i;
                r = mid + R[i];
            }
        }
    }
    int ans = 0;
    for (int i = 1; i <= len; ++i) {
        ans = max(ans, R[i]);
    }
    cout << ans;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _t;
    _t = 1;
    //cin>>_t;
    while (_t--) {
        solve();
    }
    return 0;
}
  1. 字典树

P8306 【模板】字典树

#include<iostream>
#include<cstring>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define LL long long 
const LL N = 3e6 + 10;
int trie[N][70], cnt[N], id;
void insert(string s) {
    int p = 0;
    int len = s.length();
    int temp;
    for (int i = 0; i < len; ++i) {
        if (isdigit(s[i])) temp = s[i] - '0';
        else if (isupper(s[i]))  temp = s[i] - 'A' + 10;
        else  temp = s[i] - 'a' + 36;
        if (trie[p][temp] == 0) trie[p][temp] = ++id;
        p = trie[p][temp];
        cnt[p]++;
    }
}
int find(string s) {
    int p = 0;
    int len = s.length();
    int temp;
    for (int i = 0; i < len; ++i) {
        if (isdigit(s[i])) temp = s[i] - '0';
        else if (isupper(s[i]))  temp = s[i] - 'A' + 10;
        else  temp = s[i] - 'a' + 36;
        if (trie[p][temp] == 0) return 0;
        p = trie[p][temp];
    }
    return cnt[p];
}
void solve() {
    for (int i = 0; i <= id; ++i) {
        cnt[i] = 0;
        for (int j = 0; j <= 70; ++j) {
            trie[i][j] = 0;
        }
    }
    id = 0;
    int n, q;
    cin >> n >> q;;
    for (int i = 1; i <= n; ++i) {
        string s;
        cin >> s;
        insert(s);
    }
    for (int i = 1; i <= q; ++i) {
        string s;
        cin >> s;
        cout << find(s) << '\n';
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _t;
    //_t = 1;
    cin >> _t;
    while (_t--) {
        solve();
    }
    return 0;
}
  1. KMP算法

P3375 【模板】KMP字符串匹配

#include<iostream>
#include<cstring>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define LL long long 
#define P pair<int,int>
const double eps = 1e-8;
const LL INF = 1e18;
const LL N = 1e6 + 10;
string s1, s2;
int len1, len2,n[N],sum=0,ans[N];
void creatnext() {
    int j = 0, k = -1;
    n[0] = -1;
    while (j <= len2 - 1) {
        if (k == -1 || s2[k] == s2[j]) {
            j++;
            k++;
            //if (p[k] != p[j]) n[j] = k;
            //else n[j] = n[k];
            n[j] = k;
        }
        else k = n[k];
    }
}
void kmp() {
    int i = 0, j = 0;
    while (i <len1) {
        if (j == -1 || s1[i] == s2[j]) {
            j++;
            i++;
        }
        else j = n[j];
        if (j == len2) {
            ans[++sum] = i - len2 + 1;
            j = n[j];
        }
    }
}
void solve() {
    cin >> s1 >> s2;
    len1 = s1.length();
    len2 = s2.length();
    creatnext();
    kmp();
    for (int i = 1; i <= sum; ++i) cout << ans[i] << '\n';
    for (int i = 1; i <= len2; ++i) {
        if (n[i] == -1) n[i] = 0; 
        cout << n[i] << " ";
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _t;
    _t = 1;
    //cin>>_t;
    while (_t--) {
        solve();
    }
    return 0;
}
  1. 扩展KMP

P5410 【模板】扩展 KMP(Z 函数)

#include<iostream>
#include<cstring>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define LL long long 
#define P pair<int,int>
const double eps = 1e-8;
const LL INF = 1e18;
const LL N = 2e7 + 10;
int nxt[N], ex[N], lena, lenb;
string a, b;
void creatnext() {
    int p = 0, k = 1, l;
    nxt[0] = lenb;
    while (p + 1 < lenb && b[p] == b[p + 1]) p++;
    nxt[1] = p;
    for (int i = 2; i < lenb; ++i) {
        p = k + nxt[k] - 1;
        l = nxt[i - k];
        if (i + l <= p) nxt[i] = l;
        else {
            int j = max(0, p - i + 1);
            while (i + j < lenb && b[j] == b[i + j]) j++;
            nxt[i] = j;
            k = i;
        }
    }
}
void exkmp() {
    int p = 0, k = 0, l;
    while (p < lena && p < lenb && a[p] == b[p]) p++;
    ex[0] = p;
    for (int i = 1; i < lena; ++i) {
        p = k + ex[k] - 1;
        l = nxt[i - k];
        if (i + l <= p) ex[i] = l;
        else {
            int j = max(0, p - i + 1);
            while (i + j < lena && j < lenb && b[j] == a[i + j]) j++;
            ex[i] = j;
            k = i;
        }
    }
}
void solve() {
    cin >> a >> b;
    lena = a.length(), lenb = b.length();
    creatnext();
    LL ans = 0;
    for (int i = 0; i < lenb; ++i) ans ^= 1LL * (i + 1) * (nxt[i] + 1);
    cout << ans << '\n';
    exkmp();
    ans = 0;
    for (int i = 0; i < lena; ++i) ans ^= 1LL * (i + 1) * (ex[i] + 1);
    cout << ans;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _t;
    _t = 1;
    //cin>>_t;
    while (_t--) {
        solve();
    }
    return 0;
}
  1. AC自动机

P3796 【模板】AC 自动机(加强版)

#include <iostream>
#include<cstring>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define LL long long 
const LL N = 1e6 + 10;
int trie[N][26], cnt[N], fail[N], id = 0, maxnum,num[N];
string ss[N];
string s;
void insert(string s) {
    int p = 0;
    int len = s.size();
    for (int i = 0; i < len; ++i) {
        int temp = s[i] - 'a';
        if (trie[p][temp] == 0) {
            trie[p][temp] = ++id;
        }
        p = trie[p][temp];
    }
    cnt[p]++;
    ss[p] = s;
}
void creatfail() {
    queue<int> q;
    int p = 0;
    for (int i = 0; i <= 25; ++i) {
        if (trie[p][i]) q.push(trie[p][i]);
    }
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int i = 0; i <= 25; ++i) {
            if (trie[u][i]) {
                fail[trie[u][i]] = trie[fail[u]][i];
                q.push(trie[u][i]);
            }
            else trie[u][i] = trie[fail[u]][i];
        }
    }
}
void query(string s) {
    int p = 0;
    int len = s.size();
    for (int i = 0; i < len; ++i) {
        int temp = s[i] - 'a';
        p = trie[p][temp];
        for (int j = p; j != 0 ; j = fail[j]) {
            if (cnt[j]) {
                num[j]++;
                if (num[j] > maxnum) {                  
                    maxnum = num[j];
                }
            }
        }
    }
}
void solve() {
    while (1) {
        int n;
        cin >> n;
        if (n == 0) break;
        for (int i = 0; i <= id; ++i) {
            cnt[i] = fail[i] =num[i] = 0;
            for (int j = 0; j <= 25; ++j) {
                trie[i][j] = 0;
            }
        }
        id =maxnum= 0;
        for (int i = 0; i < n; ++i) {
            cin >> s;
            insert(s);
        }
        creatfail();
        cin >> s;
        query(s);
        cout << maxnum << '\n';
        for (int i = 1; i <= id; ++i) {
            if (num[i] == maxnum) cout << ss[i] << '\n';
        }
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _t;
    _t = 1;
    //cin >> _t;
    while (_t--) {
        solve();
    }
    return 0;
}
  1. AC自动机优化(dfs)

P5357 【模板】AC 自动机(二次加强版)

此外还有拓扑排序、树上差分等做法

#include <iostream>
#include<cstring>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define LL long long
const LL N = 2e6 + 10;
int trie[N][26], cnt[N], fail[N], id = 0, num[N],MAP[N];
string s;
vector<vector<int> > v(N);
void insert(int cur,string s) {
    int p = 0;
    int len = s.size();
    for (int i = 0; i < len; ++i) {
        int temp = s[i] - 'a';
        if (trie[p][temp] == 0) {
            trie[p][temp] = ++id;
        }
        p = trie[p][temp];
    }
    MAP[cur] = p;
}
void creatfail() {
    queue<int> q;
    int p = 0;
    for (int i = 0; i <= 25; ++i) {
        if (trie[p][i]) q.push(trie[p][i]);
    }
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int i = 0; i <= 25; ++i) {
            if (trie[u][i]) {
                fail[trie[u][i]] = trie[fail[u]][i];
                q.push(trie[u][i]);
            }
            else trie[u][i] = trie[fail[u]][i];
        }
    }
}
void query(string s) {
    int p = 0;
    int len = s.size();
    for (int i = 0; i < len; ++i) {
        int temp = s[i] - 'a';
        p = trie[p][temp];
        num[p]++;
    }
}
void build() {
    for (int i = 1; i <= id; ++i) {
        v[fail[i]].push_back(i);
    }
}
void dfs(int u) {
    for (auto i : v[u]) {
        dfs(i);
        num[u] += num[i];
    }
}
void solve() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> s;
        insert(i,s);
    }
    creatfail();
    cin >> s;
    query(s);
    build();
    dfs(0);
    for (int i = 0; i <n; ++i) {
        cout<<num[MAP[i]] << '\n';
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _t;
    _t = 1;
    //cin >> _t;
    while (_t--) {
        solve();
    }
    return 0;
}
  1. 回文自动机

P5496 【模板】回文自动机(PAM)

#include<iostream>
#include<cstring>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define LL long long 
#define P pair<int,int>
const double eps = 1e-8;
const LL INF = 1e18;
const LL N = 5e5 + 10;
int last,cnt=1,s[N];
struct node {
    int len, fail, siz, son[26];
}tree[N];
int getfail(int x,int pos) {
    while (pos-tree[x].len-1<0||s[pos - tree[x].len - 1] != s[pos]) x = tree[x].fail;
    return x;
}
int pam(int pos) {
    int fa = getfail(last, pos);
    int now = tree[fa].son[s[pos]];
    if (!now) {
        now = ++cnt;
        tree[now].fail = tree[getfail(tree[fa].fail, pos)].son[s[pos]];
        tree[now].len = tree[fa].len + 2;
        tree[now].siz = tree[tree[now].fail].siz + 1;
        tree[fa].son[s[pos]] = now;
    }
    last = now;
    return tree[now].siz;
}
void solve() {
    string ss;
    cin >> ss;
    int len = ss.length(),k;
    tree[0].fail = 1;
    tree[1].len = -1;
    for (int i = 0; i < len; ++i) {
        if (!i) s[i]= ss[i]-'a';
        else s[i] = (ss[i] - 'a' + k) % 26;
        k = pam(i);
        cout << k << " ";
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _t;
    _t = 1;
    //cin>>_t;
    while (_t--) {
        solve();
    }
    return 0;
}
  1. 后缀数组(sort排序)

P3809 【模板】后缀排序

构建

查询

#include<iostream>
#include<cstring>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define LL long long 
#define P pair<int,int>
const double eps = 1e-8;
const LL INF = 1e18;
const LL N = 1e6 + 10;
string s;
int sa[N], rk[N],temp[N] ,k,n;
bool cmpsa(int i, int j) {
    if (rk[i] != rk[j]) {
        return rk[i] < rk[j];
    }
    else {
        int ri = i + k < n ? rk[i + k] : -1;
        int rj = j + k < n ? rk[j + k] : -1;
        return ri < rj;
    }
}
void creatsa() {
    for (int i = 0; i < n; ++i) {
        rk[i] = s[i];
        sa[i] = i;
    }
    for (k = 1; k < n; k *= 2) {
        sort(sa, sa + n, cmpsa);
        temp[sa[0]] = 0;
        for (int i = 0; i < n - 1; ++i) temp[sa[i + 1]] = temp[sa[i]] + (cmpsa(sa[i], sa[i + 1] )? 1:0);
        for (int i = 0; i < n; ++i) rk[i] = temp[i];
    }
}
void solve() {
    cin >> s;
    n = s.length();
    creatsa();
    for (int i = 0; i < n; ++i) cout << sa[i]+1 << " ";
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _t;
    _t = 1;
    //cin>>_t;
    while (_t--) {
        solve();
    }
    return 0;
}
  1. 后缀数组(基数排序)

P3809 【模板】后缀排序

构建

查询

#include<iostream>
#include<cstring>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define LL long long 
#define P pair<int,int>
const double eps = 1e-8;
const LL INF = 1e18;
const LL N = 1e6 + 10;
string s;
int c[N],sa[N], rk[N],tp[N] ,n,m=127;
void creatsa() {
    for (int i = 0; i < n; ++i) {
        rk[i] = s[i];
        c[rk[i]]++;
    }
    for (int i = 1; i < m; ++i) c[i] += c[i - 1];
    for (int i = n-1; i >=0; --i) sa[--c[rk[i]]] = i;
    for (int k = 1; k < n; k *= 2) {
        int num = 0;
        for (int i = n - k; i < n; ++i) tp[num++] = i;
        for (int i = 0; i < n; ++i) if (sa[i] >= k) tp[num++] = sa[i] - k;
        for (int i = 0; i < m; ++i) c[i] = 0;
        for (int i = 0; i < n; ++i) c[rk[tp[i]]]++;
        for (int i = 1; i < m; ++i) c[i] += c[i - 1];
        for (int i = n - 1; i >= 0; --i) sa[--c[rk[tp[i]]]] = tp[i];
        num = 1;
        swap(rk, tp);
        rk[sa[0]] = 0;
        for (int i = 1; i < n; ++i) {
            num += tp[sa[i]] == tp[sa[i - 1]] && tp[sa[i]+k] == tp[sa[i - 1]+k] ? 0 : 1;
            rk[sa[i]] = num-1;
        }
        if (num == n) return;
        m = num;
    }
}
void solve() {
    cin >> s;
    n = s.length();
    creatsa();
    for (int i = 0; i < n; ++i) cout << sa[i] + 1 << " ";
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _t;
    _t = 1;
    //cin>>_t;
    while (_t--) {
        solve();
    }
    return 0;
}
  1. 求height数组

void getheight() {
    int j, k = 0;
    for (int i = 0; i < n; ++i) rk[sa[i]] = i;
    for (int i = 0; i < n; ++i) {
        if (k) k--;
        if (rk[i] == 0) continue;
        int j = sa[rk[i] - 1];
        while (s[i + k] == s[j + k]) k++;
        height[rk[i]] = k;
    }
}
  1. 后缀自动机

P3804 【模板】后缀自动机 (SAM)

#include<iostream>
#include<cstring>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define LL long long 
#define P pair<int,int>
const double eps = 1e-8;
const LL INF = 1e18;
const LL N = 1e6 + 10;
struct node {
    int fa, len, son[26],num;
}tree[N*2];
vector<vector<int> >e(N * 2);
int cnt = 0, last = 0;
LL ans=0;
void insert(int x) {
    tree[++cnt].len=tree[last].len + 1;
    tree[cnt].num = 1;
    int p = last, now = cnt;
    while (p != -1 && !tree[p].son[x]) {
        tree[p].son[x] = now;
        p = tree[p].fa;
    }
    if (p == -1) tree[now].fa = 0;
    else {
        int q = tree[p].son[x];
        if (tree[q].len == tree[p].len + 1) tree[now].fa = q;
        else {
            tree[++cnt].len=tree[p].len + 1;
            int nq = cnt;
            memcpy(tree[nq].son, tree[q].son, sizeof(tree[q].son));
            tree[nq].fa = tree[q].fa;
            tree[now].fa = tree[q].fa = nq;
            while (p != -1 && tree[p].son[x] == q) {
                tree[p].son[x] = nq;
                p = tree[p].fa;
            }
        }
    }
    last = now;
}
void dfs(int x) {
    for (int i :e[x]) {
        dfs(i);
        tree[x].num += tree[i].num;
    }
    if(tree[x].num!=1) ans = max(ans, 1LL*tree[x].num*tree[x].len);
}
void solve() {
    string s;
    cin >> s;
    int n = s.length();
    tree[0].fa = -1;
    for (int i = 0; i < n; ++i) insert(s[i] - 'a');
    for (int i = 1; i <= cnt; ++i) e[tree[i].fa].push_back(i);
    dfs(0);
    cout << ans;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _t;
    _t = 1;
    //cin>>_t;
    while (_t--) {
        solve();
    }
    return 0;
}

后缀自动机应用

  1. 广义后缀自动机(离线)

P6139 【模板】广义后缀自动机(广义 SAM)

#include<iostream>
#include<cstring>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define LL long long 
#define P pair<int,int>
const double eps = 1e-8;
const LL INF = 1e18;
const LL N = 1e6 + 10;
struct node {
    int fa, len, son[26];
}tree[2 * N];
int cnt = 0, last = 0, trie[N][26], fa[N], val[N], pos[N];
void trie_insert(string s) {
    int p = 0, len = s.length();
    for (int i = 0; i < len; ++i) {
        int x = s[i] - 'a';
        if (!trie[p][x]) {
            trie[p][x] = ++cnt;
            fa[cnt] = p;
            val[cnt] = x;
        }
        p = trie[p][x];
    }
}
int sam_insert(int x) {
    /*if (tree[last].son[x]) {
        int p = last, q = tree[last].son[x];
        tree[++cnt].len = tree[p].len + 1;
        int nq = cnt;
        memcpy(tree[nq].son, tree[q].son, sizeof(tree[q].son));
        tree[nq].fa = tree[q].fa;
        tree[q].fa = nq;
        while (p != -1 && tree[p].son[x] == q) {
            tree[p].son[x] = nq;
            p = tree[p].fa;
        }
        return nq;
    }
    else {*/
        tree[++cnt].len = tree[last].len + 1;
        int p = last, now = cnt;
        while (p != -1 && !tree[p].son[x]) {
            tree[p].son[x] = now;
            p = tree[p].fa;
        }
        if (p == -1) tree[now].fa = 0;
        else {
            int q = tree[p].son[x];
            if (tree[q].len == tree[p].len + 1) tree[now].fa = q;
            else {
                tree[++cnt].len = tree[p].len + 1;
                int nq = cnt;
                memcpy(tree[nq].son, tree[q].son, sizeof(tree[q].son));
                tree[nq].fa = tree[q].fa;
                tree[now].fa = tree[q].fa = nq;
                while (p != -1 && tree[p].son[x] == q) {
                    tree[p].son[x] = nq;
                    p = tree[p].fa;
                }
            }
        }
        return now;
    //}
}
void dfs(int x) {
    for (int i = 0; i < 26; ++i) {
        if (trie[x][i]) {
            int u = trie[x][i];
            last = pos[fa[u]];
            pos[u] = sam_insert(val[u]);
            dfs(trie[x][i]);
        }
    }
}
void bfs() {
    queue<int> q;
    for (int i = 0; i < 26; ++i) if (trie[0][i]) q.push(trie[0][i]);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        last = pos[fa[u]];
        pos[u] = sam_insert(val[u]);
        for (int i = 0; i < 26; ++i) if (trie[u][i]) q.push(trie[u][i]);
    }
}
void solve() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        string s;
        cin >> s;
        trie_insert(s);
    }
    cnt = 0;
    tree[0].fa = -1;
    //dfs(0);
    bfs();
    LL ans = 0;
    for (int i = 1; i <= cnt; ++i) ans += tree[i].len - tree[tree[i].fa].len;
    cout << ans;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _t;
    _t = 1;
    //cin>>_t;
    while (_t--) {
        solve();
    }
    return 0;
}
  1. 广义后缀自动机(在线)

P6139 【模板】广义后缀自动机(广义 SAM)

#include<iostream>
#include<cstring>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define LL long long 
#define P pair<int,int>
const double eps = 1e-8;
const LL INF = 1e18;
const LL N = 1e6 + 10;
struct node {
    int fa, len, son[26];
}tree[2 * N];
int cnt = 0, last = 0;
void insert(int x) {
    if (tree[last].son[x]) {
        int p = last, q = tree[last].son[x];
        if (tree[q].len == tree[p].len + 1) last = q;
        else {
            tree[++cnt].len = tree[p].len + 1;
            int nq = cnt;
            memcpy(tree[nq].son, tree[q].son, sizeof(tree[q].son));
            tree[nq].fa = tree[q].fa;
            tree[q].fa = nq;
            while (p != -1 && tree[p].son[x] == q) {
                tree[p].son[x] = nq;
                p = tree[p].fa;
            }
            last = nq;
        }
    }
    else {
        tree[++cnt].len = tree[last].len + 1;
        int p = last, now = cnt;
        while (p != -1 && !tree[p].son[x]) {
            tree[p].son[x] = now;
            p = tree[p].fa;
        }
        if (p == -1) tree[now].fa = 0;
        else {
            int q = tree[p].son[x];
            if (tree[q].len == tree[p].len + 1) tree[now].fa = q;
            else {
                tree[++cnt].len = tree[p].len + 1;
                int nq = cnt;
                memcpy(tree[nq].son, tree[q].son, sizeof(tree[q].son));
                tree[nq].fa = tree[q].fa;
                tree[now].fa = tree[q].fa = nq;
                while (p != -1 && tree[p].son[x] == q) {
                    tree[p].son[x] = nq;
                    p = tree[p].fa;
                }
            }
        }
        last = now;
    }
}
void solve() {
    int n;
    cin >> n;
    tree[0].fa = -1;
    for (int i = 1; i <= n; ++i) {
        string s;
        cin >> s;
        int len = s.length();
        last = 0;
        for (int j = 0; j < len; ++j) insert(s[j] - 'a');
    }
    LL ans = 0;
    for (int i = 1; i <= cnt; ++i) ans += tree[i].len - tree[tree[i].fa].len;
    cout << ans;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _t;
    _t = 1;
    //cin>>_t;
    while (_t--) {
        solve();
    }
    return 0;
}

广义后缀自动机的一些应用

  1. 查找子串的出现次数。
    对于子串 t, 出现次数其实就是结束位置的数量,也就是 ∣endpos(t)∣。
    每插入一个字符,标记当时的last
    最后这些 last 统一往上跳,计算∣endpos(t)∣ 即可。

  1. 查找所有不同子串的出现次数。
    计算 ∣endpos(t)∣ 即可,
    同一个 ⁡endpos 包含的子串数量可用 len 计算出来。

  1. 查找所有不同子串的总长。
    同一个 endpos 包含的子串总长也可以用 len 计算出来。

  1. 查找所有不同子串的 总长 × 出现次数
    2, 3已经给出,因此同上。

  1. 查找两个串的最长公共后缀/前缀。
    后缀其实就是求 fail 树上的 LCA,前缀对反串建 SAM 即可。

  1. 分别维护不同串的 siz

求两个字符串的相同子串数量。

找相同字符 [P3181]

如上黑体字所说,两个串的 |endpos|要分开计算,可以开一个二维数组,用 siz[x][id] 表示节点 x在 串 id上的 |endpos|大小。

则答案为:∑siz[i][0]×siz[i][1]×(maxlen[i]−maxlen[link[i]])

  1. 线段树合并维护 siz

给出主串 S以及 m 个字符串 T[1..m]。有若干次询问,每次查询 S的子串 S[pl..pr]在 T[l..r]中的哪个 串 Ti 里的出现次数最多,输出 i以及出现次数,有多解则取最靠前的那一个。

Forensic Examination[CF666E]

先把所有字符串都插入到广义 SAM中,对于每个节点开一颗下标为 [1,m]的动态开点线段树维护 siz (注意插入 S 时就不要在线段树上进行修改操作了)。由于 siz的维护是统计子树和,所以插入结束后 要在 parent树上跑一下线段树合并。

查询时先在 parent 树上倍增找到包含子串 S[pl,pr]的等价类状态节点,然后在该点的线段树上查询区 间 [l,r]中的最大值,顺便维护下最大值所处位置即可。

  1. 树上本质不同路径数

给出一颗叶子结点不超过 20 个的无根树,每个节点上都有一个不超过 10 的数字,求树上本质不同 的路径个数(两条路径相同定义为:其路径上所有节点上的数字依次相连组成的字符串相同)。

诸神眷顾的幻想乡 [ZJOI2015] [P3346]

首先有一个很麻烦的地方是路径可以拐弯(即两端点分别在其 lca两个不同儿子节点的子树中),而 Trie树和各种自动机在“接受”字符串时都是以根为起点从上往下径直走到底

这里直接抛结论:

一颗无根树上任意一条路径必定可以在以某个叶节点为根时,变成一条从上到下的路径(利于广义 SAM的使用)

注意到题目中说叶节点不超过 20 个,这意味着暴力枚举每一个叶节点作为根节点遍历整棵树

将一共 颗树中的所有前缀串都抽出来建立广义 SAM,然后直接求本质不同的子串个数。 其中前缀串定义为从根节点(无根树的某个叶子结点)到任意一个节点的路径所构成的字符串(实际 上就是将 颗Trie 树合在了一起跑广义SAM)。

  1. 最小表示法

P1368 【模板】最小表示法

#include<iostream>
#include<cstring>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define ll long long 
#define P pair<int,int>
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const ll INF = 1e18;
const ll N = 3e5 + 10;
int a[N << 1];
void solve() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        a[i + n] = a[i];
    }
    int i = 1, j = 2, k;
    while (i <= n && j <= n) {
        k = 0;
        while (k < n && a[i + k] == a[j + k]) k++;
        if (k == n) break;
        if (a[i + k] > a[j + k]) i += k + 1;
        else j += k + 1;
        if (i == j) j++;
    }
    k = min(i, j);
    for (int i = 1; i <= n; ++i) cout << a[k + i - 1] << " ";
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _t;
    _t = 1;
    //cin>>_t;
    while (_t--) {
        solve();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值