Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using  namespace std;
  5.  
  6. std::vector<int> Prefix(std::string str){
  7.     std::vector<int> res;
  8.     res.push_back(0);
  9.     for (int i = 1; i < str.size()-1 ; ++i) {
  10.         int k = res[i - 1];
  11.         while (k > 0 && str[i] != str[k])
  12.             k = res[k - 1];
  13.         if (str[i] == str[k]) {
  14.             k++;
  15.         }
  16.         res.push_back(k);
  17.     }
  18.  
  19.  
  20.     return res;
  21. }
  22.  
  23. int main() {
  24.      std::string a;
  25.      cin>>a;
  26.     std::vector<int> res;
  27.     res = Prefix(a);
  28.  
  29.     for (int i = 0; i<res.size(); ++i) {
  30.         cout<<res[i]<<" ";
  31.     }
  32.  
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement