From 8621a1db73398891c502f96ad0a3ac2dbcf0b5c5 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Fri, 29 May 2020 07:53:46 -0400 Subject: [PATCH] documented kmp string search --- strings/knuth_morris_pratt.cpp | 120 ++++++++++++++++++++------------- 1 file changed, 72 insertions(+), 48 deletions(-) diff --git a/strings/knuth_morris_pratt.cpp b/strings/knuth_morris_pratt.cpp index f6f1169d3..b83cab966 100644 --- a/strings/knuth_morris_pratt.cpp +++ b/strings/knuth_morris_pratt.cpp @@ -1,64 +1,88 @@ -/* - The Knuth-Morris-Pratt Algorithm for finding a pattern within a piece of text - with complexity O(n + m) - 1) Preprocess pattern to identify any suffixes that are identical to prefixes - This tells us where to continue from if we get a mismatch between a character in our pattern - and the text. - 2) Step through the text one character at a time and compare it to a character in the pattern - updating our location within the pattern if necessary -*/ +/** + * \file + * \brief The [Knuth-Morris-Pratt + * Algorithm](https://en.wikipedia.org/wiki/Knuth–Morris–Pratt_algorithm) for + * finding a pattern within a piece of text with complexity O(n + m) + * + * 1. Preprocess pattern to identify any suffixes that are identical to + * prefixes. This tells us where to continue from if we get a mismatch between a + * character in our pattern and the text. + * 2. Step through the text one character at a time and compare it to a + * character in the pattern updating our location within the pattern if + * necessary + */ -#include -#include -#include -using namespace std; -vector getFailureArray(string pattern){ - int pattern_length=pattern.size(); - vectorfailure(pattern_length+1); - failure[0]=-1; - int j=-1; - for(int i=0; i +#ifdef _MSC_VER +#include // use this for MS Visucal C++ +#else +#include +#endif +#include + +/** + * Generate the partial match table aka failure function for a pattern to + * search. + * \param[in] pattern text for which to create the partial match table + * \returns the partial match table as a vector array + */ +std::vector getFailureArray(const std::string &pattern) { + int pattern_length = pattern.size(); + std::vector failure(pattern_length + 1); + failure[0] = -1; + int j = -1; + + for (int i = 0; i < pattern_length; i++) { + while (j != -1 && pattern[j] != pattern[i]) { + j = failure[j]; } j++; - failure[i+1]=j; + failure[i + 1] = j; } return failure; } -bool kmp(string pattern,string text){ - int text_length=text.size(),pattern_length=pattern.size(); - vectorfailure=getFailureArray(pattern); - int k=0; - for(int j=0; j failure = getFailureArray(pattern); + + int k = 0; + for (int j = 0; j < text_length; j++) { + while (k != -1 && pattern[k] != text[j]) { + k = failure[k]; } k++; - if(k==pattern_length)return true; + if (k == pattern_length) + return true; } return false; } -int main() -{ - - string text="alskfjaldsabc1abc1abc12k23adsfabcabc"; - string pattern="abc1abc12l"; - if(kmp(pattern,text)==true){ - cout<<"Found"<