Advertisement
logxx

zf

Nov 27th, 2022
687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. vector<int> zf(const string &s)
  8. {
  9.   int n = s.size();
  10.   vector<int> res(n);
  11.   for (int i = 1; i < n; ++i)
  12.     {
  13.       while (i + res[i] < n && s[res[i]] == s[i + res[i]])
  14.     ++res[i];
  15.     }
  16.   return res;
  17. }
  18.  
  19. int main()
  20. {
  21.   string s;
  22.   cin >> s;
  23.   for (auto x : zf(s))
  24.     cout << x << ' ';
  25.   cout << '\n';
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement