模式匹配的KMP算法实现(Python语言描述)

def misMatchLinks(pattern):
    augPattern = "0" + pattern
    links = {}
    links[1] = 0
    for k in range(2, len(augPattern)):
        s = links[k-1]
        stop = False
        while s >= 1 and not stop:
            if augPattern[s] == augPattern[k-1]:
                stop = True
            else:
                s = links[s]
        links[k] = s + 1
    return links

你可能感兴趣的:(#)