Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- namespace constants {
- const int alphabet_size = 27;
- }
- int char_converse(char symbol) {
- if (symbol == '#') return 0;
- return symbol - 'a' + 1;
- }
- int main() {
- std::string string;
- std::cin >> string;
- string += '#';
- int n = string.size();
- std::vector<int> p(n);
- std::vector<int> symbols_counter(std::max(n, constants::alphabet_size), 0);
- std::vector<int> eq_classes(n);
- // zero-step
- for (char symbol : string) {
- ++symbols_counter[char_converse(symbol)];
- }
- for (int i = 1; i < constants::alphabet_size; ++i) {
- symbols_counter[i] += symbols_counter[i - 1];
- }
- for (int i = n - 1; i >= 0; --i) {
- p[--symbols_counter[char_converse(string[i])]] = i;
- }
- eq_classes[p[0]] = 0;
- for (int i = 1; i < n; ++i) {
- eq_classes[p[i]] = eq_classes[p[i - 1]];
- if (string[p[i]] != string[p[i - 1]]) {
- ++eq_classes[p[i]];
- }
- }
- // k-step
- for (int k = 0; (1 << k) < n; ++k) {
- std::vector<int> p_new(n);
- for (int i = 0; i < n; ++i) {
- p_new[i] = p[i] - (1 << k);
- if (p_new[i] < 0) {
- p_new[i] += n;
- }
- }
- symbols_counter.assign(symbols_counter.size(), 0);
- for (int i = 0; i < n; ++i) {
- ++symbols_counter[eq_classes[i]];
- }
- for (int i = 1; i < n; ++i) {
- symbols_counter[i] += symbols_counter[i - 1];
- }
- for (int i = n - 1; i >= 0; --i) {
- p[--symbols_counter[eq_classes[p_new[i]]]] = p_new[i];
- }
- std::vector<int> new_eq(n);
- new_eq[p[0]] = 0;
- for (int i = 1; i < n; ++i) {
- new_eq[p[i]] = new_eq[p[i - 1]];
- if (eq_classes[p[i]] != eq_classes[p[i - 1]]) {
- ++new_eq[p[i]];
- } else if (eq_classes[(p[i] + (1 << k)) % n] != eq_classes[(p[i - 1] + (1 << k)) % n]) {
- ++new_eq[p[i]];
- }
- }
- eq_classes = new_eq;
- }
- for (int i = 1; i < n; ++i) {
- std::cout << p[i] + 1 << " ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement