数据结构--模式匹配

PART I:

#!/usr/bin/env python
#encoding=gbk
import sys
def BF():
    = "abbbbbbcdcdddcefg"
    = "bbbbcdcdddcef"
    = 0
    = 0
    print t, p, t.find(p)
    while i <= (len(t) - len(p)):
        """if t[i] == p[j]:move i and j to next"""
        """if t[i] != p[j]:reset i = i - j + 1 and reset j = 0"""
        if t[i + j] == p[j]:
            += 1
        else:
            += 1
            = 0
        if == len(p):
            return i
    return -1
 
if __name__ == "__main__":
    print BF()

posted on 2014-10-23 19:39  有个姑娘叫小芳  阅读(128)  评论(0编辑  收藏  举报