KMP算法:
'''
i
a b c a b d
next 0 0 0 1 2 0
t
'''
def prefix_table(pattern):
next = [0] * len(pattern)
####t与i的初始位置
i = 1
t = 0
while i < len(pattern):
if pattern[i] == pattern[t]:
next[i] = t + 1
i += 1
t += 1
elif t > 0: # 这个地方最难记,把t退回到next[t-1]位置
t = next[t - 1]
else: # t == 0
next[i] = 0
i += 1
return next
def move_prefix_table(prefix, n):
for i in range(n-1, 0, -1):
prefix[i] = prefix[i-1]
prefix[0] = -1
return prefix
def kmp_search(text, pattern):
next = prefix_table(pattern)
next = move_prefix_table(next, len(next))
m = len(text)
n = len(pattern)
i = 0
j = 0
while i < m:
if j == n-1 and text[i] == pattern[j]:
print("Found at ",(i-j))
break
j = next[j]
if text[i] == pattern[j]:
i += 1
j += 1
else:
j = next[j]
if j == -1:
i += 1
j += 1
pattern = ['A','B','A','B','C','A','B','A','A']
text = ['A','B','A','B','A','B','C','A','B','A','A','B','A','B','A','B','A','B']
kmp_search(text, pattern)
不怎么理解t = next[t-1]是什么意思?
在执行到这个语句时说明匹配失败了,而不必要重头在来,找到当前未匹配的字符前面一个匹配字符的位置进行下一轮匹配。