Advertisement
den4ik2003

Untitled

Dec 23rd, 2023
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. namespace constants {
  6.   const int alphabet_size = 27;
  7. }
  8.  
  9. int char_converse(char symbol) {
  10.   if (symbol == '#') return 0;
  11.   return symbol - 'a' + 1;
  12. }
  13.  
  14. int main() {
  15.   std::string string;
  16.   std::cin >> string;
  17.   string += '#';
  18.   int n = string.size();
  19.   std::vector<int> p(n);
  20.   std::vector<int> symbols_counter(std::max(n, constants::alphabet_size), 0);
  21.   std::vector<int> eq_classes(n);
  22.  
  23.   // zero-step
  24.   for (char symbol : string) {
  25.     ++symbols_counter[char_converse(symbol)];
  26.   }
  27.   for (int i = 1; i < constants::alphabet_size; ++i) {
  28.     symbols_counter[i] += symbols_counter[i - 1];
  29.   }
  30.   for (int i = n - 1; i >= 0; --i) {
  31.     p[--symbols_counter[char_converse(string[i])]] = i;
  32.   }
  33.  
  34.    eq_classes[p[0]] = 0;
  35.    for (int i = 1; i < n; ++i) {
  36.      eq_classes[p[i]] = eq_classes[p[i - 1]];
  37.      if (string[p[i]] != string[p[i - 1]]) {
  38.        ++eq_classes[p[i]];
  39.      }
  40.    }
  41.  
  42.    // k-step
  43.    for (int k = 0; (1 << k) < n; ++k) {
  44.      std::vector<int> p_new(n);
  45.      for (int i = 0; i < n; ++i) {
  46.        p_new[i] = p[i] - (1 << k);
  47.        if (p_new[i] < 0) {
  48.          p_new[i] += n;
  49.        }
  50.      }
  51.      symbols_counter.assign(symbols_counter.size(), 0);
  52.      for (int i = 0; i < n; ++i) {
  53.        ++symbols_counter[eq_classes[i]];
  54.      }
  55.      for (int i = 1; i < n; ++i) {
  56.        symbols_counter[i] += symbols_counter[i - 1];
  57.      }
  58.      for (int i = n - 1; i >= 0; --i) {
  59.        p[--symbols_counter[eq_classes[p_new[i]]]] = p_new[i];
  60.      }
  61.      std::vector<int> new_eq(n);
  62.      new_eq[p[0]] = 0;
  63.      for (int i = 1; i < n; ++i) {
  64.        new_eq[p[i]] = new_eq[p[i - 1]];
  65.        if (eq_classes[p[i]] != eq_classes[p[i - 1]]) {
  66.          ++new_eq[p[i]];
  67.        } else if (eq_classes[(p[i] + (1 << k)) % n] != eq_classes[(p[i - 1] + (1 << k)) % n]) {
  68.          ++new_eq[p[i]];
  69.        }
  70.      }
  71.      eq_classes = new_eq;
  72.    }
  73.  
  74.    for (int i = 1; i < n; ++i) {
  75.      std::cout << p[i] + 1 << " ";
  76.    }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement