forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkmp.py
More file actions
21 lines (18 loc) · 623 Bytes
/
kmp.py
File metadata and controls
21 lines (18 loc) · 623 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python2.7
def get_kmp(xs):
res = [-1, 0]
cur = 0
for i in range(2, len(xs) + 1):
while cur >= 0 and xs[cur] != xs[i - 1]:
cur = res[cur]
cur += 1
res.append(cur)
return res
def find_pattern(text, pattern):
kmp = get_kmp(pattern + ' ' + text)
return [i for i in range(len(text) - len(pattern) + 1)
if kmp[i + len(pattern) * 2 + 1] == len(pattern)]
if __name__ == '__main__':
text = raw_input("Input text: ")
pattern = raw_input("Input pattern: ")
print "Pattern appears at starting indices: ", find_pattern(text, pattern)