KMP算法及Python代码

Posted 黑猫-警长

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了KMP算法及Python代码相关的知识,希望对你有一定的参考价值。

KMP算法

python代码1:

# -*- coding: utf-8 -*-


def kmp_in(str_a, str_b):
    """
    判断a字符串是否包含b字符串的kmp算法
    """
    # 先算出部分匹配表
    len_a = len(str_a)
    len_b = len(str_b)
    cha = len_b - len_a
    print 'cha: %s' % cha
    part_match = []
    for x in range(1, len_a + 1):
        string = str_a[:x]
        prefix_list = calc_prefix(string)
        suffix_list = calc_suffix(string)
        matched = list(set(prefix_list) & set(suffix_list))
        part_match.append(len(matched[0]) if matched else 0)
    print 'part_match', part_match
    # 逐个匹配
    next_ = 0
    while next_ <= cha:
        print 'next_', next_
        count = 0
        for x in range(0, len_a):
            print 'str_a[%s]: %s, str_b[%s]: %s' % (x, str_a[x], next_ + x, str_b[next_ + x])
            print str_a[x], str_b[next_ + x]
            if str_a[x] == str_b[next_ + x]:
                count += 1
            else:
                print 'count:%s, x:%s, part_match[x]:%s' % (count, x, part_match[x])
                move = count - (part_match[x - 1]) if x != 0 else 1
                move = 1 if move <= 0 else move
                next_ += move
                break
        print 'count:%s' % count
        if count == len_a:
            return next_
    print 'next_:%s, cha: %s' % (next_, cha)
    print 'end, not match'


def calc_prefix(string):
    length = len(string)
    prefix_list = []
    for x in range(1, length):
        prefix_list.append(string[0:x])
    return prefix_list


def calc_suffix(string):
    length = len(string)
    suffix_list = []
    for x in range(1, length):
        suffix_list.append(string[x:])
    return suffix_list


if __name__ == '__main__':
    print kmp_in('ABCDABD', 'BBC ABCDAB ABCDABCDABDE')
    """
    BBC ABCDAB ABCDABCDABDE
                   ABCDABD
    """

以上是关于KMP算法及Python代码的主要内容,如果未能解决你的问题,请参考以下文章

Python ---- KMP(博文推荐+代码)

KMP算法及KMP算法的应用(POJ2406)

KMP算法及拓展KMP算法

kmp算法模板及理解

KMP算法及应用

数据结构KMP算法配图详解(超详细)