KMP算法python实现

#File Name : KMP算法.py

def getIndexOf(str1,str2):
    #判断,str2是否在str1中
    def getNextArray(strS):
        #用于返回strS中 每个位置匹配度的数组
        if len(strS) == 1:
            return [-1]
        nextArr = [0 for i in range(len(strS))]
        nextArr[0] = -1
        nextArr[1] = 0
        pos = 2
        cn = 0
        while pos < len(strS):
            if strS[pos-1] == strS[cn]:
                nextArr[pos] = cn+1
                pos+=1
                cn+=1
            elif cn==0:
                nextArr[pos] = 0
                pos+=1
            else:
                cn = nextArr[cn]
        return nextArr

    if str1==None or str2==None or len(str2)<1 or len(str1)

 

你可能感兴趣的:(算法牛人之路)