-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hash.cpp
243 lines (199 loc) · 5.54 KB
/
Hash.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
template <unsigned long long MOD>
class ModInteger {
private:
unsigned long long value;
public:
ModInteger() {
this->value = 0;
}
ModInteger(const unsigned long long& arg) {
this->value = arg % MOD;
}
unsigned long long getllu() const {
return this->value;
}
long long getlld() const {
return this->value;
}
unsigned int getu() const {
return this->value;
}
int getd() const {
return this->value;
}
bool operator == (const ModInteger& arg) const {
return this->value == arg.value;
}
bool operator != (const ModInteger& arg) const {
return this->value != arg.value;
}
ModInteger operator + (const ModInteger& arg) const {
return ModInteger(this->value + arg.value);
}
ModInteger& operator += (const ModInteger& arg) {
this->value = (this->value + arg.value) % MOD;
return *this;
}
ModInteger operator - () const {
return ModInteger(MOD - this->value);
}
ModInteger operator - (const ModInteger& arg) const {
//return ModInteger(this->value + (-arg).value);
return ModInteger(this->value + (MOD - arg.value));
}
ModInteger& operator -= (const ModInteger& arg) {
//this->value = (this->value + (-arg).value) % MOD;
this->value = (this->value + (MOD - arg.value)) % MOD;
return *this;
}
ModInteger operator * (const ModInteger& arg) const {
return ModInteger(this->value * arg.value);
}
ModInteger& operator *= (const ModInteger& arg) {
this->value = (this->value * arg.value) % MOD;
return *this;
}
ModInteger pow(unsigned int exp) const {
unsigned long long answer = 1;
unsigned long long power = this->value;
for(; exp > 0; exp >>= 1) {
if ((exp & 1) > 0) {
answer = (answer * power) % MOD;
}
power = (power * power) % MOD;
}
return answer;
}
ModInteger inverse() const {
return this->pow(MOD - 2);
}
ModInteger operator / (const ModInteger& arg) const {
return ModInteger(this->value * arg.inverse().hash);
}
ModInteger& operator /= (const ModInteger& arg) {
this->value = (this->value * arg.inverse().hash) % MOD;
return *this;
}
};
class Hash {
private:
ModInteger<1000000007> h1;
ModInteger<1000000009> h2;
ModInteger<1000000011> h3;
public:
Hash() {
this->h1 = 0;
this->h2 = 0;
this->h3 = 0;
}
Hash(int value) {
this->h1 = value;
this->h2 = value;
this->h3 = value;
}
Hash pow(unsigned int exp) const {
Hash answer = *this;
answer.h1 = answer.h1.pow(exp);
answer.h2 = answer.h2.pow(exp);
answer.h3 = answer.h3.pow(exp);
return answer;
}
Hash operator * (const Hash& arg) const {
Hash answer = *this;
answer.h1 = this->h1 * arg.h1;
answer.h2 = this->h2 * arg.h2;
answer.h3 = this->h3 * arg.h3;
return answer;
}
Hash operator *= (const Hash& arg) {
this->h1 *= arg.h1;
this->h2 *= arg.h2;
this->h3 *= arg.h3;
return *this;
}
Hash operator + (const Hash& arg) const {
Hash answer = *this;
answer.h1 = this->h1 + arg.h1;
answer.h2 = this->h2 + arg.h2;
answer.h3 = this->h3 + arg.h3;
return answer;
}
Hash operator += (const Hash& arg) {
this->h1 += arg.h1;
this->h2 += arg.h2;
this->h3 += arg.h3;
return *this;
}
Hash operator - (const Hash& arg) const {
Hash answer = *this;
answer.h1 = this->h1 - arg.h1;
answer.h2 = this->h2 - arg.h2;
answer.h3 = this->h3 - arg.h3;
return answer;
}
Hash operator -= (const Hash& arg) {
this->h1 -= arg.h1;
this->h2 -= arg.h2;
this->h3 -= arg.h3;
return *this;
}
bool operator < (const Hash &arg) const {
return this->h1.getllu() < arg.h1.getllu() ||
(this->h1.getllu() == arg.h1.getllu() && this->h2.getllu() < arg.h2.getllu()) ||
(this->h1.getllu() == arg.h1.getllu() && this->h2.getllu() == arg.h2.getllu() && this->h3.getllu() < arg.h3.getllu());
}
bool operator == (const Hash &arg) const {
return this->h1 == arg.h1 && this->h2 == arg.h2 && this->h3 == arg.h3;
}
void dump() {
printf("(%llu,%llu,%llu)", this->h1.getllu(), this->h2.getllu(), this->h3.getllu());
}
};
template<int SIGMA = 26, char FIRST_LETTER = 'a'>
class RollingHash {
private:
Hash hash;
Hash base;
public:
RollingHash() {
this->hash = 0;
this->base = 0;
}
void clear() {
this->hash = 0;
this->base = 0;
}
void add(char letter) {
this->hash = (this->hash * SIGMA) + (letter - FIRST_LETTER);
if (this->base == 0) {
this->base = 1;
} else {
this->base *= SIGMA;
}
}
void roll(char newLetter, char oldLetter) {
this->hash = (this->hash - (this->base * (oldLetter - FIRST_LETTER))) * SIGMA + (newLetter - FIRST_LETTER);
}
Hash getHash() const {
return this->hash;
}
};
class HashWithID {
public:
int id;
Hash hash;
HashWithID(Hash hash, int id) {
this->hash = hash;
this->id = id;
}
bool operator < (const HashWithID &arg) const {
return this->hash < arg.hash;
}
bool operator == (const HashWithID &arg) const {
return this->hash == arg.hash;
}
};