KMP算法(Python实现)

来源:互联网 发布:明礼仪知荣辱演讲稿 编辑:程序博客网 时间:2024/04/20 18:05

        关于KMP算法的原理等请参阅这篇文章:Kmp算法浅析(C++实现)

       本篇文章只是对Kmp用Python进行了实现。

        1.时间复杂度分析

        BF算法的时间复杂度:在最坏的情况下,BF算法要将目标串的每一个字符同模式串进行比较一遍,假定目标串长度为m,模式串长度为n,总的时间复杂度为O(m*n)。而对于KMP算法,进行比较的时间复杂度为O(m+n),求next数组的时间复杂度为n,总体时间复杂度为O(m+2n)。

        2.源代码

        源代码介绍:BF_Match为常规的模式匹配算法,KMP_Match_1和KMP_Match_2为KMP算法,二者是一样的,不同之处在于二者调用了不同的求Next数组的函数。求Next数组有两种方法:一种是递推法getNext_1(),一种是直接求取的方法getNext_2()。详情请参阅下面的源代码:

import randomimport datetimedef BF_Match(s, t):    slen = len(s)    tlen = len(t)    if slen >= tlen:        for k in range(slen - tlen + 1):            i = k            j = 0            while i < slen and j < tlen and s[i] == t[j]:                i = i + 1                j = j + 1            if j == tlen:                return k            else:                continue    return -1def KMP_Match_1(s, t):    slen = len(s)    tlen = len(t)    if slen >= tlen:        i = 0        j = 0        next_list = [-2 for i in range(len(t))]        getNext_1(t, next_list)        #print next_list        while i < slen:            if j == -1 or s[i] == t[j]:                i = i + 1                j = j + 1            else:                j = next_list[j]            if(j == tlen):                return i - tlen    return -1def KMP_Match_2(s, t):    slen = len(s)    tlen = len(t)    if slen >= tlen:        i = 0        j = 0        next_list = [-2 for i in range(len(t))]        getNext_2(t, next_list)        #print next_list        while i < slen:            if j == -1 or s[i] == t[j]:                i = i + 1                j = j + 1            else:                j = next_list[j]            if j == tlen:                return i - tlen    return -1def getNext_1(t, next_list):    next_list[0] = -1    j = 0    k = -1    while j < len(t) - 1:        if k == -1 or t[j] == t[k]:            j = j + 1            k = k + 1            next_list[j] = k        else:            k = next_list[k]def getNext_2(t, next_list):    next_list[0] = -1    next_list[1] = 0    for i in range(2, len(t)):        tmp = i -1        for j in range(tmp, 0, -1):            if equals(t, i, j):                next_list[i] = j                break            next_list[i] = 0def equals(s, i, j):    k = 0    m = i - j    while k <= j - 1 and m <= i - 1:        if s[k] == s[m]:            k = k + 1            m = m + 1        else:            return False    return True    def rand_str(length):    str_0 = []    for i in range(length):        str_0.append(random.choice("abcdefghijklmnopqrstuvwxyz"))    return str_0def main():    x = rand_str(20000)    y = rand_str(5)    print "The String X Length is : ", len(x), " String is :",    for i in range(len(x)):        print x[i],    print ""        print "The String Y Length is : ", len(y), " String is :",    for i in range(len(y)):        print y[i],    print ""        time_1 = datetime.datetime.now()    pos_1 = BF_Match(x, y)    time_2 = datetime.datetime.now()    print "pos_1 = ", pos_1    time_3 = datetime.datetime.now()    pos_2 = KMP_Match_1(x, y)    time_4 = datetime.datetime.now()    print "pos_2 = ", pos_2    time_5 = datetime.datetime.now()    pos_3 = KMP_Match_2(x, y)    time_6 = datetime.datetime.now()    print "pos_3 = ", pos_3    print "Function 1 spend ", time_2 - time_1    print "Function 2 spend ", time_4 - time_3    print "Function 3 spend ", time_6 - time_5main()



0 0
原创粉丝点击