代码如下:
#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