Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <string.h>
- namespace constants {
- const int alphabet_size = 53;
- }
- int char_converse(char symbol) {
- if (symbol == '#') return 0;
- if (symbol >= 'A' and symbol <= 'Z') {
- return symbol - 'A' + 1;
- }
- return symbol - 'a' + 1;
- }
- int main() {
- std::ios_base::sync_with_stdio(false);
- std::cin.tie(nullptr);
- std::string s;
- long long k_inp;
- std::cin >> s >> k_inp;
- s += '#';
- int n = s.size();
- std::vector<long long> p(n);
- std::vector<long long> symbols_counter(std::max(n, constants::alphabet_size), 0);
- std::vector<long long> eq_classes(n);
- std::vector<long long> lcp(n);
- std::vector<long long> pos(n);
- // zero-step
- for (char symbol : s) {
- ++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(s[i])]] = i;
- }
- eq_classes[p[0]] = 0;
- for (int i = 1; i < n; ++i) {
- eq_classes[p[i]] = eq_classes[p[i - 1]];
- if (s[p[i]] != s[p[i - 1]]) {
- ++eq_classes[p[i]];
- }
- }
- // k-step
- for (int k = 0; (1 << k) < n; ++k) { // TODO: 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);
- // memset(&symbols_counter, 0, sizeof(symbols_counter));
- 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<long long> 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 = 0; i < n; ++i) {
- pos[p[i]] = i;
- }
- int l = 0;
- for (int i = 0; i < n; i++) {
- l = std::max(l - 1, 0);
- if (pos[i] == n - 1) {
- continue;
- }
- int j = p[pos[i] + 1];
- while (s[i + l] == s[j + l]) {
- ++l;
- }
- lcp[pos[i]] = l;
- }
- long long cmpval = n - p[0] - 1;
- for (int i = 1; i < n; ++i) {
- cmpval += (n - p[i] - 1 - lcp[i - 1]);
- }
- if (k_inp < cmpval) {
- long long cumsumma = 0, i = -1;
- while (cumsumma < k_inp) {
- ++i;
- cumsumma += n - p[i] - lcp[i - 1] - 1;
- if (i == 0) {
- cumsumma += lcp[i - 1];
- }
- }
- int ll;
- for (int j = p[i]; j < (n - 1) - cumsumma + k_inp; ++j) {
- std::cout << s[j];
- }
- // std::cout << "\n";
- } else {
- for (int j = p[n - 1]; j <= n - 2; ++j) {
- std::cout << s[j];
- }
- // std::cout << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement