-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKMPSearch.py
40 lines (34 loc) · 1.11 KB
/
KMPSearch.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
def KMPStringSearch(text, tar):
'''
使用KMP算法实现字符串检索
:param text: 检索的文本
:param tar: 要检索的子字符串
:return: 返回一个list,为所有在text中检索到的目标子字符串的起点下标
'''
res = []
idx = 0
while idx <= (len(text) - len(tar)):
submatch = ''
for j in range(len(tar)):
if tar[j] != text[idx+j]:
mismatch = True
break
submatch += tar[j]
if j == 0:
idx += 1
else:
if len(submatch) == len(tar):
res.append(idx)
# 计算部分匹配
prefix = set()
suffix = set()
for i in range(len(submatch) - 1):
prefix.add(submatch[:-(i + 1)])
suffix.add(submatch[(i+1):])
temp = prefix.intersection(suffix)
sub_mat_len = 0
for s in temp:
sub_mat_len = max(len(s), sub_mat_len)
idx += j - sub_mat_len
return res
res = KMPStringSearch('BBC ABCDAB ABCDABCDABDE', 'ABCDABD')