Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Gosu_Hiroo
 */

#include <fstream>
#include <iostream>

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define TR(container, it)                                                      \
  for (auto it = container.begin(); it != container.end(); it++)
#define IN(c, x) ((c).find(x) != (c).end())                       // O(log n)
#define IN_L(c, x) (find((c).begin(), (c).end(), x) != (c).end()) // O(n)
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define VI vector<int>
#define VVI vector<vector<int>>
#define FOR(i, _begin, _end)                                                   \
  for (__typeof(_end) end = _end, begin = _begin,                              \
                      i = (begin) - ((begin) > (end));                         \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define REP(i, end) for (__typeof(end) i = 0, _len = (end); i < (_len); i += 1)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define F first
#define S second
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define MOD(x, m) ((((x) % (m)) + (m)) % (m))
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define READ                                                                   \
  ({                                                                           \
    int t;                                                                     \
    cin >> t;                                                                  \
    t;                                                                         \
  })
#define EB emplace_back
#define fcout cout << fixed << setprecision(12)
#define fcerr cerr << fixed << setprecision(12)

#ifdef DEBUG
#define ERR(args...)                                                           \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    _err(cerr, _it, args);                                                     \
  }
#define DBG(x) cerr << #x << " is " << x << endl;
#else
#define DBG(x)                                                                 \
  {}
#define ERR(args...)                                                           \
  {}
#endif
void _err(std::ostream &cerr, istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void _err(std::ostream &cerr, istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << ", ";
  _err(cerr, ++it, args...);
}

const double pi = 2 * acos(.0);
const int inf = 0x3f3f3f3f;
const ll mod = (ll)(1e9) + 7;
// const ll mod = (ll) 998244353 ;
const double eps = 1e-10;

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> V) {
  for (auto v : V)
    os << v << " ";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const set<T> V) {
  for (auto v : V)
    os << v << " ";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const deque<T> V) {
  for (auto v : V)
    os << v << " ";
  return os;
}

template <class T> bool Chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool Chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

// #define int ll

class CKThSubstring {
public:
  string s;
  int k;

  void solve(std::istream &cin, std::ostream &cout, std::ostream &cerr) {
    cin >> s >> k;
    auto cmp = [](string s1, string s2) {
      return lexicographical_compare(ALL(s1), ALL(s2));
    };
    set<string, decltype(cmp)> vs(cmp);
    int N = SZ(s);
    REP(i, N) REP(j, N) if (i + j < N) vs.insert(s.substr(i, N - i - j));
    for (auto s : vs)
      ERR(s)
    cout << *next(vs.begin(), k - 1) << endl;
  }
};

#undef int
int main() {

  CKThSubstring solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  std::ostringstream err;
  in.tie(0);
  ios::sync_with_stdio(0);
  //	solver.solve(in, out);
  solver.solve(in, out, err);
  return 0;
}


Fixed submission:
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Gosu_Hiroo
 */

#include <fstream>
#include <iostream>

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define TR(container, it)                                                      \
  for (auto it = container.begin(); it != container.end(); it++)
#define IN(c, x) ((c).find(x) != (c).end())                       // O(log n)
#define IN_L(c, x) (find((c).begin(), (c).end(), x) != (c).end()) // O(n)
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define VI vector<int>
#define VVI vector<vector<int>>
#define FOR(i, _begin, _end)                                                   \
  for (__typeof(_end) end = _end, begin = _begin,                              \
                      i = (begin) - ((begin) > (end));                         \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define REP(i, end) for (__typeof(end) i = 0, _len = (end); i < (_len); i += 1)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define F first
#define S second
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define MOD(x, m) ((((x) % (m)) + (m)) % (m))
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define READ                                                                   \
  ({                                                                           \
    int t;                                                                     \
    cin >> t;                                                                  \
    t;                                                                         \
  })
#define EB emplace_back
#define fcout cout << fixed << setprecision(12)
#define fcerr cerr << fixed << setprecision(12)

#ifdef DEBUG
#define ERR(args...)                                                           \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    _err(cerr, _it, args);                                                     \
  }
#define DBG(x) cerr << #x << " is " << x << endl;
#else
#define DBG(x)                                                                 \
  {}
#define ERR(args...)                                                           \
  {}
#endif
void _err(std::ostream &cerr, istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void _err(std::ostream &cerr, istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << ", ";
  _err(cerr, ++it, args...);
}

const double pi = 2 * acos(.0);
const int inf = 0x3f3f3f3f;
const ll mod = (ll)(1e9) + 7;
// const ll mod = (ll) 998244353 ;
const double eps = 1e-10;

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> V) {
  for (auto v : V)
    os << v << " ";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const set<T> V) {
  for (auto v : V)
    os << v << " ";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const deque<T> V) {
  for (auto v : V)
    os << v << " ";
  return os;
}

template <class T> bool Chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool Chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

// #define int ll

class CKThSubstring {
public:
  string s;
  int k;

  void solve(std::istream &cin, std::ostream &cout, std::ostream &cerr) {
    cin >> s >> k;
    auto cmp = [](string s1, string s2) {
      return lexicographical_compare(ALL(s1), ALL(s2));
    };
    set<string, decltype(cmp)> vs(cmp);
    int N = SZ(s);
    REP(i, 5) REP(j, N - i) vs.insert(s.substr(j, i + 1));
    for (auto s : vs)
      ERR(s)
    cout << *next(vs.begin(), k - 1) << endl;
  }
};

#undef int
int main() {

  CKThSubstring solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  std::ostringstream err;
  in.tie(0);
  ios::sync_with_stdio(0);
  //	solver.solve(in, out);
  solver.solve(in, out, err);
  return 0;
}