# Algorithms_in_C++: strings/z_function.cpp File Reference

Loading...
Searching...
No Matches
z_function.cpp File Reference

The Z function for finding occurences of a pattern within a piece of text with time and space complexity O(n + m) More...

`#include <iostream>`
`#include <cstring>`
`#include <cassert>`
`#include <vector>`
Include dependency graph for z_function.cpp:

## Functions

std::vector< uint64_t > | Z_function (const std::string &pattern)
| for IO operations More...
std::vector< uint64_t > | find_pat_in_text (const std::string &pattern, const std::string &text)
| Using Z_function to find a pattern in a text. More...
static void | test ()
| Self-test implementations. More...
int | main ()
| Main function. More...

## Detailed Description

The Z function for finding occurences of a pattern within a piece of text with time and space complexity O(n + m)

1. The Z-function for a string is an array of length n where the i-th element is equal to the greatest number of characters starting from the position i that coincide with the first characters of s.
2. E.g.: string: ababb then z[2]=2 as s[2]=s[0] and s[3]=s[1] and s[4]!=s[2]
   Author
     Ritika Gupta

## Function Documentation

## find_pat_in_text()

std::vector< uint64_t > find_pat_in_text | ( | const std::string & | pattern,
|  | const std::string & | text
| ) |  |

Using Z_function to find a pattern in a text.

Parameters

  [in] | pattern | string pattern to search
  [in] | text | text in which to search

Returns
  a vector of starting indexes where pattern is found in the text

{
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 (uint64_t i = 0; i < text_length; i++) {
if (z[i + pattern_length + 1] == pattern_length) {
matching_indexes.push_back(i);
}
}
return matching_indexes;
}
std::vector::push_back
T push_back(T... args)
std::string::size
T size(T... args)
std::vector
Z_function
std::vector< uint64_t > Z_function(const std::string &pattern)
for IO operations
Definition: z_function.cpp:28
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

Main function.

Returns
  0 on exit

{
test(); // run self-test implementations
return 0;
}
test
static void test()
Self-test implementations.
Definition: z_function.cpp:71
Here is the call graph for this function:

## test()

static void test | ( |  | ) |

static

Self-test implementations.

Returns
  void

{
// usual case
std::string text1 = "alskfjaldsabc1abc1abcbksbcdnsdabcabc";
std::string pattern1 = "abc";
// matching_indexes1 gets the indexes where pattern1 exists in text1
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";
// matching_indexes2 gets the indexes where pattern2 exists in text2
std::vector<uint64_t> matching_indexes2 = find_pat_in_text(pattern2, text2);
assert((matching_indexes2 == std::vector<uint64_t>{}));
}
std::string
find_pat_in_text
std::vector< uint64_t > find_pat_in_text(const std::string &pattern, const std::string &text)
Using Z_function to find a pattern in a text.
Definition: z_function.cpp:53
Here is the call graph for this function:

## Z_function()

std::vector< uint64_t > Z_function | ( | const std::string & | pattern | ) |

for IO operations

for string for assert for std::vector

Generate the Z-function for the inputted string.

Parameters

  [in] | pattern | text on which to apply the Z-function

Returns
  the Z-function output as a vector array

{
uint64_t pattern_length = pattern.size();
std::vector<uint64_t> z(pattern_length, 0);
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]]) {
z[i]++;
}
if (i + z[i] - 1 > r) {
r = i + z[i] - 1;
}
}
return z;
}
std::min
T min(T... args)
Here is the call graph for this function: