關於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 random
import datetime
def 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 -1
def 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 -1
def 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 -1
def 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] = 0
def 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):
return str_0
def 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_5
main()
KMP演算法python實現
目錄 一 找出prefix table 二 kmp演算法實現 二 測試 三 結果 四 時間複雜度簡單匹配演算法的時間複雜度為o m n kmp匹配演算法時間複雜度為o m n 1 把要匹配的字串pattern拆成子串找出最大公共前字尾,原來的要匹配的字串列入為求最大公共前字尾的行列裡面。最大公共前字...
KMP演算法 python實現
兩個字串匹配,kmp演算法時間複雜度o m n 總體思想就是對於匹配子串,利用先驗知識盡可能的不重複匹配。對於子串,計算出當前字元之前字串中相同字元的個數,這樣在子串和字串當前字元匹配不成功時,就可以直接跳轉到之前字串相同的位置的下乙個,減少了匹配的次數。例如 abab,next陣列為 0,0,0,...
python實現演算法 kmp演算法python實現
kmp演算法python實現 kmp演算法 kmp演算法用於字串的模式匹配,也就是找到模式字串在目標字串的第一次出現的位置 比如abababc 那麼bab在其位置1處,bc在其位置5處 我們首先想到的最簡單的辦法就是蠻力的乙個字元乙個字元的匹配,但那樣的時間複雜度會是o m n kmp演算法保證了時...