-
Notifications
You must be signed in to change notification settings - Fork 2
/
kmp.py
executable file
·34 lines (29 loc) · 873 Bytes
/
kmp.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
#!/usr/bin/env python3
class KMP:
def __init__(self, needle):
self.needle = needle
self.F = [0] * (len(needle) + 1)
i, j = 1, 0
while i < len(needle):
if needle[i] == needle[j]:
i += 1
j += 1
self.F[i] = j
elif j == 0:
i += 1
self.F[i] = 0
else:
j = self.F[j]
def find_at(self, haystack, start_index = 0):
i, j = start_index, 0
n, m = len(haystack), len(self.needle)
while i - j <= n - m:
while j < m:
if self.needle[j] != haystack[i]: break
i += 1
j += 1
if j == m: return i - m
if j == 0: i += 1
j = self.F[j]
kmp = KMP('abababc')
print(kmp.find_at('ababababababababc'))