-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy path1044. Longest Duplicate Substring.js
More file actions
47 lines (44 loc) · 1.16 KB
/
1044. Longest Duplicate Substring.js
File metadata and controls
47 lines (44 loc) · 1.16 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
/**
* @param {string} s
* @return {string}
*/
const longestDupSubstring = function (s) {
const n = s.length
s = ' ' + s
const P = 131n
// 这题数据有点强,被迫用较大的 Q (1 << 64 等价于 unsigned long long 的取余特性)
const Q = 1n << 64n
const f = [0n]; const p = [1n]
for (let i = 1; i <= n; i++) {
f[i] = (f[i - 1] * P % Q + (BigInt(s[i].charCodeAt(0) - 'a'.charCodeAt(0)))) % Q
p[i] = p[i - 1] * P % Q
}
// console.log(f, p)
const hash = (i, j) => {
// 乘完之后的取模非常重要,漏掉就会 WA(出现较小负数导致 + 一个 Q 不够)
return (f[j] - f[i - 1] * p[j - i + 1] % Q + Q) % Q
}
const len2Idx = {}
let l = 1; let r = n
while (l < r) {
const mid = l + r + 1 >> 1
if (valid(mid)) l = mid
else r = mid - 1
}
if (valid(l)) return s.slice(len2Idx[l], len2Idx[l] + l)
return ''
function valid (mid) {
const set = new Set()
// 滑窗 [i, i+mid-1]
for (let i = 1; i + mid - 1 <= n; i++) {
const j = i + mid - 1
const h = hash(i, j)
if (set.has(h)) {
len2Idx[mid] = i
return true
}
set.add(h)
}
return false
}
}