shadow's Blog

Happy coding

sunday算法的简单实现

shadow posted @ 2014年5月29日 10:20 in 未分类 with tags python sunday , 8677 阅读

# -*- 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)

 

Avatar_small
seo service UK 说:
2024年2月21日 15:30

I adore each of the threads, I relished, I'd really like much more information with this, mainly because it is quite pleasant., Appreciate it intended for giving


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter