Python KMP 算法 Next 数组实现
以下是 KMP 算法中 Next 数组的 Python 代码示例:
def getNext(p):
n = len(p)
next = [0] * n
j = 0
for i in range(1, n):
while j > 0 and p[i] != p[j]:
j = next[j-1]
if p[i] == p[j]:
j += 1
next[i] = j
return next
其中,参数 p 为模式串,该函数返回一个长度为模式串长度的 next 数组。
原文地址: https://www.cveoy.top/t/topic/oqGj 著作权归作者所有。请勿转载和采集!