Skip to content

Commit

Permalink
Update Hashing (Strings).cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashishgup1 authored Jul 9, 2020
1 parent fec482e commit b3ab4f5
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions Hashing (Strings).cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@ struct 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.resize(n + 1, 0);
hashs.resize(n + 1, 0);
pows[0] = 1;
for(int i=n-1;i>=0;i--)
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;
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;
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;
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

0 comments on commit b3ab4f5

Please sign in to comment.