Template
字符串双模哈希
Base | Mod |
---|---|
\(137\) | \(10^9+7\) |
\(139\) | \(10^9+9\) |
struct Hash {
ll base, mod, p[N], hs[N];
Hash(ll b, ll n, ll m) {
base = b, mod = m, p[0] = 1;
for (ll i = 1; i <= n; i++) p[i] = p[i - 1] * base % mod;
}
void make(string s) {
ll n = s.size();
s = '0' + s, hs[0] = 0;
for (ll i = 1; i <= n; i++) hs[i] = (hs[i - 1] * base % mod + s[i]) % mod;
}
ll get(ll l, ll r) {
return (hs[r] + mod - hs[l - 1] * p[r - l + 1] % mod) % mod;
}
} hs1(137, 1e6, 1e9 + 7), hs2(139, 1e6, 1e9 + 9);