大力出奇迹——字符串哈希
Apale 4/14/2019
什么是字符串哈希
字符串哈希,即把字符串转化为一个
哈希的过程
把字符串a看做一个base进制的数字, 则a的每个前缀的哈希值为
双哈希
取不同的base和mod,做两次哈希,得到两组值:
字符串哈希的用处
观察每个前缀串的哈希值可得:
例题
题目链接 (opens new window)
题意:两个串的最长公共子串
(听说后缀自动机能
解法:
预处理出a和b每个前缀的哈希值
因为最长公共子串的性质,存在长度为
#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
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