Skip to content

Commit 3d02121

Browse files
committed
fix
1 parent d199219 commit 3d02121

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

library/strings/string_hash.hpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
#pragma once
2-
//! https://codeforces.com/blog/entry/4898#comment-157965
2+
//! https://codeforces.com/blog/entry/4898
3+
//! https://codeforces.com/blog/entry/60442
4+
//! https://codeforces.com/blog/entry/129538
5+
//! p = probability of collision ~= 1/((1e9+7)*(1e9+9))
6+
//! probability of collision over q comparisons ~=
7+
//! (1 - (1/p))^q ~= 1 - (1/p)^q
8+
//! checking if n hashes are pairwise distinct is the same
9+
//! as doing q = (n choose 2) ~= n^2 comparisons
310
//! - use random base to avoid getting hacked on codeforces
4-
//! - work mod 1e9+7 if birthday paradox is not a problem
511
//! @time O(n + q)
612
//! @space O(n)
7-
using mint = array<ll, 2>;
8-
constexpr mint mod = {ll(1e9 + 7), ll(1e9 + 9)};
13+
using hsh = array<ll, 2>;
14+
constexpr hsh mod = {ll(1e9 + 7), ll(1e9 + 9)};
915
const ll base = 239017;
1016
struct str_hash {
11-
vector<mint> ha, pw;
17+
vector<hsh> ha, pw;
1218
str_hash(const auto& s): ha(sz(s) + 1), pw(ha) {
1319
pw[0] = {1, 1};
1420
rep(i, 0, sz(s)) rep(j, 0, 2) {
1521
pw[i + 1][j] = pw[i][j] * base % mod[j];
1622
ha[i + 1][j] = (ha[i][j] * base + s[i] + 1) % mod[j];
1723
}
1824
}
19-
mint subarray(int l, int r) { // [l, r)
20-
mint res;
25+
hsh subarray(int l, int r) { // [l, r)
26+
hsh res;
2127
rep(j, 0, 2) {
2228
res[j] = ha[r][j] - ha[l][j] * pw[r - l][j] % mod[j];
2329
if (res[j] < 0) res[j] += mod[j];

0 commit comments

Comments
 (0)