-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.cpp
More file actions
71 lines (60 loc) · 1.58 KB
/
hash.cpp
File metadata and controls
71 lines (60 loc) · 1.58 KB
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
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
struct Hash{
int n, mod = 1e9 + 7, P1 = 81, P2;
vector <ull> h1, h2;
vector <ull> deg1, deg2;
Hash(string & s){
n = s.size() + 1;
P2 = rand();
h1.resize(n), h2.resize(n);
deg1.resize(n), deg2.resize(n);
deg1[0] = deg2[0] = 1;
for(int i = 0; i < n - 1; i++){
deg1[i + 1] = deg1[i] * P1;
deg2[i + 1] = (deg2[i] * P2) % mod;
}
for(int i = 0; i < n - 1; i++){
h1[i + 1] = h1[i] * P1 + s[i];
h2[i + 1] = (h2[i] * P2 + s[i]) % mod;
}
}
ull get_hash1(int i, int j){
return h1[j + 1] - h1[i] * deg1[j - i + 1];
}
ull get_hash2(int i, int j){
return (h2[j + 1] - h2[i] * deg2[j - i + 1] + 3ll * mod) % mod;
}
bool cmp(Hash & a, int i, int j){
return (get_hash1(i, j) == a.get_hash1(i, j) && get_hash2(i, j) == a.get_hash2(i, j));
}
};
bool cmp(Hash & a, string & A, Hash & b, string & B){
if(A.size() > B.size() && a.cmp(b, 0, B.size() - 1)){
return 1;
}
else if(A.size() <= B.size() && b.cmp(a, 0, A.size() - 1)){
return 0;
}
int ln = 1;
while((1 << ln) <= min(A.size(), B.size())){
ln++;
}
int l = -1;
for(int i = ln; i >= 0; i--){
int j = (l + (1 << i));
if(j >= min(A.size(), B.size())){
continue;
}
if(a.cmp(b, 0, j)){
l = j;
}
}
return (A[l + 1] > B[l + 1]);
}
int main()
{
return 0;
}