Problem p02943 (C++)

Buggy submission (status: Runtime Error):
////////////////////////////////////////////    _LeMur_
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = 5005;
const int inf = 1000 * 1000 * 1000;
const int mod = 998244353;

int n, k;
string s;

pair<pair<int, int>, int> p[N];
int R[N];

int main() {
  mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
  cin >> n >> k;
  cin >> s;
  k = min(k, 16);
  while (k--) {
    string t = s;
    reverse(t.begin(), t.end());
    string S = s + t;
    for (int i = 0; i <= 16; i++) {
      for (int j = 0; j < (int)S.size(); j++) {
        if (i == 0) {
          p[j].first = make_pair(S[j] - 'a', 0);
          p[j].second = j;
        } else {
          if (j + (1 << (i - 1)) >= (int)S.size()) {
            p[j].first = make_pair(R[j], -1);
            p[j].second = j;
          } else {
            p[j].first = make_pair(R[j], R[j + (1 << (i - 1))]);
            p[j].second = j;
          }
        }
      }
      sort(p, p + (int)S.size());
      int it = 0;
      for (int j = 0; j < (int)S.size(); j++) {
        ++it;
        pair<int, int> x = p[j].first;
        while (j < (int)S.size() && p[j].first == x) {
          R[p[j].second] = it;
          ++j;
        }
        --j;
      }
    }
    int mn = inf;
    int id;
    for (int i = 0; i < n; i++) {
      if (R[i] < mn) {
        mn = R[i];
        id = i;
      }
    }
    if (k == 0) {
      int it = 0;
      while (it < n) {
        s[it++] = S[id++];
      }
    } else {
      int it = n - 1;
      id = 2 * n - 1 - id;
      while (it >= 0) {
        s[it--] = S[id--];
      }
    }
  }
  cout << s << endl;
  return 0;
}


Fixed submission:
////////////////////////////////////////////    _LeMur_
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = 20005;
const int inf = 1000 * 1000 * 1000;
const int mod = 998244353;

int n, k;
string s;

pair<pair<int, int>, int> p[N];
int R[N];

int main() {
  mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
  cin >> n >> k;
  cin >> s;
  k = min(k, 16);
  while (k--) {
    string t = s;
    reverse(t.begin(), t.end());
    string S = s + t;
    for (int i = 0; i <= 16; i++) {
      for (int j = 0; j < (int)S.size(); j++) {
        if (i == 0) {
          p[j].first = make_pair(S[j] - 'a', 0);
          p[j].second = j;
        } else {
          if (j + (1 << (i - 1)) >= (int)S.size()) {
            p[j].first = make_pair(R[j], -1);
            p[j].second = j;
          } else {
            p[j].first = make_pair(R[j], R[j + (1 << (i - 1))]);
            p[j].second = j;
          }
        }
      }
      sort(p, p + (int)S.size());
      int it = 0;
      for (int j = 0; j < (int)S.size(); j++) {
        ++it;
        pair<int, int> x = p[j].first;
        while (j < (int)S.size() && p[j].first == x) {
          R[p[j].second] = it;
          ++j;
        }
        --j;
      }
    }
    int mn = inf;
    int id;
    for (int i = 0; i < n; i++) {
      if (R[i] < mn) {
        mn = R[i];
        id = i;
      }
    }
    if (k == 0) {
      int it = 0;
      while (it < n) {
        s[it++] = S[id++];
      }
    } else {
      int it = n - 1;
      id = 2 * n - 1 - id;
      while (it >= 0) {
        s[it--] = S[id--];
      }
    }
  }
  cout << s << endl;
  return 0;
}