-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy path1044. Longest Duplicate Substring.cpp
278 lines (231 loc) · 8.23 KB
/
1044. Longest Duplicate Substring.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//rolling hash + binary search
//WA(something to do with overflow?)
//14 / 16 test cases passed.
class Solution {
public:
int modPower(int x, unsigned int y, unsigned int m){
if (y == 0)
return 1;
long long p = modPower(x, y/2, m) % m;
p = (p * p) % m;
return (y%2 == 0)? p : (x * p) % m;
};
// Function to return gcd of a and b
int gcd(int a, int b){
if (a == 0)
return b;
return gcd(b%a, a);
};
// Function to find modular inverse of a under modulo m
// Assumption: m is prime
int modInverse(int a, int m){
int g = gcd(a, m);
if (g != 1){
return -1;
}else{
// If a and m are relatively prime, then modulo inverse
// is a^(m-2) mode m
return modPower(a, m-2, m);
}
};
int modMultiply(int a, int b, int mod) {
int res = 0; // Initialize result
// Update a if it is more than
// or equal to mod
a %= mod;
while (b){
/*
If b is even then
a * b = 2 * a * (b / 2),
otherwise
a * b = a + a * (b - 1)
*/
// If b is odd, add 'a' to result
if(b & 1){
res = (res + a) % mod;
}
// Here we assume that doing 2*a
// doesn't cause overflow
a = (2 * a) % mod;
b >>= 1; // b = b / 2
}
return res;
};
string longestDupSubstring(string S) {
int n = S.size();
int p = 26;
// int MOD = 1e9 + 7; //slower
int MOD = 19260817;
int pInv = modInverse(p, MOD);
//binary search to find right boundary
int left = 0, right = n-1;
int len;
string ans;
vector<int> powers(n);
powers[0] = 1;
for(int i = 1; i < n; ++i){
powers[i] = modMultiply(powers[i-1], p, MOD);
}
while(left <= right){
//mid
len = left + (right-left)/2;
if(len == 0) break;
// cout << left << ", " << len << ", " << right << endl;
//hash value -> start indices
unordered_map<long long, vector<int>> hashs;
long long hash = 0, power = 1;
for(int i = 0; i < len; ++i){
// hash += (S[i]-'a')* power;
// power = (power * p) % MOD;
hash = (hash + modMultiply(S[i]-'a', powers[i], MOD)) % MOD;
}
//p^(n-1)
// power = (power * pInv) % MOD;
hashs[hash].push_back(0);
bool valid = false;
for(int start = 1; start+len-1 < n; ++start){
hash = (hash - (S[start-1]-'a')) % MOD;
hash = modMultiply(hash, pInv, MOD);
hash = (hash + modMultiply(S[start+len-1]-'a', powers[len-1], MOD) % MOD);
if(hashs.find(hash) != hashs.end()){
for(int& matched_start : hashs[hash]){
if(strcmp((S.substr(matched_start, len)).data(), S.substr(start, len).data()) == 0){
//slower
// if(S.substr(matched_start, len) == S.substr(start, len)){
valid = true;
if(len > ans.size())
ans = S.substr(start, len);
break;
}
}
}
if(valid) break;
hashs[hash].push_back(start);
// for(auto it = hashs.begin(); it != hashs.end(); ++it){
// cout << it->first << " ";
// }
// cout << endl;
}
if(valid){
left = len+1;
}else{
right = len-1;
}
}
if(right < 0) return "";
return ans;
}
};
//different formula of rolling hash, S[0] has largest multiplier
//https://leetcode.com/problems/longest-duplicate-substring/discuss/291048/C%2B%2B-solution-using-Rabin-Karp-and-binary-search-with-detailed-explaination
//Runtime: 3132 ms, faster than 6.80% of C++ online submissions for Longest Duplicate Substring.
//Memory Usage: 442.3 MB, less than 10.46% of C++ online submissions for Longest Duplicate Substring.
class Solution {
public:
int modPower(int x, unsigned int y, unsigned int m){
if (y == 0)
return 1;
long long p = modPower(x, y/2, m) % m;
p = (p * p) % m;
return (y%2 == 0)? p : (x * p) % m;
};
// Function to return gcd of a and b
int gcd(int a, int b){
if (a == 0)
return b;
return gcd(b%a, a);
};
// Function to find modular inverse of a under modulo m
// Assumption: m is prime
int modInverse(int a, int m){
int g = gcd(a, m);
if (g != 1){
return -1;
}else{
// If a and m are relatively prime, then modulo inverse
// is a^(m-2) mode m
return modPower(a, m-2, m);
}
};
int modMultiply(int a, int b, int mod) {
int res = 0; // Initialize result
// Update a if it is more than
// or equal to mod
a %= mod;
while (b){
/*
If b is even then
a * b = 2 * a * (b / 2),
otherwise
a * b = a + a * (b - 1)
*/
// If b is odd, add 'a' to result
if(b & 1){
res = (res + a) % mod;
}
// Here we assume that doing 2*a
// doesn't cause overflow
a = (2 * a) % mod;
b >>= 1; // b = b / 2
}
return res;
};
string longestDupSubstring(string S) {
int n = S.size();
int p = 26;
//some large primes: 1e9+7, 19260817, 99999989
// int MOD = 1e9 + 7; //slower
int MOD = 19260817;
int pInv = modInverse(p, MOD);
//binary search to find right boundary
int left = 0, right = n-1;
int len;
string ans;
vector<int> powers(n);
powers[0] = 1;
for(int i = 1; i < n; ++i){
powers[i] = modMultiply(powers[i-1], p, MOD);
}
while(left <= right){
//mid
len = left + (right-left)/2;
if(len == 0) break;
// cout << left << ", " << len << ", " << right << endl;
//hash value -> start indices
unordered_map<long long, vector<int>> hashs;
long long hash = 0, power = 1;
for(int i = 0; i < len; ++i){
hash = (modMultiply(hash, p, MOD) + (S[i]-'a')) % MOD;
}
hashs[hash].push_back(0);
bool valid = false;
for(int start = 1; start+len-1 < n; ++start){
hash = (hash - modMultiply(S[start-1]-'a', powers[len-1], MOD) + MOD) % MOD;
hash = (modMultiply(hash, p, MOD) + (S[start+len-1]-'a')) % MOD;
if(hashs.find(hash) != hashs.end()){
for(int& matched_start : hashs[hash]){
if(strcmp((S.substr(matched_start, len)).data(), S.substr(start, len).data()) == 0){
valid = true;
if(len > ans.size())
ans = S.substr(start, len);
break;
}
}
}
if(valid) break;
hashs[hash].push_back(start);
// for(auto it = hashs.begin(); it != hashs.end(); ++it){
// cout << it->first << " ";
// }
// cout << endl;
}
if(valid){
left = len+1;
}else{
right = len-1;
}
}
if(right < 0) return "";
return ans;
}
};