字符串匹配算法之sunday算法python实现

目标字符串s,匹配字符串p,找出p在s匹配的位置
s: A B C D E F G H I J K
i
p: G H I
j
i=0,j=0时 ,
if s[0] != p[0],则 i 移动len(p)的长度
检查s[len(p)]和p(j)中的每一个元素是否有相同
相同:则i移动到相同的位置开始检查
不同:则i移动的len(p)+1的位置开始匹配p(j)
依次循环找到字符串的位置

class MySunday(object):
    def __init__(self,s,p):
        self.s=s
        self.p=p
        self.s_len=len(s)
        self.p_len=len(p)
    def sundayAlgorithm(self):
        i=0
        s_header = 0
        s_tail = self.p_len
        while i<=self.s_len:
            if self.p[i]==self.s[i+s_header]:
                i=i+1
                if i==self.p_len:
                    return s_header       
            else:
                s_header=s_tail-self.compareP(self.s[s_tail]);
                s_tail=s_header + self.p_len
    def compareP(self,m):
        i=0
        while i

你可能感兴趣的:(字符串匹配算法之sunday算法python实现)