-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstringhash.cpp
More file actions
46 lines (44 loc) · 940 Bytes
/
stringhash.cpp
File metadata and controls
46 lines (44 loc) · 940 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
37
38
39
40
41
42
43
44
45
46
// String Hashing (rolling hash)
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
const int mod = 1e9 + 7;
long long mul = 114115;
long long h[MAXN];
long long p[MAXN];
void build(string s){
long long cur = 0;
long long now = mul;
p[0] = 1;
for (int i = 1 ; i <= s.size() ; i++){
p[i] = now;
now *= mul;
now %= mod;
}
for (int i = 1 ; i <= (int)s.size() ; i ++){
cur *= mul;
cur += s[i-1];
cur %= mod;
h[i] = cur;
}
}
long long query(int l,int r){
long long ret = (h[r] - (h[l-1] * p[r - l + 1] % mod) + mod) % mod;
return ret;
}
int main() {
// string hash
string s;
cin >> s;
build(s);
s = " " + s;
int l1, r1, l2, r2;
cin >> l1 >> r1 >> l2 >> r2;
// string 1-based
if (query(l1, r1) == query(l2, r2)) {
cout << "SAME\n";
}
else {
cout << "DIFFERENT\n";
}
}