Advertisement
newb_ie

manacher

Aug 9th, 2021
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. vector<int> construct_d1 (string const & s,int n) {
  2. vector<int> d1(n);
  3. for (int i = 0,l = 0,r = -1; i < n; ++i) {
  4. int k = (i > r) ? 1 : min(d1[l + r - i],r - i + 1);
  5. while (0 <= i - k and i + k < n and s[i - k] == s[i + k]) {
  6. ++k;
  7. }
  8. d1[i] = k--;
  9. if (i + k > r) {
  10. l = i - k,r = i + k;
  11. }
  12. }
  13. return d1;
  14. }
  15.  
  16. vector<int> construct_d2 (string const & s,int n) {
  17. vector<int> d2(n);
  18. for (int i = 0,l = 0,r = -1; i < n; ++i) {
  19. int k = (i > r) ? 0 : min(d2[l + r - i + 1],r - i + 1);
  20. while (0 <= i - k - 1 and i + k < n and s[i - k - 1] == s[i + k]) {
  21. ++k;
  22. }
  23. d2[i] = k--;
  24. if (i + k > r) {
  25. l = i - k - 1;
  26. r = i + k;
  27. }
  28. }
  29. return d2;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement