跳到正文

算法·理论

串串学习笔记

Ryc12345678910约 8 分钟
原文

这是 2026年5月19日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年5月19日
更新
2026年5月24日
篇幅
约 8 分钟

版本

  1. v3当前
  2. v2
  3. v1

串串学习笔记

哈希

定义

一个把字符串映射到整数的函数 ff,这个函数 ff 被称为哈希函数。

例题与示例代码

1. 字符串哈希
1. 单哈希
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;
}
2. 双哈希
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;
}
2. 字符串哈希plus

增强了数据强度。单哈希可能过不去(我没试过)双哈希轻松过去。

C++
#include <bits/stdc++.h>
using namespace std;
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;
}
const int N = 1e6 + 5;
int n;
string a[N];
namespace Solution {
    inline void Main() {
        cin >> n;
        for (int i = 1;i <= n;i++) cin >> a[i];
        sort(a + 1, a + 1 + n);
        int ans = 1;
        for (int i = 1;i < n;i++) {
            if (compare(a[i], a[i + 1])) ans++;
        }
        cout << ans;
    }
}
signed main() {
    int T = 1;
    while (T--) Solution::Main();
    return 0;
}

字典树

Code

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;

例题一 于是他错误地点名开始了

直接套模版就 AC 了。没什么好说的。

C++
namespace Solution {
    inline void Main() {
        cin >> n;
        for (int i = 1;i <= n;i++) {
            string s;
            cin >> s;
            trie.insert(s, (int)s.size());
        }
        cin >> m;
        while (m--) {
            string s;
            cin >> s;
            int ans = trie.find(s, (int)s.size());
            if (ans == 0) cout << "WRONG\n";
            else if (!mp[s]) mp[s] = 1, cout << "OK\n";
            else cout << "REPEAT\n";
        }
    }
}