不得不说,今日一题,属实太难了,我看了半天题解,总算是看明白了点,直接上代码
之后还得继续复习TODO
import random
class Solution:
def longestDupSubstring(self, s: str) -> str:
a1, a2 = random.randint(26, 100), random.randint(26, 100)
mod1, mod2 = random.randint(10 ** 9 + 7, 2 ** 31 - 1), random.randint(10 ** 9 + 7, 2 ** 31 - 1)
n = len(s)
# 编码方式,将小写字母转换成数字
arr = [ord(c) - ord('a') for c in s]
l, r = 1, n - 1
length, start = 0, -1
while l <= r:
m = l + (r - l + 1) // 2
idx = self.check(arr, m, a1, a2, mod1, mod2)
# 先从左半边开始进行编码,然后再对右边进行编码
if idx != -1:
l = m + 1
# 记录子字符串长度和下标
length = m
start = idx
else:
r = m - 1
return s[start:start + length] if start != -1 else ""
def check(self, arr, m, a1, a2, mod1, mod2):
n = len(arr)
aL1, aL2 = pow(a1, m, mod1), pow(a2, m, mod2)
h1, h2 = 0, 0
# 双重哈希
for i in range(m):
h1 = (h1 * a1 + arr[i]) % mod1
h2 = (h2 * a2 + arr[i]) % mod2
# 编码组合
seen = {(h1, h2)}
for start in range(1, n - m + 1):
h1 = (h1 * a1 - arr[start - 1] * aL1 + arr[start + m - 1]) % mod1
h2 = (h2 * a2 - arr[start - 1] * aL2 + arr[start + m - 1]) % mod2
if (h1, h2) in seen:
return start
seen.add((h1, h2))
return -1
# if __name__ == '__main__':
# print((1, 2) == (1, 2))