KMP算法的python实现

理论部分可以参照这篇文章:字符串匹配的KMP算法

或者英文版的:Jake Boxer-The Knuth-Morris-Pratt Algorithm in my own words

下面根据以上的介绍,用python实现的KMP算法(有不对的地方,还望指正)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
def getOne(substr):
    length = len(substr)
    MAX = 0
    j = length-1
    for index in range(length-1):
        if substr[:index+1] == substr[j:]:
            MAX = max(MAX,index+1)
        j -= 1
    return MAX
def buildTable(substr):
    length = len(substr)
    table = []
    for index in range(length):
        v = getOne(substr[:index+1])
        table.append(v)
    return table

def KMP(string,substr):
    t = buildTable(substr)
    j = 0
    for i in range(len(string)):
        if j>=len(substr):
            return i-len(substr)
        if string[i] == substr[j]:
            j += 1
            continue
        if j>0:
            nextp = j-t[j-1]
            j = nextp
    return -1


你可能感兴趣的:(KMP算法的python实现)