Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, x, n) for (int i = x; i >= n; i--)
#define rrep(i, n) RREP(i, n, 0)
#define pb push_back

using namespace std;

// #define DEBUG_IS_VALID

#ifdef DEBUG_IS_VALID
#define DEB 1
#else
#define DEB 0
#endif
#define DUMPOUT cout
#define dump(...)                                                              \
  if (DEB)                                                                     \
    DUMPOUT << "  " << #__VA_ARGS__ << " :[" << __LINE__ << ":"                \
            << __FUNCTION__ << "]" << endl                                     \
            << "    ";                                                         \
  if (DEB)                                                                     \
  dump_func(__VA_ARGS__)
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  os << "{";
  for (int i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
  os << "}";
  return os;
}
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
  DUMPOUT << head;
  if (sizeof...(Tail) == 0) {
    DUMPOUT << " ";
  } else {
    DUMPOUT << ", ";
  }
  dump_func(std::move(tail)...);
}
template <class T> inline void chmax(T &a, T const &b) { a = max(a, b); }
template <class T> inline void chmin(T &a, T const &b) { a = min(a, b); }

using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

const int mod = 1e9 + 7, INF = 1 << 30;
const double EPS = 1e-12, PI = 3.1415926535897932384626;
const ll lmod = 1e9 + 7, LINF = 1LL << 60;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // int N,M; cin >> N >> M;
  // vi p(N); rep(i,N) cin >> p[i];
  string s;
  int K;
  cin >> s >> K;
  int N = s.size();
  int cnt = 0;
  vector<string> li;
  function<void(string)> go = [&](string a) {
    rep(i, li.size()) if (li[i] == a) return;
    li.pb(a);
    sort(ALL(li));
    dump(li);
    if (li.size() == K + 1)
      li.pop_back();
  };
  vi ary(N);
  iota(ary.begin(), ary.end(), 0);
  sort(ary.begin(), ary.end(), [&](int l, int r) { return s[l] < s[r]; });
  char lst = 'a';
  for (auto i : ary) {
    string a = "";
    if (N > 4000) {
      if (s[i] > lst && ary.size() == K)
        break;
    }
    a += s[i];
    go(a);
    REP(j, i + 1, N) {
      a += s[j];
      go(a);
      cnt++;
      if (cnt > 5 * int(1e7)) {
        cout << li[K - 1] << endl;
        return 0;
      }
    }
  }
  if (N == 1)
    li.pb(s);
  cout << li[K - 1] << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, x, n) for (int i = x; i >= n; i--)
#define rrep(i, n) RREP(i, n, 0)
#define pb push_back

using namespace std;

// #define DEBUG_IS_VALID

#ifdef DEBUG_IS_VALID
#define DEB 1
#else
#define DEB 0
#endif
#define DUMPOUT cout
#define dump(...)                                                              \
  if (DEB)                                                                     \
    DUMPOUT << "  " << #__VA_ARGS__ << " :[" << __LINE__ << ":"                \
            << __FUNCTION__ << "]" << endl                                     \
            << "    ";                                                         \
  if (DEB)                                                                     \
  dump_func(__VA_ARGS__)
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  os << "{";
  for (int i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
  os << "}";
  return os;
}
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
  DUMPOUT << head;
  if (sizeof...(Tail) == 0) {
    DUMPOUT << " ";
  } else {
    DUMPOUT << ", ";
  }
  dump_func(std::move(tail)...);
}
template <class T> inline void chmax(T &a, T const &b) { a = max(a, b); }
template <class T> inline void chmin(T &a, T const &b) { a = min(a, b); }

using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

const int mod = 1e9 + 7, INF = 1 << 30;
const double EPS = 1e-12, PI = 3.1415926535897932384626;
const ll lmod = 1e9 + 7, LINF = 1LL << 60;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // int N,M; cin >> N >> M;
  // vi p(N); rep(i,N) cin >> p[i];
  string s;
  int K;
  cin >> s >> K;
  int N = s.size();
  int cnt = 0;
  vector<string> li;
  function<void(string)> go = [&](string a) {
    rep(i, li.size()) if (li[i] == a) return;
    li.pb(a);
    sort(ALL(li));
    dump(li);
    if (li.size() == K + 1)
      li.pop_back();
  };
  vi ary(N);
  iota(ary.begin(), ary.end(), 0);
  sort(ary.begin(), ary.end(), [&](int l, int r) { return s[l] < s[r]; });
  char lst = 'a';
  for (auto i : ary) {
    string a = "";
    if (N > 4000) {
      if (s[i] > lst && ary.size() == K)
        break;
    }
    a += s[i];
    go(a);
    REP(j, i + 1, N) {
      a += s[j];
      go(a);
      cnt++;
      if (cnt > int(1e7)) {
        cout << li[K - 1] << endl;
        return 0;
      }
    }
  }
  if (N == 1)
    li.pb(s);
  cout << li[K - 1] << endl;
  return 0;
}