大力出奇迹——字符串哈希

4/14/2019

什么是字符串哈希

字符串哈希,即把字符串转化为一个basebase进制的数字

哈希的过程

把字符串a看做一个base进制的数字, 则a的每个前缀的哈希值为 hash[i]hash[i] == (hash[i1]base+a[i])%mod(hash[i-1] * base + a[i]) \% mod 其中,basebasemodmod一般取质数,可以减小冲突 把hashhash数组定义为无符号类型,可以不模,使其自然溢出 然鹅单哈希容易被卡,最好使用双哈希

双哈希

取不同的base和mod,做两次哈希,得到两组值:hash1[],hash2[]hash1[], hash2[] 此时原串的哈希值为一个pairpair 实测双哈希不会慢太多,可放心使用。

字符串哈希的用处

观察每个前缀串的哈希值可得: hash[l,r]=hash[r]has[l1]baserl+1hash[l, r] = hash[r] - has[l-1]*base^{r-l+1}(当hash不为无符号数时,减法后取模需%mod+mod)%mod\%mod + mod) \% mod) 于是可以O(1)O(1)求得任意子串的哈希值。 简单的说,字符串哈希可以在字符串匹配的过程中,把两个字符串的比较转化为两个数字的比较,在时间上消掉一个nn

例题

题目链接 (opens new window) 题意:两个串的最长公共子串 (听说后缀自动机能O(n)O(n)求。晚点再补两份后缀数组&&\&\&后缀自动机的代码。)

解法:

预处理出a和b每个前缀的哈希值 因为最长公共子串的性质,存在长度为mm的最长公共子串,就一定存在长度为m1m-1的最长公共子串。 所以可以二分答案 check的时候,把a长度为m的所有子串的哈希值排序,对b的每个长度为m的哈希值,在有序序列中二分查找,能找到则check函数返回1。 时间复杂度:check里面一次排序nlognnlogn, 二分答案lognlogn,一共nlog2nnlog^2n。 看到一种更好的做法 ,把check中排序+二分的操作换成哈希,可以再消掉一个loglog... 这里放一个双哈希的nlog2nnlog^2n的代码。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<ull, ull> puu;
const int base1 = 31;
const int base2 = 196613;

puu has1[100005], has2[100005];
ull p1[100005], p2[100005];

void Hash(const string &a, puu has[])
{
    has[0] = {a[0], a[0]};
    int len = a.size();
    for (int i = 1; i < len; ++i)
    {
        has[i].first = has[i - 1].first * base1 + a[i];
        has[i].second = has[i - 1].second * base2 + a[i];
    }
}

puu sub(int l, int r, puu has[])
{
    if (!l)
        return has[r];
    puu ans;
    ans.first = has[r].first - has[l - 1].first * p1[r - l + 1];
    ans.second = has[r].second - has[l - 1].second * p2[r - l + 1];
    return ans;
}

ull lena, lenb;
vector<puu> h;

bool ok(int n, int lena, int lenb)
{
    h.clear();
    for (int i = n - 1; i < lena; ++i)
		h.push_back(sub(i-n+1, i, has1));
    sort(h.begin(), h.end());
    puu t;
    for (int i = n - 1; i < lenb; ++i)
    {
        t = sub(i - n + 1, i, has2);
        if (binary_search(h.begin(), h.end(), t))
            return 1;
    }
    return 0;
}

string a, b;

int main()
{
    cin >> a >> b;
    Hash(a, has1);
    Hash(b, has2);
    p1[0] = p2[0] = 1;
    for (int i = 1; i <= 100005; ++i)
    {
        p1[i] = base1 * p1[i - 1];
        p2[i] = base2 * p2[i - 1];
    }
    int l = 1, r = min(a.size(), b.size()), m;
    while (l <= r)
    {
        m = l + r >> 1;
        if (ok(m, a.size(), b.size()))
            l = m + 1;
        else
            r = m - 1;
    }
    cout << r;
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Last Updated: 4/3/2022, 12:55:14 AM