Advertisement
maxim_shlyahtin

Kmp

Apr 23rd, 2024 (edited)
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 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";
  45.     // Вывод суффикса, на который происходит смещение
  46.    
  47. }
  48.  
  49.  
  50. std::vector<int> kmp(std::string& a, std::string& t, std::vector<int>& pi) {
  51.     std::vector<int> res;
  52.     int n = a.length();
  53.     int m = t.length();
  54.     int i = 0, j = 0;
  55.     while (i < n) {
  56.         if (a[i] == t[j]) {
  57.             ++i; ++j;
  58.             if (j == m) {
  59.                 res.push_back(i - m);
  60.             }
  61.             display_current_pointer_position(a, t, i, j);
  62.         }
  63.         else {
  64.             if (j > 0) {
  65.                 j = pi[j - 1];
  66.                 std::cout << "Suffix for offset: ";
  67.                 print_str(t, 0, j);
  68.                 std::cout << '\n';
  69.                 std::cout << "offset: " << j << '\n';
  70.                 std::cout << '\n';
  71.                 display_current_pointer_position(a, t, i, j);
  72.             }
  73.             else {
  74.                 ++i;
  75.             }
  76.         }
  77.         //display_current_pointer_position(a, t, i, j);
  78.     }
  79.     if (res.size() == 0) {
  80.         res.push_back(-1);
  81.     }
  82.     return res;
  83. }
  84.  
  85. //лилилось лилилась
  86.  
  87. int main() {
  88.     std::string p;
  89.     std::cin >> p;
  90.     std::string t;
  91.     std::cin >> t;
  92.     std::vector<int> pi(p.length(), 0);
  93.     form_suffix_array(pi, p);
  94.     std::vector<int> res = kmp(t, p, pi);
  95.     for (int i = 0; i < res.size(); ++i) {
  96.         if (i < res.size() - 1)
  97.             std::cout << res[i] << ',';
  98.         else
  99.             std::cout << res[i];
  100.     }
  101.     std::cout << '\n';
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement