-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmp_match.py
47 lines (36 loc) · 1014 Bytes
/
kmp_match.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
from border_array_match import construct_border_array
from utils import gen_rand_str
import time
import os
def search_kmp(x, p):
m = len(p)
matches = []
# preprocessing
border_array = construct_border_array(p)
prime_array = [0 if j == 0 else border_array[j - 1] + 1 for j in range(m + 1)]
# main
i = 0
j = 0
while i <= len(x) - m + j:
i, j = match(i, j, m, x, p)
if j == m:
matches.append(i - m)
if j == 0:
i = i + 1
else:
j = prime_array[j] - 1
return matches
def match(i, j, m, x, p):
while j < m and x[i] == p[j]:
i = i + 1
j = j + 1
return i, j
def main():
x = "A" * (10000 - 1) + "B" + "A" * (10000 - 1) + "B" + "A" * (10000 - 1) + "B"
p = "A" * 10000
t = time.process_time()
matches = search_kmp(x, p)
elapsed_time = time.process_time() - t
print("time =", elapsed_time, flush=True)
if __name__ == "__main__":
main()