Skip to content

Commit edff958

Browse files
github-actionsgithub-actions
github-actions
authored and
github-actions
committed
clang-format and clang-tidy fixes for 0c7515e
1 parent c212937 commit edff958

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

strings/z_function.cpp

+33-28
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
/**
22
* @file
3-
* @brief The [Z function](https://cp-algorithms.com/string/z-function.html) for finding occurences of a pattern
4-
* within a piece of text with time and space complexity O(n + m)
5-
* @details
3+
* @brief The [Z function](https://cp-algorithms.com/string/z-function.html) for
4+
* finding occurences of a pattern within a piece of text with time and space
5+
* complexity O(n + m)
6+
* @details
67
* DESCRIPTION
7-
* 1. The Z-function for a string is an array of length n where the
8-
* i-th element is equal to the greatest number of characters starting
8+
* 1. The Z-function for a string is an array of length n where the
9+
* i-th element is equal to the greatest number of characters starting
910
* from the position i that coincide with the first characters of s.
1011
* 2. Eg string : ababb then z[2]=2 as s[2]=s[0] and s[3]=s[1] and s[4]!=s[2]
1112
* @author [Ritika Gupta](https://github.com/RitikaGupta8734)
1213
*/
1314

14-
#include <iostream> /// for IO operations
15+
#include <iostream> /// for IO operations
1516
#ifdef _MSC_VER
1617
#include <string> /// for string (use this for MS Visual C++)
1718
#else
18-
#include <cstring> /// for string
19+
#include <cstring> /// for string
1920
#endif
20-
#include <vector> /// for std::vector
21-
#include <cassert> /// for assert
21+
#include <cassert> /// for assert
22+
#include <vector> /// for std::vector
2223

2324
/**
2425
* @brief Generate the Z-function for the inputted string.
@@ -27,12 +28,15 @@
2728
*/
2829
std::vector<int> Z_function(const std::string &pattern) {
2930
int pattern_length = pattern.size();
30-
std::vector<int> z(pattern_length,0);
31-
32-
for (int i = 1,l = 0,r = 0; i < pattern_length; i++) {
33-
if(i<=r)z[i]=std::min(r-i+1,z[i-l]);
34-
while(i+z[i]<pattern_length && pattern[z[i]]==pattern[i+z[i]]) z[i]++;
35-
if(i+z[i]-1>r)r=i+z[i]-1;
31+
std::vector<int> z(pattern_length, 0);
32+
33+
for (int 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+
while (i + z[i] < pattern_length && pattern[z[i]] == pattern[i + z[i]])
37+
z[i]++;
38+
if (i + z[i] - 1 > r)
39+
r = i + z[i] - 1;
3640
}
3741
return z;
3842
}
@@ -41,36 +45,38 @@ std::vector<int> Z_function(const std::string &pattern) {
4145
* @brief Using Z_function to find a pattern in a text
4246
* \param[in] pattern string pattern to search
4347
* \param[in] text text in which to search
44-
* \returns a vector of starting indexes where pattern is found in the text
48+
* \returns a vector of starting indexes where pattern is found in the text
4549
*/
46-
std::vector<int> find_pat_in_text(const std::string &pattern, const std::string &text) {
50+
std::vector<int> find_pat_in_text(const std::string &pattern,
51+
const std::string &text) {
4752
int text_length = text.size(), pattern_length = pattern.size();
48-
std::vector<int> z = Z_function(pattern+'#'+text);
53+
std::vector<int> z = Z_function(pattern + '#' + text);
4954
std::vector<int> matching_indexes;
5055

5156
for (int i = 0; i < text_length; i++) {
52-
if(z[i+pattern_length+1]==pattern_length) matching_indexes.push_back(i);
57+
if (z[i + pattern_length + 1] == pattern_length)
58+
matching_indexes.push_back(i);
5359
}
5460
return matching_indexes;
55-
}
61+
}
5662

5763
/**
5864
* @brief Self-test implementations
5965
* @returns void
6066
*/
61-
static void test(){
67+
static void test() {
6268
// usual case
6369
std::string text1 = "alskfjaldsabc1abc1abcbksbcdnsdabcabc";
6470
std::string pattern1 = "abc";
65-
66-
std::vector<int> matching_indexes1=find_pat_in_text(pattern1,text1);
67-
assert((matching_indexes1 == std::vector<int>{10,14,18,30,33}));
68-
71+
72+
std::vector<int> matching_indexes1 = find_pat_in_text(pattern1, text1);
73+
assert((matching_indexes1 == std::vector<int>{10, 14, 18, 30, 33}));
74+
6975
// corner case
7076
std::string text2 = "greengrass";
7177
std::string pattern2 = "abc";
72-
73-
std::vector<int> matching_indexes2=find_pat_in_text(pattern2,text2);
78+
79+
std::vector<int> matching_indexes2 = find_pat_in_text(pattern2, text2);
7480
assert((matching_indexes2 == std::vector<int>{}));
7581
}
7682

@@ -79,7 +85,6 @@ static void test(){
7985
* @returns 0 on exit
8086
*/
8187
int main() {
82-
8388
test(); // run self-test implementations
8489
return 0;
8590
}

0 commit comments

Comments
 (0)