Problem p02943 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

const int max_n = 5555, inf = 1000111222;

int n, k;
string s;

string getr(const string &s, int pos, int mx) {
  if (mx + (mx << k) >= n) {
    return "";
  }
  // cout << "$" << mx << endl;
  return {s.rbegin() + s.length() - 1 - pos + mx,
          s.rbegin() + s.length() - 1 - pos + n - (mx << k)};
}

string one_step(const string &s) {
  string rs = s;
  reverse(rs.begin(), rs.end());
  string t = s + rs;
  string res = s;
  for (int i = 1; i <= n; ++i) {
    res = min(res, t.substr(i, n));
  }
  return res;
}

int main() {
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  cin >> n >> k >> s;
  --k;
  char mn = s[0];
  for (char c : s) {
    mn = min(mn, c);
  }
  if (k) {
    k = min(k, 20);
    --k;
    string rs = s;
    reverse(rs.begin(), rs.end());
    int mx = 0, pos = 0;
    string t = s + rs;
    for (int i = 0; i < t.length(); ++i) {
      int st = i;
      while (i < t.length() && t[i] == mn) {
        ++i;
      }
      int cnt = i - st;
      if (cnt == 0) {
        continue;
      }
      if (i < n) {
        continue;
      }
      if (cnt == 1) {
        // cout << i << ": " << getr(t, i - 1, cnt) << endl;
      }
      if (i >= n &&
          (mx < cnt || mx == cnt && getr(t, pos, mx) > getr(t, i - 1, mx))) {
        pos = i - 1;
        mx = cnt;
      }
    }
    // cout << t << endl;
    // cout << mx << " " << pos << " " << getr(t, pos) << endl;
    s = t.substr(pos - n + 1, n);
    // reverse(s.begin(), s.end());
    // cout << "$" << s << " " << k << endl;
    mx = min(mx, n);
    while (k--) {
      string rs = s;
      reverse(rs.begin(), rs.end());
      string t = s + rs;
      s = t.substr(s.length() + mx - n, n);
      mx *= 2;
      mx = min(mx, n);
      if (mx == n) {
        break;
      }
    }
    // cout << s << endl;
  }
  cout << one_step(s) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int max_n = 5555, inf = 1000111222;

int n, k;
string s;

string getr(const string &s, int pos, int mx) {
  if (mx + (mx << k) >= n) {
    return "";
  }
  // cout << "$" << mx << endl;
  return {s.rbegin() + s.length() - 1 - pos + mx,
          s.rbegin() + s.length() - 1 - pos + n - (mx << k)};
}

string one_step(const string &s) {
  string rs = s;
  reverse(rs.begin(), rs.end());
  string t = s + rs;
  string res = s;
  for (int i = 1; i <= n; ++i) {
    res = min(res, t.substr(i, n));
  }
  return res;
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  cin >> n >> k >> s;
  --k;
  char mn = s[0];
  for (char c : s) {
    mn = min(mn, c);
  }
  if (k) {
    k = min(k, 20);
    --k;
    string rs = s;
    reverse(rs.begin(), rs.end());
    int mx = 0, pos = 0;
    string t = s + rs;
    for (int i = 0; i < t.length(); ++i) {
      int st = i;
      while (i < t.length() && t[i] == mn) {
        ++i;
      }
      int cnt = i - st;
      if (cnt == 0) {
        continue;
      }
      if (i < n) {
        continue;
      }
      if (cnt == 1) {
        // cout << i << ": " << getr(t, i - 1, cnt) << endl;
      }
      if (i >= n &&
          (mx < cnt || mx == cnt && getr(t, pos, mx) > getr(t, i - 1, mx))) {
        pos = i - 1;
        mx = cnt;
      }
    }
    // cout << t << endl;
    // cout << mx << " " << pos << " " << getr(t, pos) << endl;
    s = t.substr(pos - n + 1, n);
    // reverse(s.begin(), s.end());
    // cout << "$" << s << " " << k << endl;
    mx = min(mx, n);
    while (k--) {
      string rs = s;
      reverse(rs.begin(), rs.end());
      string t = s + rs;
      s = t.substr(s.length() + mx - n, n);
      mx *= 2;
      mx = min(mx, n);
      if (mx == n) {
        break;
      }
    }
    // cout << s << endl;
  }
  cout << one_step(s) << endl;
  return 0;
}