Problem p02943 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;
string ans;
bool checker(ll N, ll K) {
  ll now = 1;
  for (int i = 3; i < K; i++) {
    now *= 2;
    if (now >= N) {
      return true;
    }
  }
  return false;
}

void f(int i, string S, int K, int len) {
  string T = S;
  int N = S.size();
  reverse(T.begin(), T.end());
  S += T;
  /*
  if(len == 0 && K == 1) {
      string ret = S.substr(i, N);
      cerr << ret << endl;
      chmin(ans, ret);
      return;
  }
  */
  if (K == 1) {
    string ret = S.substr(max(0, N - len), N);
    cerr << "ans: " << ret << endl;
    chmin(ans, ret);
    return;
  }
  if (i != -1) {
    string ret = S.substr(2 * N - i - N, N);
    /// cerr << i << " " << len << " " << ret << endl;
    if (i + len != N)
      f(-1, ret, K - 1, len);
    if (i + len == N)
      f(-1, ret, K - 1, min(N, len * 2));
    return;
  }
  // cerr << "now:" << S << endl;
  string ret = S.substr(min(len, N), N);
  cerr << ret << endl;
  f(-1, ret, K - 1, min(N, len * 2));
}

string solve(int N, int K, string S) {
  if (K == 0)
    return S;
  string ans = "z";
  ans[0]++;
  string T = S;
  reverse(T.begin(), T.end());
  S += T;
  for (int i = 0; i <= N; i++) {
    string tmp = S.substr(i, N);
    string ret = solve(N, K - 1, tmp);
    chmin(ans, ret);
  }
  return ans;
}

int main() {
  // cout.precision(10);
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  string S;
  cin >> N >> K >> S;
  if (K <= 2) {
    cout << solve(N, K, S) << endl;
    return 0;
  }
  if (checker(N, K)) {
    char mini = 'z';
    for (int i = 0; i < S.size(); i++) {
      chmin(mini, S[i]);
    }
    for (int i = 0; i < S.size(); i++) {
      cout << mini;
    }
    cout << endl;
    return 0;
  }
  char mini = 'z';
  for (int i = 0; i < S.size(); i++) {
    chmin(mini, S[i]);
  }
  ans = "z";
  ans[0]++;
  // cerr << ans << endl;
  /*
  for(int i = 0; i < S.size(); i++) {
      cerr << "----" << i << "----" << endl;
      f(i, S, K, 0);
  }
  */
  int l = 0;
  for (int r = 0; r < S.size(); r++) {
    if (S[r] == mini)
      continue;
    if (l != r) {
      cerr << l << " " << r << endl;
      f(l, S, K, r - l);
    }
    l = r + 1;
  }
  if (l != S.size()) {
    cerr << l << " " << S.size() - l << endl;
    f(l, S, K, S.size() - l);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;
string ans;
bool checker(ll N, ll K) {
  ll now = 1;
  for (int i = 3; i < K; i++) {
    now *= 2;
    if (now >= N) {
      return true;
    }
  }
  return false;
}

void f(int i, string S, int K, int len) {
  string T = S;
  int N = S.size();
  reverse(T.begin(), T.end());
  S += T;
  /*
  if(len == 0 && K == 1) {
      string ret = S.substr(i, N);
      cerr << ret << endl;
      chmin(ans, ret);
      return;
  }
  */
  if (K == 1) {
    string ret = S.substr(max(0, N - len), N);
    cerr << "ans: " << ret << endl;
    chmin(ans, ret);
    return;
  }
  if (i != -1) {
    string ret = S.substr(2 * N - i - N, N);
    /// cerr << i << " " << len << " " << ret << endl;
    if (i + len != N)
      f(-1, ret, K - 1, len);
    if (i + len == N)
      f(-1, ret, K - 1, min(N, len * 2));
    return;
  }
  // cerr << "now:" << S << endl;
  string ret = S.substr(min(len, N), N);
  cerr << ret << endl;
  f(-1, ret, K - 1, min(N, len * 2));
}

string solve(int N, int K, string S) {
  if (K == 0)
    return S;
  string ans = "z";
  ans[0]++;
  string T = S;
  reverse(T.begin(), T.end());
  S += T;
  for (int i = 0; i <= N; i++) {
    string tmp = S.substr(i, N);
    string ret = solve(N, K - 1, tmp);
    chmin(ans, ret);
  }
  return ans;
}

int main() {
  // cout.precision(10);
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  string S;
  cin >> N >> K >> S;
  if (K <= 1) {
    cout << solve(N, K, S) << endl;
    return 0;
  }
  if (checker(N, K)) {
    char mini = 'z';
    for (int i = 0; i < S.size(); i++) {
      chmin(mini, S[i]);
    }
    for (int i = 0; i < S.size(); i++) {
      cout << mini;
    }
    cout << endl;
    return 0;
  }
  char mini = 'z';
  for (int i = 0; i < S.size(); i++) {
    chmin(mini, S[i]);
  }
  ans = "z";
  ans[0]++;
  // cerr << ans << endl;
  /*
  for(int i = 0; i < S.size(); i++) {
      cerr << "----" << i << "----" << endl;
      f(i, S, K, 0);
  }
  */
  int l = 0;
  for (int r = 0; r < S.size(); r++) {
    if (S[r] == mini)
      continue;
    if (l != r) {
      cerr << l << " " << r << endl;
      f(l, S, K, r - l);
    }
    l = r + 1;
  }
  if (l != S.size()) {
    cerr << l << " " << S.size() - l << endl;
    f(l, S, K, S.size() - l);
  }
  cout << ans << endl;
  return 0;
}