-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodulo_hashing.cpp
107 lines (98 loc) · 2.42 KB
/
modulo_hashing.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <bits/stdc++.h>
// cerr << " Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << " ms \n " ;
using namespace std;
#define int long long
#define pi acos(-1)
#define all(x) x.begin(), x.end()
#define endl "\n"
#define debg cout << "yes" << endl;
#define fast ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define test int t;cin >> t; while (t--)
#define pb push_back
#define vec vector<int>
#define firs first
#define sec second
#define fo(i, n) for (int i = 0; i < n; i++)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define ln length()
#define spc(x) cout << x << " ";
#define ent(x) cout << x << "\n";
#define mem(x, v) memset(x, v, sizeof(x));
#define deb(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); debug(_it, args); }
void debug(istream_iterator<string> it) {}
template<typename T, typename... Args>
void debug(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; debug(++it, args...);}
int add(int a,int b,int mod)
{
int res=(a+b)%mod;
if(res<0)res+=mod;
return res;
}
int mult(int a,int b,int mod)
{
int res=(a*1LL*b)%mod;
if(res<0)res+=mod;
return res;
}
int power(int a,int b,int mod)
{
int res=1;
while(b){
if(b%2==1)res=mult(res,a,mod);
a=mult(a,a,mod);
b/=2;
}
return res;
}
int mod_inverse(int a,int mod)
{
return power(a,mod-2,mod);
}
int hashing(string s,int mod)
{
int res=0;
int pow=1;
int p=31;
for(char ch:s)
{
res=(res+((ch-'a'+1)*pow))%mod;
pow=(pow*p)%mod;
}
return res;
}
int Hash[1000000];
int inv[1000000];
void init_hashing(string s,int mod)
{
int p=31;
int pow=1;
inv[0]=1;
int pw_inv=power(p,mod-2,mod);
for(int i=1;i<s.ln;i++)
{
inv[i]=mult(inv[i-1],pw_inv,mod);
}
Hash[0]=(s[0]-'a'+1);
for(int i=1;i<s.ln;i++)
{
char ch=s[i];
pow=(pow*p)%mod;
Hash[i]=(Hash[i-1]+(ch-'a'+1)*pow)%mod;
}
}
int substring(int l,int r,int mod)
{
int res=Hash[r];
if(l>0)res-=Hash[l-1];
res=mult(res,inv[l],mod);
return res;
}
int32_t main()
{
string s="srasel";
init_hashing(s,1000000007);
string ss="s";
int res=hashing(ss,1000000007);
int res1=substring(0,0,1000000007);
deb(res,res1)
}