Advertisement
maxim_shlyahtin

Kmp

Apr 23rd, 2024 (edited)
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. void form_suffix_array(std::vector<int>& pi, std::string& a) {
  6.     int j = 0, i = 1;
  7.     while (i < a.length()) {
  8.         if (a[i] != a[j]) {
  9.             if (j == 0 && pi[i] == 0) {
  10.                 ++i;
  11.             }
  12.             else {
  13.                 j = pi[j - 1];
  14.             }
  15.         }
  16.         else {
  17.             pi[i] = j + 1;
  18.             ++i;
  19.             ++j;
  20.         }
  21.     }
  22. }
  23.  
  24. void print_str(std::string& str, int start, int end) {
  25.     //std::cout << start << " " << end << '\n';
  26.     if (start == end) return;
  27.     start = start > str.length() ? start - str.length() : start;
  28.     end = end > str.length() ? end - str.length() : end;
  29.     for (int i = start; i < end; ++i) {
  30.         std::cout << str[i];
  31.     }
  32.     std::cout << '\n';
  33. }
  34. //←→↑↓
  35. auto print_space = [](int j) {for (int i = 0; i < j; ++i) std::cout << " ";};
  36.  
  37. void display_current_pointer_position(std::string& a, std::string& t, int i, int j) {
  38.     print_space(i);
  39.     std::cout << "|\n";
  40.     print_str(a, 0, a.length());
  41.     print_space(i - j);
  42.     print_str(t, 0, t.length());
  43.     print_space(i);
  44.     std::cout << "|\n\n";
  45. }
  46.  
  47. std::vector<int> kmp(std::string& a, std::string& t, std::vector<int>& pi) {
  48.     std::vector<int> res;
  49.     int n = a.length();
  50.     int m = t.length();
  51.     int i = 0, j = 0;
  52.     while (i < n) {
  53.         if (a[i] == t[j]) {
  54.             ++i; ++j;
  55.             if (j == m) {
  56.                 res.push_back(i - m);
  57.             }
  58.         }
  59.         else {
  60.             if (j > 0) {
  61.                 //display_current_pointer_position(a, t, i, j);
  62.                 j = pi[j - 1];
  63.             }
  64.             else {
  65.                 ++i;
  66.             }
  67.         }
  68.         display_current_pointer_position(a, t, i, j);
  69.     }
  70.     if (res.size() == 0) {
  71.         res.push_back(-1);
  72.     }
  73.     return res;
  74. }
  75.  
  76. //лилилось лилилась
  77.  
  78. int main() {
  79.     std::string p;
  80.     std::cin >> p;
  81.     std::string t;
  82.     std::cin >> t;
  83.     std::vector<int> pi(p.length(), 0);
  84.     form_suffix_array(pi, p);
  85.     std::vector<int> res = kmp(t, p, pi);
  86.     for (int i = 0; i < res.size(); ++i) {
  87.         if (i < res.size() - 1)
  88.             std::cout << res[i] << ',';
  89.         else
  90.             std::cout << res[i];
  91.     }
  92.     std::cout << '\n';
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement