【算法与数据结构】KMP算法
2017-11-17 00:43
197 查看
Kmp由于其超低的时间复杂度O(m+n),因而被应用到广泛的应用到各种编辑器上。作为基础算法,网上有太多的人孜孜不倦的写教程,所以具体思想也就不再赘述。
看来有些程序员为了造轮子,解释思想,真是不择手段。
具体思想可以看这篇文章:
KMP算法详解
Kmp算法排除思想外,很值得说的就是由于其不回溯的特性,其适用于大型匹配。
看来有些程序员为了造轮子,解释思想,真是不择手段。
具体思想可以看这篇文章:
KMP算法详解
Kmp算法排除思想外,很值得说的就是由于其不回溯的特性,其适用于大型匹配。
#include<iostream> #include<string> #define DEBUG #ifdef DEBUG #include<vector> #endif // DEBUG class Kmp { public: Kmp(const std::string& str) :substr{ str }, next{ new int[str.length()+1]{} } { getnext(substr,next); } ~Kmp(){ if (next != nullptr) { delete[] next; } } int capture(std::string str) { #ifdef DEBUG std::vector<char> all;; #endif // DEBUG const auto subLength = substr.length(); const auto strLength = str.length(); size_t i = 0, j = 0; while (j == -1 || i < strLength && j < subLength) { if ( str[i] == substr[j] ) { #ifdef DEBUG all.push_back(str[i]); letterVector.push_back(str[i]); #endif // DEBUG i += 1; j += 1; } else { j = next[j]; if (j == -1) { i++; j++;} #ifdef DEBUG all.push_back(str[i]); auto beDele = letterVector.begin() + letterVector.size() - j; letterVector.erase(letterVector.begin(), beDele); #endif // DEBUG } } if ( j < subLength) { return i - subLength; } #ifdef DEBUG std::cout << all.size() << std::endl; std::cout << "The last matching sequences :" << std::endl; for (auto letter : letterVector) { std::cout << letter << " "; } #endif // DEBUG return 0; } private: void getnext(const std::string& subStr, int next[]) { next[0] = -1; int j = -1; size_t i = 0; while (i < subStr.length()){ if (j == -1 || subStr[i] == subStr[j]) { /*j++; i++; next[i] = j;*/ if (subStr[++i] == subStr[++j]) { next[i] = next[j]; } else { next[i] = j; } } else{ j = next[j]; } } #ifdef DEBUG std::cout << "next sequnences:"; for (auto i = 0; i < substr.length(); i++) { std::cout << next[i] <<" "; } std::cout << std::endl; #endif // DEBUG } private: std::string substr; int *next; #ifdef DEBUG std::vector<char> letterVector; #endif // DEBUG }; int main() { auto sequece = "ABCDABABCDABXBABCDABABCDABB"; Kmp check{ "ABABAAB" }; std::cout<<check.capture(sequece); return 0; }
相关文章推荐
- 《数据结构》之串的模式匹配算法——KMP算法
- 数据结构第四次上机 第四章之BF和KMP算法(顺序串的各种模式匹配算法)
- 【算法与数据结构】字符串匹配之KMP算法
- KMP算法之NEXT数组代码原理分析 - 数据结构和算法38
- 【数据结构&&算法系列】KMP算法介绍及实现(c++ && java)
- KMP算法之NEXT数组代码原理分析 - 数据结构和算法38
- 算法与数据结构-KMP算法
- 数据结构- 串的模式匹配算法:BF和 KMP算法
- KMP算法之最终实现及优化 - 数据结构和算法39
- 数据结构- 串的模式匹配算法:BF和 KMP算法
- 一步一步复习数据结构和算法基础-KMP算法
- 【算法与数据结构】字符串匹配之KMP算法
- 数据结构- 串的模式匹配算法:BF和 KMP算法
- 数据结构- 串的模式匹配算法:BF和 KMP算法
- KMP算法(养成篇) - 数据结构和算法36
- KMP算法(养成篇) - 数据结构和算法36
- [算法与数据结构] - No.11 字符串匹配KMP算法
- 【数据结构】算法4.6-4.7 KMP算法
- 数据结构Java实现——③串--->串的模式匹配:Brute-Force算法和 KMP算法
- 数据结构- 串的模式匹配算法:BF和 KMP算法