diff --git a/d0/df1/z__function_8cpp__incl.map b/d0/df1/z__function_8cpp__incl.map new file mode 100644 index 000000000..bacfcdc0a --- /dev/null +++ b/d0/df1/z__function_8cpp__incl.map @@ -0,0 +1,7 @@ + + + + + + + diff --git a/d0/df1/z__function_8cpp__incl.md5 b/d0/df1/z__function_8cpp__incl.md5 new file mode 100644 index 000000000..ba234f97f --- /dev/null +++ b/d0/df1/z__function_8cpp__incl.md5 @@ -0,0 +1 @@ +b81f9d648bffbbf0a41ae13bb9d1e19d \ No newline at end of file diff --git a/d0/df1/z__function_8cpp__incl.svg b/d0/df1/z__function_8cpp__incl.svg new file mode 100644 index 000000000..92b8b49d0 --- /dev/null +++ b/d0/df1/z__function_8cpp__incl.svg @@ -0,0 +1,82 @@ + + + + + + +strings/z_function.cpp + + + +Node1 + + +strings/z_function.cpp + + + + + +Node2 + + +iostream + + + + + +Node1->Node2 + + + + + +Node3 + + +cstring + + + + + +Node1->Node3 + + + + + +Node4 + + +cassert + + + + + +Node1->Node4 + + + + + +Node5 + + +vector + + + + + +Node1->Node5 + + + + + diff --git a/d3/d80/z__function_8cpp.html b/d3/d80/z__function_8cpp.html new file mode 100644 index 000000000..32b010a39 --- /dev/null +++ b/d3/d80/z__function_8cpp.html @@ -0,0 +1,343 @@ + + + + + + + +Algorithms_in_C++: strings/z_function.cpp File Reference + + + + + + + + + + + + + + + +
+
+ + + + + + +
+
Algorithms_in_C++ 1.0.0 +
+
Set of algorithms implemented in C++.
+
+
+ + + + + + + +
+
+ +
+
+
+ +
+ +
+
+ + +
+ +
+ +
+ +
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. +
  3. 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
    +
  4. +
+

Function Documentation

+ +

◆ find_pat_in_text()

+ +
+
+ + + + + + + + + + + + + + + + + + +
std::vector< uint64_t > find_pat_in_text (const std::stringpattern,
const std::stringtext 
)
+
+ +

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::stringpattern)
+
+ +

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:
+
+
+
+
+ +
+
+
+
+ + + + diff --git a/d3/d80/z__function_8cpp.js b/d3/d80/z__function_8cpp.js new file mode 100644 index 000000000..84923484b --- /dev/null +++ b/d3/d80/z__function_8cpp.js @@ -0,0 +1,7 @@ +var z__function_8cpp = +[ + [ "find_pat_in_text", "d3/d80/z__function_8cpp.html#ac186ca3ac3a69b5e52543bb13fe46db8", null ], + [ "main", "d3/d80/z__function_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4", null ], + [ "test", "d3/d80/z__function_8cpp.html#aa8dca7b867074164d5f45b0f3851269d", null ], + [ "Z_function", "d3/d80/z__function_8cpp.html#ac044c4794349a8cff6256b99950d5773", null ] +]; \ No newline at end of file diff --git a/d3/d80/z__function_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.map b/d3/d80/z__function_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.map new file mode 100644 index 000000000..8cfc22d99 --- /dev/null +++ b/d3/d80/z__function_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.map @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/d3/d80/z__function_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.md5 b/d3/d80/z__function_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.md5 new file mode 100644 index 000000000..f7f2f5c93 --- /dev/null +++ b/d3/d80/z__function_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.md5 @@ -0,0 +1 @@ +fbb324c1f590a1d8a49578785415c7d9 \ No newline at end of file diff --git a/d3/d80/z__function_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg b/d3/d80/z__function_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg new file mode 100644 index 000000000..0cb354186 --- /dev/null +++ b/d3/d80/z__function_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg @@ -0,0 +1,103 @@ + + + + + + +test + + + +Node1 + + +test + + + + + +Node2 + + +find_pat_in_text + + + + + +Node1->Node2 + + + + + +Node3 + + +std::vector::push_back + + + + + +Node2->Node3 + + + + + +Node4 + + +std::string::size + + + + + +Node2->Node4 + + + + + +Node5 + + +Z_function + + + + + +Node2->Node5 + + + + + +Node5->Node4 + + + + + +Node6 + + +std::min + + + + + +Node5->Node6 + + + + + diff --git a/d3/d80/z__function_8cpp_ac044c4794349a8cff6256b99950d5773_cgraph.map b/d3/d80/z__function_8cpp_ac044c4794349a8cff6256b99950d5773_cgraph.map new file mode 100644 index 000000000..907815881 --- /dev/null +++ b/d3/d80/z__function_8cpp_ac044c4794349a8cff6256b99950d5773_cgraph.map @@ -0,0 +1,5 @@ + + + + + diff --git a/d3/d80/z__function_8cpp_ac044c4794349a8cff6256b99950d5773_cgraph.md5 b/d3/d80/z__function_8cpp_ac044c4794349a8cff6256b99950d5773_cgraph.md5 new file mode 100644 index 000000000..6e4c758d6 --- /dev/null +++ b/d3/d80/z__function_8cpp_ac044c4794349a8cff6256b99950d5773_cgraph.md5 @@ -0,0 +1 @@ +43af2766e9018b42600a49c42e71449d \ No newline at end of file diff --git a/d3/d80/z__function_8cpp_ac044c4794349a8cff6256b99950d5773_cgraph.svg b/d3/d80/z__function_8cpp_ac044c4794349a8cff6256b99950d5773_cgraph.svg new file mode 100644 index 000000000..ee5712a9b --- /dev/null +++ b/d3/d80/z__function_8cpp_ac044c4794349a8cff6256b99950d5773_cgraph.svg @@ -0,0 +1,52 @@ + + + + + + +Z_function + + + +Node1 + + +Z_function + + + + + +Node2 + + +std::min + + + + + +Node1->Node2 + + + + + +Node3 + + +std::string::size + + + + + +Node1->Node3 + + + + + diff --git a/d3/d80/z__function_8cpp_ac186ca3ac3a69b5e52543bb13fe46db8_cgraph.map b/d3/d80/z__function_8cpp_ac186ca3ac3a69b5e52543bb13fe46db8_cgraph.map new file mode 100644 index 000000000..70b8c970b --- /dev/null +++ b/d3/d80/z__function_8cpp_ac186ca3ac3a69b5e52543bb13fe46db8_cgraph.map @@ -0,0 +1,7 @@ + + + + + + + diff --git a/d3/d80/z__function_8cpp_ac186ca3ac3a69b5e52543bb13fe46db8_cgraph.md5 b/d3/d80/z__function_8cpp_ac186ca3ac3a69b5e52543bb13fe46db8_cgraph.md5 new file mode 100644 index 000000000..8c4c43c24 --- /dev/null +++ b/d3/d80/z__function_8cpp_ac186ca3ac3a69b5e52543bb13fe46db8_cgraph.md5 @@ -0,0 +1 @@ +cc2c759e5135a275dc88341aadf188ea \ No newline at end of file diff --git a/d3/d80/z__function_8cpp_ac186ca3ac3a69b5e52543bb13fe46db8_cgraph.svg b/d3/d80/z__function_8cpp_ac186ca3ac3a69b5e52543bb13fe46db8_cgraph.svg new file mode 100644 index 000000000..cae9f961b --- /dev/null +++ b/d3/d80/z__function_8cpp_ac186ca3ac3a69b5e52543bb13fe46db8_cgraph.svg @@ -0,0 +1,88 @@ + + + + + + +find_pat_in_text + + + +Node1 + + +find_pat_in_text + + + + + +Node2 + + +std::vector::push_back + + + + + +Node1->Node2 + + + + + +Node3 + + +std::string::size + + + + + +Node1->Node3 + + + + + +Node4 + + +Z_function + + + + + +Node1->Node4 + + + + + +Node4->Node3 + + + + + +Node5 + + +std::min + + + + + +Node4->Node5 + + + + + diff --git a/d3/d80/z__function_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.map b/d3/d80/z__function_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.map new file mode 100644 index 000000000..e3b4e37dd --- /dev/null +++ b/d3/d80/z__function_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.map @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/d3/d80/z__function_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.md5 b/d3/d80/z__function_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.md5 new file mode 100644 index 000000000..8663244ef --- /dev/null +++ b/d3/d80/z__function_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.md5 @@ -0,0 +1 @@ +2830528a84813ea1c4ad0ea9bb900ecc \ No newline at end of file diff --git a/d3/d80/z__function_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg b/d3/d80/z__function_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg new file mode 100644 index 000000000..7b436224a --- /dev/null +++ b/d3/d80/z__function_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg @@ -0,0 +1,206 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +main + + + +Node1 + + +main + + + + + +Node2 + + +test + + + + + +Node1->Node2 + + + + + +Node3 + + +find_pat_in_text + + + + + +Node2->Node3 + + + + + +Node4 + + +std::vector::push_back + + + + + +Node3->Node4 + + + + + +Node5 + + +std::string::size + + + + + +Node3->Node5 + + + + + +Node6 + + +Z_function + + + + + +Node3->Node6 + + + + + +Node6->Node5 + + + + + +Node7 + + +std::min + + + + + +Node6->Node7 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/d3/d80/z__function_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph_org.svg b/d3/d80/z__function_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph_org.svg new file mode 100644 index 000000000..f6245135a --- /dev/null +++ b/d3/d80/z__function_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph_org.svg @@ -0,0 +1,118 @@ + + + + + + +main + + + +Node1 + + +main + + + + + +Node2 + + +test + + + + + +Node1->Node2 + + + + + +Node3 + + +find_pat_in_text + + + + + +Node2->Node3 + + + + + +Node4 + + +std::vector::push_back + + + + + +Node3->Node4 + + + + + +Node5 + + +std::string::size + + + + + +Node3->Node5 + + + + + +Node6 + + +Z_function + + + + + +Node3->Node6 + + + + + +Node6->Node5 + + + + + +Node7 + + +std::min + + + + + +Node6->Node7 + + + + + diff --git a/d5/d88/md__d_i_r_e_c_t_o_r_y.html b/d5/d88/md__d_i_r_e_c_t_o_r_y.html index 160f750fa..8e6f985da 100644 --- a/d5/d88/md__d_i_r_e_c_t_o_r_y.html +++ b/d5/d88/md__d_i_r_e_c_t_o_r_y.html @@ -471,7 +471,8 @@ Strings
  • Horspool
  • Knuth Morris Pratt
  • Manacher Algorithm
  • -
  • Rabin Karp
  • +
  • Rabin Karp
  • +
  • Z Function
  • diff --git a/dir_73a3cc5065b223eb41b02873c0e19f0e.html b/dir_73a3cc5065b223eb41b02873c0e19f0e.html index 118b98529..96a64b96f 100644 --- a/dir_73a3cc5065b223eb41b02873c0e19f0e.html +++ b/dir_73a3cc5065b223eb41b02873c0e19f0e.html @@ -111,6 +111,9 @@ Files file  rabin_karp.cpp  The Rabin-Karp Algorithm for finding a pattern within a piece of text with complexity O(n + m)
      +file  z_function.cpp + The Z function for finding occurences of a pattern within a piece of text with time and space complexity O(n + m)
    diff --git a/dir_73a3cc5065b223eb41b02873c0e19f0e.js b/dir_73a3cc5065b223eb41b02873c0e19f0e.js index f14df8a9d..0592ca415 100644 --- a/dir_73a3cc5065b223eb41b02873c0e19f0e.js +++ b/dir_73a3cc5065b223eb41b02873c0e19f0e.js @@ -4,5 +4,6 @@ var dir_73a3cc5065b223eb41b02873c0e19f0e = [ "horspool.cpp", "d3/dfe/horspool_8cpp.html", "d3/dfe/horspool_8cpp" ], [ "knuth_morris_pratt.cpp", "de/d6a/knuth__morris__pratt_8cpp.html", "de/d6a/knuth__morris__pratt_8cpp" ], [ "manacher_algorithm.cpp", "d3/d39/manacher__algorithm_8cpp.html", "d3/d39/manacher__algorithm_8cpp" ], - [ "rabin_karp.cpp", "d6/dce/rabin__karp_8cpp.html", "d6/dce/rabin__karp_8cpp" ] + [ "rabin_karp.cpp", "d6/dce/rabin__karp_8cpp.html", "d6/dce/rabin__karp_8cpp" ], + [ "z_function.cpp", "d3/d80/z__function_8cpp.html", "d3/d80/z__function_8cpp" ] ]; \ No newline at end of file diff --git a/files.html b/files.html index dee64154a..c1d5ad3dc 100644 --- a/files.html +++ b/files.html @@ -337,6 +337,7 @@ solve-a-rat-in-a-maze-c-java-pytho/"  knuth_morris_pratt.cppThe Knuth-Morris-Pratt Algorithm for finding a pattern within a piece of text with complexity O(n + m)  manacher_algorithm.cppImplementation of Manacher's Algorithm  rabin_karp.cppThe Rabin-Karp Algorithm for finding a pattern within a piece of text with complexity O(n + m) + z_function.cppThe Z function for finding occurences of a pattern within a piece of text with time and space complexity O(n + m) diff --git a/globals_dup.js b/globals_dup.js index 1d9337e85..f66490c91 100644 --- a/globals_dup.js +++ b/globals_dup.js @@ -21,5 +21,6 @@ var globals_dup = [ "s", "globals_s.html", null ], [ "t", "globals_t.html", null ], [ "u", "globals_u.html", null ], - [ "w", "globals_w.html", null ] + [ "w", "globals_w.html", null ], + [ "z", "globals_z.html", null ] ]; \ No newline at end of file diff --git a/globals_f.html b/globals_f.html index 4896f8ff1..32e4efcc9 100644 --- a/globals_f.html +++ b/globals_f.html @@ -108,6 +108,7 @@ $(document).ready(function(){initNavTree('globals_f.html',''); initResizable();
  • fibonacci_search() : fibonacci_search.cpp
  • fill() : decimal_to_roman_numeral.cpp
  • Find() : disjoint_set.cpp
  • +
  • find_pat_in_text() : z_function.cpp
  • finding_number_of_digits_in_a_number() : finding_number_of_digits_in_a_number.cpp
  • FindNextGap() : comb_sort.cpp
  • fit_OLS_regressor() : ordinary_least_squares_regressor.cpp
  • diff --git a/globals_func.js b/globals_func.js index a954404fa..25dad0c64 100644 --- a/globals_func.js +++ b/globals_func.js @@ -20,5 +20,6 @@ var globals_func = [ "r", "globals_func_r.html", null ], [ "s", "globals_func_s.html", null ], [ "t", "globals_func_t.html", null ], - [ "u", "globals_func_u.html", null ] + [ "u", "globals_func_u.html", null ], + [ "z", "globals_func_z.html", null ] ]; \ No newline at end of file diff --git a/globals_func_f.html b/globals_func_f.html index dd0e5a1ee..75d42ae94 100644 --- a/globals_func_f.html +++ b/globals_func_f.html @@ -106,6 +106,7 @@ $(document).ready(function(){initNavTree('globals_func_f.html',''); initResizabl
  • fibonacci_search() : fibonacci_search.cpp
  • fill() : decimal_to_roman_numeral.cpp
  • Find() : disjoint_set.cpp
  • +
  • find_pat_in_text() : z_function.cpp
  • finding_number_of_digits_in_a_number() : finding_number_of_digits_in_a_number.cpp
  • FindNextGap() : comb_sort.cpp
  • fit_OLS_regressor() : ordinary_least_squares_regressor.cpp
  • diff --git a/globals_func_m.html b/globals_func_m.html index 704b32474..a2e4b3211 100644 --- a/globals_func_m.html +++ b/globals_func_m.html @@ -93,7 +93,7 @@ $(document).ready(function(){initNavTree('globals_func_m.html',''); initResizabl  

    - m -