Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added z_algorithm in strings #1581

Merged
merged 20 commits into from
Sep 4, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: added z_algorithm in strings
  • Loading branch information
RitikaGupta8734 committed Aug 30, 2021
commit da4fb386510fd066477f8058f1f87eee23254f3e
75 changes: 75 additions & 0 deletions strings/z_function.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @file
* @brief The Z function for finding occurences of a pattern
* within a piece of text with time and space complexity O(n + m)
* @details
* DESCRIPTION
RitikaGupta8734 marked this conversation as resolved.
Show resolved Hide resolved
* 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. Eg string : ababb then z[2]=2 as s[2]=s[0] and s[3]=s[1] and s[4]!=s[2]
RitikaGupta8734 marked this conversation as resolved.
Show resolved Hide resolved
* LINK
* https://cp-algorithms.com/string/z-function.html
ayaankhan98 marked this conversation as resolved.
Show resolved Hide resolved
* @author [Ritika Gupta](https://github.com/RitikaGupta8734)
*/

#include <iostream>
RitikaGupta8734 marked this conversation as resolved.
Show resolved Hide resolved
#ifdef _MSC_VER
#include <string> // use this for MS Visual C++
RitikaGupta8734 marked this conversation as resolved.
Show resolved Hide resolved
#else
#include <cstring>
RitikaGupta8734 marked this conversation as resolved.
Show resolved Hide resolved
#endif
#include <vector>
# include <cassert>
RitikaGupta8734 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Generate the Z-function for the inputted string.
RitikaGupta8734 marked this conversation as resolved.
Show resolved Hide resolved
* \param[in] pattern text on which to apply the Z-function
* \returns the Z-function output as a vector array
*/
std::vector<int> Z_function(const std::string &pattern) {
int pattern_length = pattern.size();
std::vector<int> z(pattern_length,0);

for (int 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;
}

/**
* Using Z_function to find a pattern in a text
RitikaGupta8734 marked this conversation as resolved.
Show resolved Hide resolved
* \param[in] pattern string pattern to search
* \param[in] text text in which to search
* \returns a vector of starting indexes where pattern is found in the text
*/
std::vector<int> find_pat_in_text(const std::string &pattern, const std::string &text) {
int text_length = text.size(), pattern_length = pattern.size();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using uint64_t for non-negative values (or their appropriate size: uint32_t, uint16_t, uint8_t) or int64_t for negative values. Check other parts of the code (reference). 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made the required changes. Kindly review :)

std::vector<int> z = Z_function(pattern+'#'+text);
std::vector<int> matching_indexes;

for (int i = 0; i < text_length; i++) {
if(z[i+pattern_length+1]==pattern_length) matching_indexes.push_back(i);
}
return matching_indexes;
}


/** Main function */
int main() {
std::string text1 = "alskfjaldsabc1abc1abcbksbcdnsdabcabc";
std::string pattern1 = "abc";

std::vector<int> matching_indexes1=find_pat_in_text(pattern1,text1);
assert((matching_indexes1 == std::vector<int>{10,14,18,30,33}));

std::string text2 = "greengrass";
std::string pattern2 = "abc";

std::vector<int> matching_indexes2=find_pat_in_text(pattern2,text2);
assert((matching_indexes2 == std::vector<int>{}));
ayaankhan98 marked this conversation as resolved.
Show resolved Hide resolved

return 0;
}