Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void getSubstrings(string& source, string& substring, vector<int>& res)
- {
- vector<int> br(substring.length(), 0);
- int index;
- for (int i = 1; i < substring.length(); i++)
- {
- index = br[i - 1];
- while (index > 0 && substring[i] != substring[index])
- index = br[index - 1];
- if (substring[i] == substring[index])
- index++;
- br[i] = index;
- }
- vector<int> brs(substring.length(), 0);
- for (int i = 0; i < substring.length() - 1; i++)
- {
- if (substring[i + 1] != substring[br[i]])
- brs[i] = br[i];
- else
- brs[i] = brs[br[i]];
- }
- brs[substring.length() - 1] = br[substring.length() - 1];
- int j = 0;
- for (int i = 0; i < source.size(); i++)
- {
- while (j > 0 && source[i] != substring[j])
- j = brs[j - 1];
- if (source[i] == substring[j])
- {
- j++;
- }
- if (j == substring.length())
- {
- res.push_back(i - j + 1);
- j = brs[j - 1];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement