String Matching -- Brute Force + Rabin-Karp + KMP
2015-02-28 19:32
169 查看
String Matching
这个问题已经被做烂了...
下面是C语言实现集合.
http://www-igm.univ-mlv.fr/~lecroq/string/
留个爪~
暴力解法:
暴力美啊~
""" Programmer : EOF Date : 2015.02.28 Code file : nsm.py """ def naive_string_matcher(T, P) : if (T or P) is None : return n = len(T) m = len(P) for s in range(0, n-m+1) : match = True for i in range(0, m) : if P[i] is not T[s+i] : match = False break if match is True : print "Pattern occurs with shift", s #------------ for testing -------------------- string1 = "hello goodbye and hello" string2 = "hello" naive_string_matcher(string1, string2)
Rabin-Krap :
""" Programmer : EOF Code date : 2015.02.28 Code file : rkm.py e-mail : jasonleaster@gmail.com """ def rabin_karp_matcher(T, P, d, q) : n = len(T) m = len(P) h = d**(m-1) % q p = 0 t_0 = 0 t = [0 for i in range(0, n - m + 1)] # preprosecessing for i in range(0, m) : p = ( d*p + ord(P[i]) ) % q t[0] = (d*t[0] + ord(T[i])) % q # matching for s in range(0, n-m+1) : match = True if p is t[s] : for i in range(0, m) : if P[i] is not T[s+i] : match = False break if match is True : print "Pattern occurs with shift", s if s < n-m : t[s+1] = (d * ( t[s] - ord(T[s]) * h ) + ord(T[s+m]) ) % q #------------ for testing -------------------- string1 = "hello goodbye and hello" string2 = "hello" rabin_karp_matcher(string1, string2, 10, 13)
KMP:
""" Programmer : EOF Code date : 2015.02.28 Code file : kmp.py e-mail : jasonleaster@gmail.com Code description : Here is a implementation of KMP which is a useful algorithm for string matching. """ def kmp_matcher(T, P) : n = len(T) m = len(P) pi = compute_prefix_function(P) q = -1 # number of characters matched for i in range(0, n) : # scan the text from left to right while q > 0 and P[q+1] != T[i] : q = pi[q] # next character doesn't match if P[q+1] == T[i] : q += 1 # next character matches if (q+1) == m : # Is all of P matched ? print "Pattern occurs with shift ", i-m q = pi[q] # look for the next match def compute_prefix_function(P) : m = len(P) pi = [-1 for i in range(0, m)] k = -1 # Attention ! for q in range(1, m) : while k > 0 and P[k+1] != P[q] : k = pi[k] if P[k+1] == P[q] : k += 1 pi[q] = k return pi #-------------for testing---------------------- #string_1 = "hello goodbye and hello" #string_2 = "hello" string_1 = "abcabaabcabac" string_2 = "abaa" kmp_matcher(string_1, string_2)
PHP神人吐槽KMP
waiting for updates ... ...
相关文章推荐
- Rabin Karp Matching: String Matching
- String matching using Rabin-Karp Algorithm
- java实现Brute-Force和KMP模式匹配
- nyoj5 Binary String Matching(KMP)
- KMP string pattern matching
- nyoj 5 Binary String Matching(kmp)
- nyoj 5 Binary String Matching (KMP)
- NYOJ5 Binary String Matching ——KMP
- 5 Binary String Matching【kmp】
- Brute-Force算法 (eg.Binary String Matching)
- 字符串模式匹配之Brute force、KMP、Horspool算法
- nyoj -5 Binary String Matching 【kmp】
- NYOJ 5 Binary String Matching (kmp 字符串匹配)
- 两种模式匹配算法:Brute-Force和KMP
- Binary String Matching(kmp+str)
- NYOJ-----5---Binary String Matching(KMP)
- 字符串系列1 Rabin-Karp, 有限自动机, KMP, 扩展 KMP
- Binary String Matching 5 (简单KMP模板题)
- 字符串匹配(暴匹、 Rabin-Karp、KMP)
- Introduction to String Searching Algorithms--Rabin-Karp and Knuth-Morris-Pratt Algorithms [翻译]