-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhasher.cpp
More file actions
35 lines (34 loc) · 1.1 KB
/
hasher.cpp
File metadata and controls
35 lines (34 loc) · 1.1 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
// String Hasher
template <int B, int MOD>
struct Hasher {
// Static hash function
template <typename iter_type>
static int hash(iter_type begin, iter_type end) {
int h = 0;
for (iter_type it = begin; it != end; ++it)
h = (1LL * h * B + *it) % MOD;
return h;
}
// Data members
vector <int> pows, h;
// Constructor
template <typename iter_type>
Hasher(iter_type begin, iter_type end) {
const int sz = end - begin + 1;
pows.resize(sz), h.resize(sz);
pows[0] = 1, h[0] = 0;
for (iter_type it = begin; it != end; ++it) {
pows[it - begin + 1] = (1LL * pows[it - begin] * B) % MOD;
h[it - begin + 1] = (1LL * h[it - begin] * B + (*it)) % MOD;
}
}
// Continuous subsequence hash query[l, r]
int hash(const int l, const int r) {
int res = (h[r + 1] - 1LL * h[l] * pows[r - l + 1]) % MOD;
if (res < 0)
res += MOD;
return res;
}
};
typedef Hasher <263, 1000000000 + 7> H1;
typedef Hasher <277, 1000000000 + 9> H2;