Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. // 0-base
  2. // input : a b c a b a b c a
  3. // output : 9 0 0 2 0 4 0 0 1
  4.  
  5. vector<int> z_algo(const string &s) {
  6. int l = 0, r = 0;
  7. int N = sz(s);
  8. vector<int> Z(N);
  9. Z[0] = N;
  10. repp(i, 1, N) {
  11. if (i > r) {
  12. l = r = i;
  13. while(r < N and s[r] == s[r-l]) r++;
  14. r--;
  15. Z[i] = r-l+1;
  16. } else {
  17. int k = i-l;
  18. if (Z[k] < r-i+1) Z[i] = Z[k];
  19. else {
  20. l = i;
  21. while(r < N and s[r] == s[r-l]) r++;
  22. r--;
  23. Z[i] = r-l+1;
  24. }
  25. }
  26. }
  27. return Z;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement