Updated int -> uint64_t

Updated int -> uint64_t for non-negative values
This commit is contained in:
RitikaGupta8734 2021-09-02 01:10:48 +05:30 committed by GitHub
parent edff958ab2
commit 12d5123995
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<int> Z_function(const std::string &pattern) {
int pattern_length = pattern.size();
std::vector<int> z(pattern_length, 0);
std::vector<uint64_t> Z_function(const std::string &pattern) {
uint64_t pattern_length = pattern.size();
std::vector<uint64_t> 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<int> 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<int> find_pat_in_text(const std::string &pattern,
std::vector<uint64_t> find_pat_in_text(const std::string &pattern,
const std::string &text) {
int text_length = text.size(), pattern_length = pattern.size();
std::vector<int> z = Z_function(pattern + '#' + text);
std::vector<int> matching_indexes;
uint64_t text_length = text.size(), pattern_length = pattern.size();
std::vector<uint64_t> z = Z_function(pattern + '#' + text);
std::vector<uint64_t> 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<int> matching_indexes1 = find_pat_in_text(pattern1, text1);
assert((matching_indexes1 == std::vector<int>{10, 14, 18, 30, 33}));
std::vector<uint64_t> matching_indexes1 = find_pat_in_text(pattern1, text1);
assert((matching_indexes1 == std::vector<uint64_t>{10, 14, 18, 30, 33}));
// corner case
std::string text2 = "greengrass";
std::string pattern2 = "abc";
std::vector<int> matching_indexes2 = find_pat_in_text(pattern2, text2);
assert((matching_indexes2 == std::vector<int>{}));
std::vector<uint64_t> matching_indexes2 = find_pat_in_text(pattern2, text2);
assert((matching_indexes2 == std::vector<uint64_t>{}));
}
/**