Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. void getSubstrings(string& source, string& substring, vector<int>& res)
  2. {
  3. vector<int> br(substring.length(), 0);
  4. int index;
  5. for (int i = 1; i < substring.length(); i++)
  6. {
  7. index = br[i - 1];
  8. while (index > 0 && substring[i] != substring[index])
  9. index = br[index - 1];
  10.  
  11. if (substring[i] == substring[index])
  12. index++;
  13.  
  14. br[i] = index;
  15. }
  16.  
  17. vector<int> brs(substring.length(), 0);
  18. for (int i = 0; i < substring.length() - 1; i++)
  19. {
  20. if (substring[i + 1] != substring[br[i]])
  21. brs[i] = br[i];
  22. else
  23. brs[i] = brs[br[i]];
  24. }
  25. brs[substring.length() - 1] = br[substring.length() - 1];
  26.  
  27. int j = 0;
  28. for (int i = 0; i < source.size(); i++)
  29. {
  30. while (j > 0 && source[i] != substring[j])
  31. j = brs[j - 1];
  32.  
  33. if (source[i] == substring[j])
  34. {
  35. j++;
  36. }
  37.  
  38. if (j == substring.length())
  39. {
  40. res.push_back(i - j + 1);
  41. j = brs[j - 1];
  42. }
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement