sunday算法的简单实现
# -*- coding: utf-8 -*-
def sunday(dst,sub):
ld=len(dst)
ls=len(sub)
i=j=0#i表示dst中的起始位置,j表示已经匹配了的长度
while i+j+1<ld:
#print i,j
if dst[i+j]==sub[j]:
if j==ls-1:
return i
else:j+=1
continue
else:
j=0
tmp = find(sub,ls,dst[i+ls])
if tmp==-1:
i=i+ls
else:i=i+ls-tmp
if i+ls>ld:
return -1
return -1
def find(sub,ls,ch):
for i in range(ls-1,-1,-1):
if sub[i]==ch:
return i
return -1
if __name__=="__main__":
d="abcxxxbaaaabaaaxbbaaabcdamno"
s="aaab"
print sunday(d,s)