以下是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数组。

求KMP算法的next数组的python代码

原文地址: https://www.cveoy.top/t/topic/fP2z 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录