The Z function for finding occurences of a pattern within a piece of text with time and space complexity O(n + m)
More...
#include <cstdint>
#include <iostream>
#include <cstring>
#include <cassert>
#include <vector>
Go to the source code of this file.
|
| 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.
|
| |
The Z function for finding occurences of a pattern within a piece of text with time and space complexity O(n + m)
- 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.
- 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
Definition in file z_function.cpp.
◆ 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
Definition at line 54 of file z_function.cpp.
55 {
56 uint64_t text_length = text.size(), pattern_length = pattern.size();
57 std::vector<uint64_t> z =
Z_function(pattern +
'#' + text);
58 std::vector<uint64_t> matching_indexes;
59
60 for (uint64_t i = 0; i < text_length; i++) {
61 if (z[i + pattern_length + 1] == pattern_length) {
62 matching_indexes.push_back(i);
63 }
64 }
65 return matching_indexes;
66}
std::vector< uint64_t > Z_function(const std::string &pattern)
for IO operations
◆ main()
Main function.
- Returns
- 0 on exit
Definition at line 94 of file z_function.cpp.
94 {
96 return 0;
97}
static void test()
Self-test implementations.
◆ test()
Self-test implementations.
- Returns
- void
Definition at line 72 of file z_function.cpp.
72 {
73
74 std::string text1 = "alskfjaldsabc1abc1abcbksbcdnsdabcabc";
75 std::string pattern1 = "abc";
76
77
79 assert((matching_indexes1 == std::vector<uint64_t>{10, 14, 18, 30, 33}));
80
81
82 std::string text2 = "greengrass";
83 std::string pattern2 = "abc";
84
85
87 assert((matching_indexes2 == std::vector<uint64_t>{}));
88}
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.
◆ 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
Definition at line 29 of file z_function.cpp.
29 {
30 uint64_t pattern_length = pattern.size();
31 std::vector<uint64_t> z(pattern_length, 0);
32
33 for (uint64_t i = 1, l = 0, r = 0; i < pattern_length; i++) {
34 if (i <= r) {
35 z[i] = std::min(r - i + 1, z[i - l]);
36 }
37 while (i + z[i] < pattern_length &&
38 pattern[z[i]] == pattern[i + z[i]]) {
39 z[i]++;
40 }
41 if (i + z[i] - 1 > r) {
42 r = i + z[i] - 1;
43 }
44 }
45 return z;
46}