PART I:
#!/usr/bin/env python#encoding=gbkimport sysdef BF(): t = "abbbbbbcdcdddcefg" p = "bbbbcdcdddcef" i = 0 j = 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]: j += 1 else: i += 1 j = 0 if j == len(p): return i return -1if __name__ == "__main__": print BF()原文:http://www.cnblogs.com/ariesblogs/p/4046637.html