Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
// #define NDEBUG

#include <bits/extc++.h>
#include <bits/stdc++.h>

#define StarBurstStream                                                        \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define iceil(a, b) (((a) + (b)-1) / (b))
#define tomax(a, b) ((a) = max((a), (b)))
#define printv(a, b)                                                           \
  {                                                                            \
    bool pvaspace = false;                                                     \
    for (auto pva : a) {                                                       \
      if (pvaspace)                                                            \
        b << " ";                                                              \
      pvaspace = true;                                                         \
      b << pva;                                                                \
    }                                                                          \
    b << "\n";                                                                 \
  }

// #define TEST

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;

const ll MOD = 1000000007;
const ll MAX = 2147483647;

template <typename A, typename B>
ostream &operator<<(ostream &o, pair<A, B> p) {
  return o << '(' << p.F << ',' << p.S << ')';
}

int main() {
  StarBurstStream

      string s;
  cin >> s;
  int k;
  cin >> k;

  int n = s.size();
  set<string> st;
  for (int i = 0; i < n; i++) {
    for (int j = 1; i + j <= n; j++) {
      st.insert(s.substr(i, j));
      if (st.size() > k)
        st.erase(prev(st.end()));
    }
  }
  cout << *st.rbegin() << "\n";

  return 0;
}

Fixed submission:
// #define NDEBUG

#include <bits/extc++.h>
#include <bits/stdc++.h>

#define StarBurstStream                                                        \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define iceil(a, b) (((a) + (b)-1) / (b))
#define tomax(a, b) ((a) = max((a), (b)))
#define printv(a, b)                                                           \
  {                                                                            \
    bool pvaspace = false;                                                     \
    for (auto pva : a) {                                                       \
      if (pvaspace)                                                            \
        b << " ";                                                              \
      pvaspace = true;                                                         \
      b << pva;                                                                \
    }                                                                          \
    b << "\n";                                                                 \
  }

// #define TEST

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;

const ll MOD = 1000000007;
const ll MAX = 2147483647;

template <typename A, typename B>
ostream &operator<<(ostream &o, pair<A, B> p) {
  return o << '(' << p.F << ',' << p.S << ')';
}

int main() {
  StarBurstStream

      string s;
  cin >> s;
  int k;
  cin >> k;

  int n = s.size();
  set<string> st;
  for (int i = 0; i < n; i++) {
    for (int j = 1; i + j <= n && j <= k; j++) {
      st.insert(s.substr(i, j));
      if (st.size() > k)
        st.erase(prev(st.end()));
    }
  }
  cout << *st.rbegin() << "\n";

  return 0;
}