Skip to content

Commit 12d5123

Browse files
Updated int -> uint64_t
Updated int -> uint64_t for non-negative values
1 parent edff958 commit 12d5123

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

strings/z_function.cpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
* \param[in] pattern text on which to apply the Z-function
2727
* \returns the Z-function output as a vector array
2828
*/
29-
std::vector<int> Z_function(const std::string &pattern) {
30-
int pattern_length = pattern.size();
31-
std::vector<int> z(pattern_length, 0);
29+
std::vector<uint64_t> Z_function(const std::string &pattern) {
30+
uint64_t pattern_length = pattern.size();
31+
std::vector<uint64_t> z(pattern_length, 0);
3232

33-
for (int i = 1, l = 0, r = 0; i < pattern_length; i++) {
33+
for (uint64_t i = 1, l = 0, r = 0; i < pattern_length; i++) {
3434
if (i <= r)
3535
z[i] = std::min(r - i + 1, z[i - l]);
3636
while (i + z[i] < pattern_length && pattern[z[i]] == pattern[i + z[i]])
@@ -47,13 +47,13 @@ std::vector<int> Z_function(const std::string &pattern) {
4747
* \param[in] text text in which to search
4848
* \returns a vector of starting indexes where pattern is found in the text
4949
*/
50-
std::vector<int> find_pat_in_text(const std::string &pattern,
50+
std::vector<uint64_t> find_pat_in_text(const std::string &pattern,
5151
const std::string &text) {
52-
int text_length = text.size(), pattern_length = pattern.size();
53-
std::vector<int> z = Z_function(pattern + '#' + text);
54-
std::vector<int> matching_indexes;
52+
uint64_t text_length = text.size(), pattern_length = pattern.size();
53+
std::vector<uint64_t> z = Z_function(pattern + '#' + text);
54+
std::vector<uint64_t> matching_indexes;
5555

56-
for (int i = 0; i < text_length; i++) {
56+
for (uint64_t i = 0; i < text_length; i++) {
5757
if (z[i + pattern_length + 1] == pattern_length)
5858
matching_indexes.push_back(i);
5959
}
@@ -69,15 +69,15 @@ static void test() {
6969
std::string text1 = "alskfjaldsabc1abc1abcbksbcdnsdabcabc";
7070
std::string pattern1 = "abc";
7171

72-
std::vector<int> matching_indexes1 = find_pat_in_text(pattern1, text1);
73-
assert((matching_indexes1 == std::vector<int>{10, 14, 18, 30, 33}));
72+
std::vector<uint64_t> matching_indexes1 = find_pat_in_text(pattern1, text1);
73+
assert((matching_indexes1 == std::vector<uint64_t>{10, 14, 18, 30, 33}));
7474

7575
// corner case
7676
std::string text2 = "greengrass";
7777
std::string pattern2 = "abc";
7878

79-
std::vector<int> matching_indexes2 = find_pat_in_text(pattern2, text2);
80-
assert((matching_indexes2 == std::vector<int>{}));
79+
std::vector<uint64_t> matching_indexes2 = find_pat_in_text(pattern2, text2);
80+
assert((matching_indexes2 == std::vector<uint64_t>{}));
8181
}
8282

8383
/**

0 commit comments

Comments
 (0)