KMP matching
2010-06-10 11:41
162 查看
#include <iostream> #include <string> using namespace std ; struct KeyWord { string strkey ; int *matcher ; } ; void KMP_prefix(KeyWord &kw) ; bool KMP_matcher(string &s, KeyWord &t) ; int main() { string s = "12ababc5677" ; KeyWord t ; t.strkey = "77" ; if (KMP_matcher(s, t)) { cout << "SUCCESS" << endl ; } return 0 ; } void KMP_prefix(KeyWord &key) { int len = key.strkey.size() ; key.matcher = new int[len] ; key.matcher[0] = 0 ; int k = -1, q ; for (q = 1; q < len; ++q) { while ((k >= 0) && (key.strkey[k+1] != key.strkey[q])) { k = key.matcher[k] - 1 ; } if (key.strkey[k+1] == key.strkey[q]) { ++k ; } key.matcher[q] = k+1 ; } } bool KMP_matcher(string &s, KeyWord &t) { int n = s.size() ; int m = t.strkey.size() ; KMP_prefix(t) ; int q = -1, i ; for (i = 0; i < n; ++i) { while ((q >= 0) && (t.strkey[q+1] != s[i])) { q = t.matcher[q] - 1 ; } if (t.strkey[q+1] == s[i]) { ++q ; } if (q+1 == m) { return true ; } } return false ; }
相关文章推荐
- KMP string pattern matching
- Wildcard Matching & Regular Expression Matching & KMP
- 5 Binary String Matching【kmp】
- nyoj -5 Binary String Matching 【kmp】
- String Matching -- Brute Force + Rabin-Karp + KMP
- [LeetCode] Wildcard Matching 字符串匹配,kmp,回溯,dp
- NYOJ 5 Binary String Matching (kmp 字符串匹配)
- nyoj 5 Binary String Matching (KMP)
- Binary String Matching(kmp+str)
- 【字符串匹配】KMP(implement strStr()), 正则匹配(Wildcard Matching),2-dim 动规(regular expression)
- NYOJ5 Binary String Matching ——KMP
- nyoj5 Binary String Matching(KMP)
- KMP string matching algorithm
- Binary String Matching 5 (简单KMP模板题)
- nyoj 5 Binary String Matching(kmp)
- kmp matching
- NYOJ-----5---Binary String Matching(KMP)
- NYOJ 5 Binary String Matching (kmp 字符串匹配)
- POJ3450 Corporate Identity 【KMP 暴力strstr可过】
- hdu 2203 亲和串 (kmp入门)