26
26
* \param[in] pattern text on which to apply the Z-function
27
27
* \returns the Z-function output as a vector array
28
28
*/
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 );
32
32
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++) {
34
34
if (i <= r)
35
35
z[i] = std::min (r - i + 1 , z[i - l]);
36
36
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) {
47
47
* \param[in] text text in which to search
48
48
* \returns a vector of starting indexes where pattern is found in the text
49
49
*/
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,
51
51
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;
55
55
56
- for (int i = 0 ; i < text_length; i++) {
56
+ for (uint64_t i = 0 ; i < text_length; i++) {
57
57
if (z[i + pattern_length + 1 ] == pattern_length)
58
58
matching_indexes.push_back (i);
59
59
}
@@ -69,15 +69,15 @@ static void test() {
69
69
std::string text1 = " alskfjaldsabc1abc1abcbksbcdnsdabcabc" ;
70
70
std::string pattern1 = " abc" ;
71
71
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 }));
74
74
75
75
// corner case
76
76
std::string text2 = " greengrass" ;
77
77
std::string pattern2 = " abc" ;
78
78
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 >{}));
81
81
}
82
82
83
83
/* *
0 commit comments