From b9fbd60dcbfd4bd0674ec25f0ad646038ec93537 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Thu, 11 Jul 2019 15:58:58 +0200
Subject: [PATCH] fixes in knuth morris pratt algo

---
 alib2algo/src/string/properties/BorderArray.h         | 2 +-
 alib2algo/src/stringology/exact/KnuthMorrisPratt.h    | 8 ++++++--
 alib2integrationtest/test-src/tests/exactMatching.cpp | 2 +-
 3 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/alib2algo/src/string/properties/BorderArray.h b/alib2algo/src/string/properties/BorderArray.h
index f26d6718fa..1d8b5d585f 100644
--- a/alib2algo/src/string/properties/BorderArray.h
+++ b/alib2algo/src/string/properties/BorderArray.h
@@ -31,7 +31,7 @@ ext::vector<size_t> BorderArray::construct(const string::LinearString < SymbolTy
 	const auto& w = string.getContent();
 	ext::vector<size_t> res(w.size() + 1);
 
-	res[0] = 0;
+	res[0] = -1;
 	res[1] = 0;
 	for(size_t i = 1; i < w.size(); i++) {
 		size_t b = res[i];
diff --git a/alib2algo/src/stringology/exact/KnuthMorrisPratt.h b/alib2algo/src/stringology/exact/KnuthMorrisPratt.h
index 9d6cf6bd0d..44a520e825 100644
--- a/alib2algo/src/stringology/exact/KnuthMorrisPratt.h
+++ b/alib2algo/src/stringology/exact/KnuthMorrisPratt.h
@@ -58,8 +58,12 @@ ext::set < unsigned > KnuthMorrisPratt::match ( const string::LinearString < Sym
 		if ( j >= pattern.getContent ( ).size ( ) ) occ.insert ( i );
 
 		 // shift heristics
-		i += j - ba[j];
-		j = ba[j];
+		if ( j != 0 ) {
+			i += j - ba[j];
+			j = ba[j];
+		} else {
+			i += 1;
+		}
 	}
 
 	//measurements::end();
diff --git a/alib2integrationtest/test-src/tests/exactMatching.cpp b/alib2integrationtest/test-src/tests/exactMatching.cpp
index 10129014d0..41a6c7763d 100644
--- a/alib2integrationtest/test-src/tests/exactMatching.cpp
+++ b/alib2integrationtest/test-src/tests/exactMatching.cpp
@@ -26,7 +26,7 @@ static std::string qGenString ( const size_t & len, const size_t &alph_len, cons
 TEST_CASE ( "ExactMatching", "[integration]" ) {
 	auto definition = GENERATE ( as < std::tuple < std::string, std::string, bool > > ( ),
 			std::make_tuple ( "Exact Boyer Moore", "stringology::exact::BoyerMoore $subject $pattern", true ),
-			std::make_tuple ( "Exact Knuth Morris Pratt", "stringology::exact::KnuthMorrisPratt $subject $pattern", true ),
+			std::make_tuple ( "Exact Knuth Morris Pratt", "stringology::exact::KnuthMorrisPratt $subject $pattern", false ),
 			std::make_tuple ( "Exact Boyer Moore Horspool", " stringology::exact::BoyerMooreHorspool $subject $pattern", true ),
 			std::make_tuple ( "Exact Reversed Boyer Moore Horspool", " stringology::exact::ReversedBoyerMooreHorspool $subject $pattern", true ),
 			std::make_tuple ( "Quick Search", "stringology::exact::QuickSearch $subject $pattern", true ),
-- 
GitLab