Python实现字符串匹配算法Boyer- Moore

参考链接: 阮一峰 字符串匹配的Boyer-Moore算法

感谢作者分享!

文中demo使用Python3实现。

待完成:好后缀规则。

其他:学习Python中,若demo中有Python相关或其他错误,请稍加批判。

def string_match_boyer_moore(string,match,start=0):
    string_len = len(string)
    match_len  = len(match)
    end = match_len - 1
    print (string)
    print (match)
    print ('******')
    if string_len < match_len:
        print ('Not Found')
        return start;
    while string[end] == match[end]:
        end -= 1
        if end == 0:
            print ('Success Position:' + str(start))
            return
    idx = contain_char(match,string[end])
    shift = match_len
    if idx > -1:
        shift = end - idx
    start += shift
    string_match_boyer_moore(string[shift:],match,start)

def contain_char(s,c):
   for i in range(len(s)):
      if c == s[i]:
          return i
   return -1

string_match_boyer_moore('here is a simple example','example')
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 由于是毕业后转行的原因,所以本人在工作之前没有系统的学过数据结构、算法导论之类的课。说白了就是没有这样的底蕴,哈哈...
    张晨辉Allen阅读 728评论 0 0
  • 包(lib)、模块(module) 在Python中,存在包和模块两个常见概念。 模块:编写Python代码的py...
    清清子衿木子水心阅读 3,846评论 0 27
  • 时间如白驹过隙,一眨眼好像都溜走了。 每次我好像都没有真正面对自己。 晚上又吃了很多,咖喱饭,油炸食品,西瓜,粽子...
    晨曦之路阅读 369评论 0 1
  • 睡前忽然想起来今天听到的《冬吴同学会》第55期“得体的境界”,现在想起仍然意犹未尽。老吴、小梁两人之间不同维度见解...
    少年程序漂流阅读 721评论 0 0
  • 说句实话。 你能见到的人都估得准你的胖瘦、年龄、美丑、有没有钱、会不会打扮。 大部分时间出于礼貌选择不说或美化一番...
    dc45dda0c8b4阅读 961评论 3 0