Repository navigation
Expand file tree
/
Copy pathStringHash_2Hash.cpp
More file actions
78 lines (68 loc) · 1.92 KB
/
Copy pathStringHash_2Hash.cpp
File metadata and controls
78 lines (68 loc) · 1.92 KB
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
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
const int MOD1 = 1e9 + 7, PRIME1 = 31;
const int MOD2 = 1e9 + 9, PRIME2 = 37;
vector<int> powersMOD1, powersInvMOD1;
vector<int> powersMOD2, powersInvMOD2;
inline int add(int a, int b, int mod) {
return (a + b) % mod;
}
inline int sub(int a, int b, int mod) {
return (a - b + mod) % mod;
}
inline int mul(int a, int b, int mod) {
return (ll) a * b % mod;
}
int power(int x, int p, int mod) {
int ret = 1;
while (p) {
if (p%2) {
ret = mul(ret, x, mod);
}
p /= 2;
x = mul(x, x, mod);
}
return ret;
}
int inv(int x, int mod) {
return power(x, mod - 2, mod);
}
int divide(int a, int b, int mod) {
return mul(a, inv(b, mod), mod);
}
void calcPowers() {
powersMOD1.push_back(1);
powersMOD2.push_back(1);
for (int i = 0; i < N; i++) {
powersMOD1.push_back(mul(powersMOD1.back(), PRIME1, MOD1));
powersMOD2.push_back(mul(powersMOD2.back(), PRIME2, MOD2));
}
for (auto x: powersMOD1) {
powersInvMOD1.push_back(inv(x, MOD1));
}
for (auto x: powersMOD2) {
powersInvMOD2.push_back(inv(x, MOD2));
}
}
vector<pair<int,int>> calcHash(const string&s) {
vector<pair<int, int>> ret(s.size());
for (int i = 0; i < (int) s.size(); i++) {
if (i > 0) {
ret[i] = ret[i - 1];
}
ret[i].first = add(ret[i].first, mul(s[i], powersMOD1[i], MOD1), MOD1);
ret[i].second = add(ret[i].second, mul(s[i], powersMOD2[i], MOD2), MOD2);
}
return ret;
}
pair<int,int> getHash(const vector<pair<int, int>> &hash, int l, int r) {
if (l == 0) {
return hash[r];
}
pair<int, int> ret;
ret.first = mul(sub(hash[r].first, hash[l - 1].first, MOD1), powersInvMOD1[l], MOD1);
ret.second = mul(sub(hash[r].second, hash[l - 1].second, MOD2), powersInvMOD2[l], MOD2);
return ret;
}