Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define BEGIN                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define END return EXIT_SUCCESS
#define FOR(I, A, B) for ((I) = (A); (I) < (B); (I)++)
#define REP(I, N) FOR(I, 0, N)
#define ALL(C) (C).begin(), (C).end()
#define INPUT(P) std::cin >> (P)
#define INPUT2(P1, P2) std::cin >> (P1) >> (P2)
#define INPUT3(P1, P2, P3) std::cin >> (P1) >> (P2) >> (P3)
#define INPUTS(I, N, V) REP(I, N) std::cin >> V[I]
#define OUTPUT(P) std::cout << P << std::endl
#define OUTPUT2(P1, P2) std::cout << P1 << " " << P2 << std::endl
#define OUTPUT3(P1, P2, P3)                                                    \
  std::cout << P1 << " " << P2 << " " << P3 << std::endl
#define OUTPUTS(I, N, V) REP(I, N) std::cout << V[i] << std::endl
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
using namespace std;
using namespace __gnu_pbds;
int i, j, k;
string S;
int K;
set<string> substrs;
int N;
int len;
inline void solve() {
  INPUT2(S, K);
  N = S.size();
  FOR(len, 1, N + 1) {
    FOR(i, 0, N - len + 1) { substrs.insert(S.substr(i, len)); }
  }
  set<string>::iterator it = substrs.begin();
  advance(it, K - 1);
  OUTPUT(*it);
}

int main(int argc, char **argv) {
  BEGIN;
  solve();
  END;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define BEGIN                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define END return EXIT_SUCCESS
#define FOR(I, A, B) for ((I) = (A); (I) < (B); (I)++)
#define REP(I, N) FOR(I, 0, N)
#define ALL(C) (C).begin(), (C).end()
#define INPUT(P) std::cin >> (P)
#define INPUT2(P1, P2) std::cin >> (P1) >> (P2)
#define INPUT3(P1, P2, P3) std::cin >> (P1) >> (P2) >> (P3)
#define INPUTS(I, N, V) REP(I, N) std::cin >> V[I]
#define OUTPUT(P) std::cout << P << std::endl
#define OUTPUT2(P1, P2) std::cout << P1 << " " << P2 << std::endl
#define OUTPUT3(P1, P2, P3)                                                    \
  std::cout << P1 << " " << P2 << " " << P3 << std::endl
#define OUTPUTS(I, N, V) REP(I, N) std::cout << V[i] << std::endl
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
using namespace std;
using namespace __gnu_pbds;
int i, j, k;
string S;
int K;
set<string> substrs;
int N;
int len;
inline void solve() {
  INPUT2(S, K);
  N = S.size();
  FOR(len, 1, K + 1) {
    FOR(i, 0, N - len + 1) { substrs.insert(S.substr(i, len)); }
  }
  set<string>::iterator it = substrs.begin();
  advance(it, K - 1);
  OUTPUT(*it);
}

int main(int argc, char **argv) {
  BEGIN;
  solve();
  END;
}