1 Star 0 Fork 0

fanyangchu/PythonCode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
kmp_str_search.py 1.51 KB
一键复制 编辑 原始数据 按行查看 历史
Jérôme Krell 提交于 2019-10-10 14:22 . Reformat Code by PyCharm-Community
"""Author Anurag Kumar(mailto:anuragkumarak95@gmail.com)
The Knuth-Morris-Pratt Algorithm for finding a pattern within a piece of te$
with complexity O(n + m)
1) Preprocess pattern to identify any suffixes that are identical to prefix$
This tells us where to continue from if we get a mismatch between a cha$
and the text.
2) Step through the text one character at a time and compare it to a charac$
updating our location within the pattern if necessary
"""
def kmp(pattern, text, len_p=None, len_t=None):
# 1) Construct the failure array
failure = [0]
i = 0
for index, char in enumerate(pattern[1:]):
if pattern[i] == char:
i += 1
else:
i = 0
failure.append(i)
# 2) Step through text searching for pattern
i, j = 0, 0 # index into text, pattern
while i < len(text):
if pattern[j] == text[i]:
if j == (len(pattern) - 1):
return True
i += 1
j += 1
# if this is a prefix in our pattern
# just go back far enough to continue
elif failure[j] > 0:
j = failure[j] - 1
else:
i += 1
return False
if __name__ == '__main__':
# Test 1)
pattern = "abc1abc12"
text1 = "alskfjaldsabc1abc1abc12k23adsfabcabc"
text2 = "alskfjaldsk23adsfabcabc"
assert kmp(pattern, text1) and not kmp(pattern, text2)
# Test 2)
pattern = "ABABX"
text = "ABABZABABYABABX"
assert kmp(pattern, text)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/fanych/pythoncode.git
git@gitee.com:fanych/pythoncode.git
fanych
pythoncode
PythonCode
master

搜索帮助