Problem p03355 (C++)

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

using namespace std;

#ifdef DEBUG_MODE
#define DBG(n) n;
#else
#define DBG(n) ;
#endif
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    for (int W = 0; W < (n); W++)                                              \
      cerr << v[W] << ' ';                                                     \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, i, j)                                                        \
  {                                                                            \
    for (int aaa = 0; aaa < i; aaa++) {                                        \
      for (int bbb = 0; bbb < j; bbb++)                                        \
        cerr << v[aaa][bbb] << ' ';                                            \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define MOD 1000000007

typedef long long ll;
typedef pair<ll, ll> P;

int main() {
  string str;
  cin >> str;
  int k;
  cin >> k;
  int cou = 0;
  set<string> v;
  REP(i, 26) {
    REP(j, str.size()) {
      if (str[j] == (char)('a' + i)) {
        for (int k = j; k < str.size(); k++) {
          v.insert(str.substr(j, k - j + 1));
        }
      }
    }
    DBG(cout << v.size() << endl;);
    if (v.size() >= k) {
      auto it = v.begin();
      REP(i, k - 1) it++;
      cout << *it << endl;
      return 0;
    }
  }

  return 0;
}


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

using namespace std;

#ifdef DEBUG_MODE
#define DBG(n) n;
#else
#define DBG(n) ;
#endif
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    for (int W = 0; W < (n); W++)                                              \
      cerr << v[W] << ' ';                                                     \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, i, j)                                                        \
  {                                                                            \
    for (int aaa = 0; aaa < i; aaa++) {                                        \
      for (int bbb = 0; bbb < j; bbb++)                                        \
        cerr << v[aaa][bbb] << ' ';                                            \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define MOD 1000000007

typedef long long ll;
typedef pair<ll, ll> P;

int main() {
  string str;
  cin >> str;
  int k;
  cin >> k;
  int cou = 0;
  set<string> v;
  REP(i, 26) {
    REP(j, str.size()) {
      if (str[j] == (char)('a' + i)) {
        for (int k = j; k < str.size(); k++) {
          v.insert(str.substr(j, k - j + 1));
          if (v.size() > 5) {
            auto it = v.begin();
            REP(i, 5) it++;
            if (*it <= str.substr(j, k - j + 1))
              break;
          }
        }
      }
    }
    DBG(cout << v.size() << endl;);
    if (v.size() >= k) {
      auto it = v.begin();
      REP(i, k - 1) it++;
      cout << *it << endl;
      return 0;
    }
  }

  return 0;
}