Advertisement
adwas33

Untitled

Feb 5th, 2022
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. vector<int> KMP_generate(string P, int m) {
  7. vector<int> S(m);
  8. S[0] = -1;
  9. // P[k+1] to potencjalny kandydat podciagu
  10. for(int i = 1, k = -1; i < m; ++i) {
  11. while(k > -1 && P[k + 1] != P[i])
  12. k = S[k];
  13. if(P[k + 1] == P[i]) // jeszcze nie koniec
  14. ++k;
  15. S[i] = k;
  16. }
  17. return S;
  18. }
  19.  
  20.  
  21. int main() {
  22. string znaki="aaabaccaaabaaababcaa";
  23. vector<int> wygenerowany= KMP_generate(znaki,znaki.size());
  24. for (const auto &item : wygenerowany)
  25. cout<<item+1<<" ";
  26.  
  27. return 0;
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement