diff --git a/strings/z_function.cpp b/strings/z_function.cpp index 6056dd5ae..8493e8a9c 100644 --- a/strings/z_function.cpp +++ b/strings/z_function.cpp @@ -26,11 +26,11 @@ * \param[in] pattern text on which to apply the Z-function * \returns the Z-function output as a vector array */ -std::vector Z_function(const std::string &pattern) { - int pattern_length = pattern.size(); - std::vector z(pattern_length, 0); +std::vector Z_function(const std::string &pattern) { + uint64_t pattern_length = pattern.size(); + std::vector z(pattern_length, 0); - for (int i = 1, l = 0, r = 0; i < pattern_length; i++) { + for (uint64_t i = 1, l = 0, r = 0; i < pattern_length; i++) { if (i <= r) z[i] = std::min(r - i + 1, z[i - l]); while (i + z[i] < pattern_length && pattern[z[i]] == pattern[i + z[i]]) @@ -47,13 +47,13 @@ std::vector Z_function(const std::string &pattern) { * \param[in] text text in which to search * \returns a vector of starting indexes where pattern is found in the text */ -std::vector find_pat_in_text(const std::string &pattern, +std::vector find_pat_in_text(const std::string &pattern, const std::string &text) { - int text_length = text.size(), pattern_length = pattern.size(); - std::vector z = Z_function(pattern + '#' + text); - std::vector matching_indexes; + uint64_t text_length = text.size(), pattern_length = pattern.size(); + std::vector z = Z_function(pattern + '#' + text); + std::vector matching_indexes; - for (int i = 0; i < text_length; i++) { + for (uint64_t i = 0; i < text_length; i++) { if (z[i + pattern_length + 1] == pattern_length) matching_indexes.push_back(i); } @@ -69,15 +69,15 @@ static void test() { std::string text1 = "alskfjaldsabc1abc1abcbksbcdnsdabcabc"; std::string pattern1 = "abc"; - std::vector matching_indexes1 = find_pat_in_text(pattern1, text1); - assert((matching_indexes1 == std::vector{10, 14, 18, 30, 33})); + std::vector matching_indexes1 = find_pat_in_text(pattern1, text1); + assert((matching_indexes1 == std::vector{10, 14, 18, 30, 33})); // corner case std::string text2 = "greengrass"; std::string pattern2 = "abc"; - std::vector matching_indexes2 = find_pat_in_text(pattern2, text2); - assert((matching_indexes2 == std::vector{})); + std::vector matching_indexes2 = find_pat_in_text(pattern2, text2); + assert((matching_indexes2 == std::vector{})); } /**