Python-匹配算法(未完)

In [1]: def naive_machine(self,p): # self是目标字符串,p是需要查找的字符串,朴素匹配算法
   ...:     if not isinstance(self,str) and not isinstance(p,str):
   ...:         raise stringTypeError
   ...:     m,n = len(p),len(self)
   ...:     i,j = 0,0
   ...:     while i<m and j<n:
   ...:         if list(p)[i] == list(self)[j]:
   ...:             i,j = i+1,j+1
   ...:         else:
   ...:             i,j = 0, j-i+1 #当匹配中途中断时需要回到上一个位置的下一个位置
   ...:     if i==m:
   ...:        return j-i
   ...:     return -1
发布了101 篇原创文章 · 获赞 46 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_40539952/article/details/104280148