Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
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
 
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.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

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]patternstring pattern to search
[in]texttext in which to search
Returns
a vector of starting indexes where pattern is found in the text
54 {
55 uint64_t text_length = text.size(), pattern_length = pattern.size();
56 std::vector<uint64_t> z = Z_function(pattern + '#' + text);
57 std::vector<uint64_t> matching_indexes;
58
59 for (uint64_t i = 0; i < text_length; i++) {
60 if (z[i + pattern_length + 1] == pattern_length) {
61 matching_indexes.push_back(i);
62 }
63 }
64 return matching_indexes;
65}
T push_back(T... args)
T size(T... args)
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
93 {
94 test(); // run self-test implementations
95 return 0;
96}
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
71 {
72 // usual case
73 std::string text1 = "alskfjaldsabc1abc1abcbksbcdnsdabcabc";
74 std::string pattern1 = "abc";
75
76 // matching_indexes1 gets the indexes where pattern1 exists in text1
77 std::vector<uint64_t> matching_indexes1 = find_pat_in_text(pattern1, text1);
78 assert((matching_indexes1 == std::vector<uint64_t>{10, 14, 18, 30, 33}));
79
80 // corner case
81 std::string text2 = "greengrass";
82 std::string pattern2 = "abc";
83
84 // matching_indexes2 gets the indexes where pattern2 exists in text2
85 std::vector<uint64_t> matching_indexes2 = find_pat_in_text(pattern2, text2);
86 assert((matching_indexes2 == std::vector<uint64_t>{}));
87}
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]patterntext on which to apply the Z-function
Returns
the Z-function output as a vector array
28 {
29 uint64_t pattern_length = pattern.size();
30 std::vector<uint64_t> z(pattern_length, 0);
31
32 for (uint64_t i = 1, l = 0, r = 0; i < pattern_length; i++) {
33 if (i <= r) {
34 z[i] = std::min(r - i + 1, z[i - l]);
35 }
36 while (i + z[i] < pattern_length &&
37 pattern[z[i]] == pattern[i + z[i]]) {
38 z[i]++;
39 }
40 if (i + z[i] - 1 > r) {
41 r = i + z[i] - 1;
42 }
43 }
44 return z;
45}
T min(T... args)
Here is the call graph for this function: