-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmp.py
44 lines (37 loc) · 829 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
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 25 2017
@author: heshenghuan ([email protected])
http://github.com/heshenghuan
"""
def next(p):
m = len(p)
pi = [0] * m
k = 0
for q in range(1, m):
while k > 0 and p[k] != p[q]:
k = pi[k - 1]
if p[k] == p[q]:
k = k + 1
pi[q] = k
return pi
def KMP(t, p):
n = len(t)
m = len(p)
pi = next(p)
q = 0
for i in range(n):
while q > 0 and p[q] != t[i]:
q = pi[q - 1]
if p[q] == t[i]:
q = q + 1
if q == m:
return True # i - m + 1
return False # -1
if __name__ == "__main__":
a = [1, 0, 10]
b = [1, 0, 1]
# a = "a b a c a a b a c a b a c a b a a b b"
# b = "a b a c a b"
print KMP(a, b)