-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringHashing.cpp
87 lines (77 loc) · 1.97 KB
/
StringHashing.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<bits/stdc++.h>
using namespace std;
const int N = 4000;
const int base = 31;
const int mod = 1e9 + 7;
class ModArithmetic{
public:
static int add(int a,int b,int mod){
int res = (a + b) % mod;
if(res < 0)
res += mod;
return res;
}
static int mult(int a,int b,int mod){
int res = (a * 1LL * b) % mod;
if(res < 0)
res += mod;
return res;
}
static int pow(int a,int b,int mod){
int res = 1;
while(b){
if(b&1){
res = mult(res,a,mod);
}
a = mult(a,a,mod);
b /= 2;
}
return res;
}
};
class StringHash{
string str;
int pw[N],inv[N],hash[N];
public:
StringHash(string str):str(str){
precompute();
build();
}
void precompute(){
pw[0] = 1;
// cout << "printing pw[i] .....\n";
for(int i=1;i<N;i++){
pw[i] = ModArithmetic::mult(base,pw[i-1],mod);
// cout << pw[i] <<" ";
}
cout << "\n";
int base_inv = ModArithmetic::pow(base,mod-2,mod);
inv[0] = 1;
// cout << "printing inv[i] .....\n";
for(int i=1;i<N;i++){
inv[i] = ModArithmetic::mult(inv[i-1],base_inv,mod);
// cout << inv[i] << " ";
}
}
void build(){
int n = str.length();
for(int i=0;i<n;i++){
int prev_hash = (i==0? 0 : hash[i-1]);
hash[i] = ModArithmetic::add(prev_hash,ModArithmetic::mult(str[i]-'a'+1,pw[i],mod),mod);
}
cout << "len => "<< n <<"\n";
}
int getHash(int i,int j){
if(i == 0)
return hash[j];
int res = ModArithmetic::add(hash[j],-hash[i-1],mod);
res = ModArithmetic::mult(res,inv[i],mod);
return res;
}
};
int main() {
string s1 = "abcdebcdf";
StringHash stringHash(s1);
cout << stringHash.getHash(1,3) <<"," <<stringHash.getHash(5,7) <<"\n";
return 0;
}