forked from Ashishgup1/Competitive-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashing (Strings).cpp
More file actions
36 lines (31 loc) · 835 Bytes
/
Hashing (Strings).cpp
File metadata and controls
36 lines (31 loc) · 835 Bytes
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
struct Hashs
{
vector<int> hashs;
vector<int> pows;
int P;
int MOD;
Hashs() {}
Hashs(string &s, int P, int MOD) : P(P), MOD(MOD)
{
int n = s.size();
pows.resize(n + 1, 0);
hashs.resize(n + 1, 0);
pows[0] = 1;
for(int i = n - 1; i >= 0; i--)
{
hashs[i] = (1LL * hashs[i + 1] * P + s[i] - 'a' + 1) % MOD;
pows[n - i] = (1LL * pows[n - i - 1] * P) % MOD;
}
pows[n] = (1LL * pows[n - 1] * P) % MOD;
}
int get_hash(int l, int r)
{
int ans = hashs[l] + MOD - (1LL * hashs[r + 1] * pows[r - l + 1]) % MOD;
ans %= MOD;
return ans;
}
};
//Problem 1: https://codeforces.com/contest/633/problem/C
//Solution 1: https://codeforces.com/contest/633/submission/42330829
//Problem 2: https://atcoder.jp/contests/abc141/tasks/abc141_e
//Solution 2: https://atcoder.jp/contests/abc141/submissions/7523234