Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. void getBorders(string &s, string &res)
  2. {
  3. int n = s.size();
  4. s = "$" + s;
  5. std::vector<int> br(n + 1);
  6. br[1] = 0;
  7.  
  8. for (int i = 2; i <= n; ++i)
  9. {
  10.  
  11. if (s[i] == s[br[i - 1] + 1])
  12. {
  13. br[i] = br[i - 1] + 1;
  14. }
  15. else
  16. {
  17. int j = br[i - 1];
  18. bool flag = false;
  19. while (!flag)
  20. {
  21. if (j == 0)
  22. {
  23. br[i] = 0;
  24. break;
  25. }
  26. if (s[br[j] + 1] != s[i])
  27. {
  28. j = br[j];
  29. }
  30. else
  31. {
  32. br[i] = br[j] + 1;
  33. flag = true;
  34. }
  35. }
  36. }
  37. }
  38.  
  39. int k = n, j = br[n - 1];
  40. while (j != 0)
  41. {
  42. if (s[n] == s[j + 1])
  43. res = s.substr(1, j + 1) + "\n" + res;
  44. j = br[j];
  45. }
  46. if (s[n] == s[1])
  47. {
  48. res = "\n" + res;
  49. res = s[1] + res;
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement