Advertisement
maxim_shlyahtin

optimized_kmp

Apr 23rd, 2024
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 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(char a, std::string& t, std::vector<int>& pi) {
  48.     std::vector<int> res;
  49.     int m = t.length();
  50.     int i = 0, j = 0;
  51.     while (a != '.') {
  52.         if (a == t[j]) {
  53.             ++i; ++j;
  54.             if (j == m) {
  55.                 res.push_back(i - m);
  56.             }
  57.             std::cin >> a;
  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.                 std::cin >> a;
  67.             }
  68.         }
  69.         //display_current_pointer_position(a, t, i, j);
  70.     }
  71.     if (res.size() == 0) {
  72.         res.push_back(-1);
  73.     }
  74.     return res;
  75. }
  76.  
  77. //лилилось лилилась
  78.  
  79. int main() {
  80.     std::string p;
  81.     std::cin >> p;
  82.     //std::string t;
  83.     //std::cin >> t;
  84.     std::vector<int> pi(p.length(), 0);
  85.     form_suffix_array(pi, p);
  86.     char a;
  87.     std::cin >> a;
  88.     std::vector<int> res = kmp(a, p, pi);
  89.     for (int i = 0; i < res.size(); ++i) {
  90.         if (i < res.size() - 1)
  91.             std::cout << res[i] << ',';
  92.         else
  93.             std::cout << res[i];
  94.     }
  95.     std::cout << '\n';
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement