Problem p02943 (C++)

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

#define LL long long
#define pii pair<int, int>
using namespace std;
const int inf = 2147483647;
const int N = 20001;

int _max(int x, int y) { return x > y ? x : y; }
int _min(int x, int y) { return x < y ? x : y; }
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
void put(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    put(x / 10);
  putchar(x % 10 + '0');
}

int n, k;
char ss[5001], hh[5001];

bool cmp(int x, int y) {
  for (int i = 1; i <= n; i++) {
    if (ss[x + i - 1] > ss[y + i - 1])
      return 1;
    else if (ss[x + i - 1] < ss[y + i - 1])
      return 0;
  }
  return 0;
}

int main() {
  n = read(), k = read();
  scanf("%s", ss + 1);
  int gg = 'z';
  for (int i = 1; i <= n; i++)
    gg = _min(gg, ss[i]);
  if (k > 30) {
    for (int i = 1; i <= n; i++)
      putchar(gg);
    return 0;
  } else {
    for (int i = n + 1; i <= 2 * n; i++)
      ss[i] = ss[n - (i - n) + 1];
    int now = 1;
    for (int i = 2; i <= n + 1; i++) {
      if (cmp(now, i))
        now = i;
    }
    for (int i = now; i <= now + n - 1; i++)
      hh[i - now + 1] = ss[i];
    k--;
    int s = 0;
    while (hh[s + 1] == hh[1])
      s++;
    while (k--) {
      for (int i = 1; i <= n; i++)
        ss[i] = hh[i];
      for (int i = 1; i <= _min(n, s * 2); i++)
        hh[i] = ss[1];
      for (int i = _min(n, s * 2) + 1, t = 1; i <= n; i++, ++t)
        hh[i] = ss[s + t];
      s = _min(s * 2, n);
      if (s == n)
        break;
    }
    printf("%s", hh + 1);
  }
  return 0;
}


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

#define LL long long
#define pii pair<int, int>
using namespace std;
const int inf = 2147483647;
const int N = 20001;

int _max(int x, int y) { return x > y ? x : y; }
int _min(int x, int y) { return x < y ? x : y; }
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
void put(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    put(x / 10);
  putchar(x % 10 + '0');
}

int n, k;
char ss[N], hh[N];

bool cmp(int x, int y) {
  for (int i = 1; i <= n; i++) {
    if (ss[x + i - 1] > ss[y + i - 1])
      return 1;
    else if (ss[x + i - 1] < ss[y + i - 1])
      return 0;
  }
  return 0;
}

int main() {
  n = read(), k = read();
  scanf("%s", ss + 1);
  int gg = 'z';
  for (int i = 1; i <= n; i++)
    gg = _min(gg, ss[i]);
  if (k > 30) {
    for (int i = 1; i <= n; i++)
      putchar(gg);
    return 0;
  } else {
    for (int i = n + 1; i <= 2 * n; i++)
      ss[i] = ss[n - (i - n) + 1];
    int now = 1;
    for (int i = 2; i <= n + 1; i++) {
      if (cmp(now, i))
        now = i;
    }
    for (int i = now; i <= now + n - 1; i++)
      hh[i - now + 1] = ss[i];
    k--;
    int s = 0;
    while (hh[s + 1] == hh[1])
      s++;
    while (k--) {
      for (int i = 1; i <= n; i++)
        ss[i] = hh[i];
      for (int i = 1; i <= _min(n, s * 2); i++)
        hh[i] = ss[1];
      for (int i = _min(n, s * 2) + 1, t = 1; i <= n; i++, ++t)
        hh[i] = ss[s + t];
      s = _min(s * 2, n);
      if (s == n)
        break;
    }
    printf("%s", hh + 1);
  }
  return 0;
}