跳到正文

剪贴板

剪贴板 mdcyixxg

原文
剪贴板信息

剪贴板信息

收录
更新

字符串板子

哈希

单哈希

C++
using ull = unsigned long long;
const ull base = 131;
const ull mod = 1e9 + 7;
inline ull get_hash(string s) {
    int len = s.size();
    ull res = 0;
    for (int i = 0;i < len;i++) res = (res * base + (ull)s[i]) % mod;
    return res;
}

双哈希

C++
using ull = unsigned long long;
ull base = 131;
ull mod1 = 212370440130137957, mod2 = 1e9 + 7;
inline ull get_hash1(string s) {
    int len = s.size();
    ull ans = 0;
    for (int i = 0; i < len; i++) ans = (ans * base + (ull)s[i]) % mod1;
    return ans;
}
inline ull get_hash2(string s) {
    int len = s.size();
    ull ans = 0;
    for (int i = 0; i < len; i++) ans = (ans * base + (ull)s[i]) % mod2;
    return ans;
}
inline bool compare(const string s, const string t) {
    bool f1 = get_hash1(s) != get_hash1(t);
    bool f2 = get_hash2(s) != get_hash2(t);
    return f1 || f2;
}

字典树

Trie

C++
const int N = 3e6 + 5;
const int L = 66;
class Trie {
private:
    inline int calc(char c) {
        if (c >= '0' && c <= '9') return c - '0';
        if (c >= 'a' && c <= 'z') return c - 'a' + 10;
        return c - 'A' + 36;
    }
public:
    int nex[N][L], cnt;
    int tot[N];
    bool exist[N];
    inline void init() {
        for (int i = 0;i <= cnt;i++) {
            tot[i] = 0, exist[i] = 0;
            for (int j = 0;j < L;j++) {
                nex[i][j] = 0;
            }
        }
    }
    inline void insert(char* s, int l) {
        int p = 0;
        for (int i = 0;i < l;i++) {
            int c = calc(s[i]);
            if (!nex[p][c]) nex[p][c] = ++cnt;
            p = nex[p][c];
            tot[p]++;
        }
        exist[p] = 1;
    }
    inline bool find(char* s, int l) {
        int p = 0;
        for (int i = 0;i < l;i++) {
            int c = calc(s[i]);
            if (!nex[p][c]) return false;
            p = nex[p][c];
        }
        return exist[p];
    }
    inline int search(char* s, int l) {
        int p = 0;
        for (int i = 0;i < l;i++) {
            int c = calc(s[i]);
            if (!nex[p][c]) return 0;
            p = nex[p][c];
        }
        return tot[p];
    }
}trie;

01Trie

C++
const int N = 1e5 + 5;
const int BIT = 30;
const int M = N * (BIT + 1);
class Trie {
public:
    int nex[M][2], tot;
    Trie() {
        tot = 1;
        memset(nex, 0, sizeof(nex));
    }
    inline void insert(int x) {
        int p = 1;
        for (int i = BIT;i >= 0;i--) {
            int c = (x >> i) & 1;
            if (!nex[p][c]) nex[p][c] = ++tot;
            p = nex[p][c];
        }
    }
    inline int get(int x) {
        int res = 0, p = 1;
        for (int i = BIT;i >= 0;i--) {
            int c = (x >> i) & 1;
            if (nex[p][c ^ 1]) {
                res |= (1 << i);
                p = nex[p][c ^ 1];
            } else {
                p = nex[p][c];
            }
        }
        return res;
    }
}trie;

KMP算法

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
int pi[N];
inline void get_prefix(string s) {
    int n = s.size();
    for (int i = 1;i < n;i++) {
        int j = pi[i - 1];
        while (j && s[i] != s[j]) j = pi[j - 1];
        if (s[i] == s[j]) j++;
        pi[i] = j;
    }
}
inline int KMP(string str, string pet) {
    int n = str.size();
    int m = pet.size();
    get_prefix(pet);
    int j = 0;
    for (int i = 0;i < n;i++) {
        while (j > 0 && str[i] != pet[j]) j = pi[j - 1];
        if (str[i] == pet[j]) j++;
        if (j == m) return i - m + 2;
    }
    return -1;
}
string str, pet;
namespace Solution {
    inline void Main() {
        cin >> str >> pet;
        int n = str.size();
        int m = pet.size();
        get_prefix(pet);
        int j = 0;
        for (int i = 0;i < n;i++) {
            while (j > 0 && str[i] != pet[j]) j = pi[j - 1];
            if (str[i] == pet[j]) j++;
            if (j == m) {
                cout << i - m + 2 << '\n';
                j = pi[j - 1];
            }
        }
        for (int i = 0;i < m;i++) cout << pi[i] << " ";
    }
}
signed main() {
    int T = 1;
    // cin >> T;
    while (T--) Solution::Main();
    return 0;
}

exKMP/Z函数

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e7 + 5;
long long z[N];
long long p[N];
void getz(string str) {
    int n = str.size();
    z[0] = n;
    int l = 0;
    int r = 0;
    for (int i = 1; i < n; i++) {
        if (i <= r) {
            z[i] = min((long long)z[i - l], (long long)r - i + 1);
        }
        while (i + z[i] < n && str[i + z[i]] == str[z[i]]) {
            z[i]++;
        }
        if (i + z[i] - 1 > r) {
            l = i;
            r = i + z[i] - 1;
        }
    }
}
void getp(string str, string pat) {
    int n = str.size(), m = pat.size();
    int l = 0;
    int r = -1;
    for (int i = 0; i < n; i++) {
        if (i <= r) {
            p[i] = min(z[i - l], (long long)r - i + 1);
        }
        while (i + p[i] < n && p[i] < m && str[i + p[i]] == pat[p[i]]) {
            p[i]++;
        }
        if (i + p[i] - 1 > r) {
            l = i;
            r = i + p[i] - 1;
        }
    }
}
string a, b;
namespace Solution {
    inline void Main() {
        cin >> a >> b;
        int n = a.size();
        int m = b.size();
        getz(b);
        getp(a, b);
        int ans1 = 0;
        int ans2 = 0;
        for (int i = 0; i < m; i++) {
            ans1 ^= (int)(i + 1) * (z[i] + 1);
        }
        for (int i = 0; i < n; i++) {
            ans2 ^= (int)(i + 1) * (p[i] + 1);
        }
        cout << ans1 << "\n" << ans2 << endl;
    }
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) Solution::Main();
    return 0;
}

失配树

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int LOG = 20;
int nxt[N];
int up[N][LOG + 1];
int dep[N];
inline void get_prefix(const string& str) {
    int n = str.size() - 1;
    nxt[1] = 0;
    for (int i = 2; i <= n; i++) {
        int j = nxt[i - 1];
        while (j > 0 && str[i] != str[j + 1]) j = nxt[j];
        if (str[i] == str[j + 1]) j++;
        nxt[i] = j;
    }
}
inline int lca(int u, int v) {
    if (dep[u] < dep[v]) swap(u, v);
    int diff = dep[u] - dep[v];
    for (int i = 0; i <= LOG; i++) {
        if (diff >> i & 1) u = up[u][i];
    }
    if (u == v) return u;
    for (int i = LOG; i >= 0; i--) {
        if (up[u][i] != up[v][i]) u = up[u][i], v = up[v][i];
    }
    return up[u][0];
}
string s;
int m;
namespace Solution {
    inline void Main() {
        cin >> s;
        s = " " + s;
        int n = s.size() - 1;
        get_prefix(s);
        dep[0] = 0;
        for (int i = 0; i <= LOG; i++) {
            up[0][i] = 0;
        }
        for (int i = 1; i <= n; i++) {
            up[i][0] = nxt[i];
            dep[i] = dep[nxt[i]] + 1;
            for (int j = 1; j <= LOG; j++) {
                up[i][j] = up[up[i][j - 1]][j - 1];
            }
        }
        cin >> m;
        while (m--) {
            int p, q;
            cin >> p >> q;
            int ans = lca(p, q);
            if (ans == p || ans == q) ans = up[ans][0];
            cout << ans << '\n';
        }
    }
}
signed main() {
    Solution::Main();
    return 0;
}

Manacher

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
inline int Manacher(string str) {
    int n = str.size();
    vector <int> p(2 * n + 5);
    vector <char> t(2 * n + 5);
    int m = 0;
    t[++m] = '$';
    for (int i = 0;i < n;i++) t[++m] = str[i], t[++m] = '$';
    int M = 0, R = 0;
    for (int i = 1;i <= m;i++) {
        if (i > R) p[i] = 1;
        else p[i] = min(p[2 * M - i], R - i + 1);
        while (i - p[i] >= 1 && i + p[i] <= m && t[i - p[i]] == t[i + p[i]]) p[i]++;
        if (i + p[i] - 1 > R) M = i, R = i + p[i] - 1;
    }
    return *max_element(p.begin(), p.end()) - 1;
}
string s;
namespace Solution {
    inline void Main() {
        cin >> s;
        cout << Manacher(s);
    }
}
signed main() {
    int T = 1;
    // cin >> T;
    while (T--) Solution::Main();
    return 0;
}

AC自动机

P3808 AC 自动机(简单版)

C++
#include <bits/stdc++.h>

using namespace std;

class ACAM {
private:
    struct Node {
        int nex[26];
        int fail;
        int cnt;
        Node() {
            memset(nex, -1, sizeof(nex));
            fail = cnt = 0;
        }
    };
    vector <Node> trie;
    inline int newNode() {
        trie.emplace_back();
        return trie.size() - 1;
    }
public:
    ACAM() {
        newNode();
    }
    inline void insert(string& str) {
        int u = 0;
        for (char ch : str) {
            int i = ch - 'a';
            if (trie[u].nex[i] == -1) trie[u].nex[i] = newNode();
            u = trie[u].nex[i];
        }
        trie[u].cnt++;
    }
    inline void build() {
        queue <int> q;
        for (int i = 0; i < 26; i++) {
            int v = trie[0].nex[i];
            if (v != -1) trie[v].fail = 0, q.push(v);
            else trie[0].nex[i] = 0;
        }
        while (!q.empty()) {
            int u = q.front();
            q.pop();
            for (int i = 0; i < 26; i++) {
                int v = trie[u].nex[i];
                if (v != -1) trie[v].fail = trie[trie[u].fail].nex[i], q.push(v);
                else trie[u].nex[i] = trie[trie[u].fail].nex[i];
            }
        }
    }
    inline int query(string& str) {
        int u = 0, res = 0;
        for (char ch : str) {
            int c = ch - 'a';
            u = trie[u].nex[c];
            for (int j = u; j && trie[j].cnt != -1; j = trie[j].fail) {
                res += trie[j].cnt;
                trie[j].cnt = -1;
            }
        }
        return res;
    }
};
ACAM ac;
int n;
string s;
namespace Solution {
    inline void Main() {
        cin >> n;
        for (int i = 1; i <= n; i++) {
            cin >> s;
            ac.insert(s);
        }
        ac.build();
        cin >> s;
        cout << ac.query(s);
    }
}
signed main() {
    int T = 1;
    // cin >> T;
    while (T--) Solution::Main();
    return 0;
}

P3796 AC 自动机(简单版 II)

C++
#include <bits/stdc++.h>

using namespace std;
const int N = 1e6 + 5;
class ACAM {
private:
    struct Node {
        int nex[26];
        int fail;
        int idx;
        Node() {
            memset(nex, -1, sizeof(nex));
            fail  = idx = 0;
        }
    };
    vector <Node> trie;
    inline int newNode() {
        trie.emplace_back();
        return trie.size() - 1;
    }
public:
    ACAM() {
        newNode();
    }
    inline void insert(string& str, int id) {
        int u = 0;
        for (char ch : str) {
            int i = ch - 'a';
            if (trie[u].nex[i] == -1) trie[u].nex[i] = newNode();
            u = trie[u].nex[i];
        }
        trie[u].idx = id;
    }
    inline void build() {
        queue <int> q;
        for (int i = 0; i < 26; i++) {
            int v = trie[0].nex[i];
            if (v != -1) trie[v].fail = 0, q.push(v);
            else trie[0].nex[i] = 0;
        }
        while (!q.empty()) {
            int u = q.front();
            q.pop();
            for (int i = 0; i < 26; i++) {
                int v = trie[u].nex[i];
                if (v != -1) trie[v].fail = trie[trie[u].fail].nex[i], q.push(v);
                else trie[u].nex[i] = trie[trie[u].fail].nex[i];
            }
        }
    }
    inline void query(string& str, vector <int>& tot) {
        int u = 0, res = 0;
        for (char ch : str) {
            int c = ch - 'a';
            u = trie[u].nex[c];
            for (int j = u; j > 0; j = trie[j].fail) {
                if (trie[j].idx > 0) tot[trie[j].idx]++;
            }
        }
    }
    inline void clear() {
        trie.clear();
        newNode();
    }
};
ACAM ac;
int n;
string words[N];
vector <int> ans(N);
namespace Solution {
    inline void Main() {
        while (cin >> n) {
            if (n == 0) return;
            ac.clear();
            fill(ans.begin(), ans.begin() + 1 + n, 0);
            for (int i = 1; i <= n; i++) {
                cin >> words[i];
                ac.insert(words[i], i);
            }
            ac.build();
            string text;
            cin >> text;
            ac.query(text, ans);
            int mx = 0;
            for (int i = 1; i <= n; i++) {
                if (ans[i] > mx) mx = ans[i];
            }
            cout << mx << '\n';
            for (int i = 1; i <= n; i++) {
                if (ans[i] == mx) cout << words[i] << '\n';
            }
        }
    }
}
signed main() {
    int T = 1;
    // cin >> T;
    while (T--) Solution::Main();
    return 0;
}

P5357 【模板】AC 自动机

C++
#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 5;
class ACAM {
private:
    struct Node {
        int nex[26];
        int fail;
        int idx;
        Node() {
            memset(nex, -1, sizeof(nex));
            fail = idx = 0;
        }
    };
    vector <Node> trie;
    vector <int> vis;
    vector <int> order;
    inline int newNode() {
        trie.emplace_back();
        vis.push_back(0);
        return trie.size() - 1;
    }
public:
    ACAM() {
        newNode();
    }
    inline void insert(string& str, int id) {
        int u = 0;
        for (char ch : str) {
            int c = ch - 'a';
            if (trie[u].nex[c] == -1) trie[u].nex[c] = newNode();
            u = trie[u].nex[c];
        }
        trie[u].idx = id;
    }
    inline void build() {
        queue <int> q;
        for (int c = 0; c < 26; c++) {
            int v = trie[0].nex[c];
            if (v != -1) trie[v].fail = 0, q.push(v);
            else trie[0].nex[c] = 0;
        }
        while (!q.empty()) {
            int u = q.front();
            order.push_back(u);
            q.pop();
            for (int c = 0; c < 26; c++) {
                int v = trie[u].nex[c];
                if (v != -1) trie[v].fail = trie[trie[u].fail].nex[c], q.push(v);
                else trie[u].nex[c] = trie[trie[u].fail].nex[c];
            }
        }
    }
    inline void query(string& text) {
        int u = 0;
        for (char ch : text) {
            int c = ch - 'a';
            u = trie[u].nex[c];
            vis[u]++;
        }
    }
    inline vector<int> get_result(int mx) {
        for (int i = order.size() - 1; i >= 0; i--) {
            int u = order[i];
            vis[trie[u].fail] += vis[u];
        }
        vector <int> cnt(mx + 1, 0);
        for (int u = 0; u < (int)trie.size(); u++) {
            if (trie[u].idx != 0) {
                cnt[trie[u].idx] = vis[u];
            }
        }
        return cnt;
    }
};
ACAM ac;
int n;
int tot;
int idx[N];
map<string, int> mp;
vector <int> ans(N);
string pat[N];
string text;
namespace Solution {
    inline void Main() {
        cin >> n;
        for (int i = 1; i <= n; i++) {
            cin >> pat[i];
            if (mp.find(pat[i]) == mp.end()) {
                mp[pat[i]] = ++tot;
            }
            idx[i] = mp[pat[i]];
            ac.insert(pat[i], idx[i]);
        }
        ac.build();
        cin >> text;
        ac.query(text);
        ans = ac.get_result(tot);
        for (int i = 1; i <= n; i++) cout << ans[idx[i]] << '\n';
    }
}
signed main() {
    int T = 1;
    // cin >> T;
    while (T--) Solution::Main();
    return 0;
}

后缀数组

后缀自动机

Lyndon 分解

C++
inline vector<string> Duval(string s) {
    int n = s.size();
    vector <string> res;
    int i = 0, j, k;
    while (i < n) {
        j = i + 1, k = i;
        while (j < n && s[k] <= s[j]) {
            if (s[k] < s[j]) k = i;
            else k++;
            j++;
        }
        int len = j - k;
        while (i <= k) {
            res.push_back(s.substr(i, len));
            i += len;
        }
    }
    return res;
}