现在的位置: 首页 > 综合 > 正文

005_013 Python 寻找子序列 字符串应该用find 否则用KMP

2017年11月01日 ⁄ 综合 ⁄ 共 515字 ⁄ 字号 评论关闭

代码如下:

#encoding=utf-8

print '中国'

#寻找子序列

#字符串应该用find 否则用KMP
def KnuthMorrisPratt(text, pattern):    
    pattern = list(pattern)
    length = len(pattern)
   
    shifts = [1] * (length + 1)
    shift = 1
    for pos, pat in enumerate(pattern):
        while shift <= pos and pat != pattern[pos-shift]:
            shift += shifts[pos-shift]
        shifts[pos+1] = shift
    
    startPos = 0
    matchLen = 0
    for c in text:
        while matchLen == length or matchLen >= 0 and pattern[matchLen] != c:
            startPos += shifts[matchLen]
            matchLen -= shifts[matchLen]
        matchLen += 1
        if matchLen == length: yield startPos


for i in KnuthMorrisPratt([1,2,3,4,2,3],[2,3]):
    print i

打印结果如下:

中国
1
4

抱歉!评论已关闭.