-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCOCI '06 Contest 5 #6 Dvaput.py
62 lines (48 loc) · 1.46 KB
/
COCI '06 Contest 5 #6 Dvaput.py
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
# TLE, PYTHON IS TOO SLOW, CHECK C++ CODE
# https://dmoj.ca/problem/coci06c5p6
# Hashing
from itertools import accumulate
N = int(input())
str1 = list(map(lambda x: ord(x) - 97, input())) # lower case alphabet
# - hash start -
# MOD = 177635683940025046467781066894531
MOD = 2147483647 # 2^31 - 1
MOD2 = 2147483629 # double hashing
p = 29
power = [0] * N # precompute powers of `p`, with MOD
power[0] = 1
for i in range(1, N):
power[i] = (power[i - 1] * p) % MOD
power2 = [0] * N # precompute powers of `p`, with MOD2
power2[0] = 1
for i in range(1, N):
power2[i] = (power2[i - 1] * p) % MOD2
hash1 = [0] * N # precompute hashes of each character in `str1`
for i in range(N):
hash1[i] = (str1[i] * power[N - i - 1]) % MOD
psa1 = [0] + list(accumulate(hash1)) # psa for range hash query
hash2 = [0] * N # double hash
for i in range(N):
hash2[i] = (str1[i] * power2[N - i - 1]) % MOD2
psa2 = [0] + list(accumulate(hash2))
# - hash end -
def works(le):
seen = set()
for i in range(N - le + 1):
# get both hashes
hash_single = (psa1[i + le] - psa1[i]) * power[i] % MOD # shift up
hash_double = (psa2[i + le] - psa2[i]) * power2[i] % MOD2 # shift up
p = (hash_single, hash_double)
if p in seen:
return True
seen.add(p)
return False
low = 0
high = N
while low <= high:
mid = low + (high - low) // 2
if works(mid):
low = mid + 1
else:
high = mid - 1
print(low - 1)