Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
// #define int long long

#define LOG(variable) cout << #variable ":\t" << (variable) << endl
#define LOGCON(i, container)                                                   \
  for (int(i) = 0; (i) < (container).size(); ++(i))                            \
  cout << (i) << ":\t" << (container)[(i)] << endl
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPS(i, r, n) for (int i = (r); i < (n); ++i)
#define REPR(i, n) for (int i = (n); i >= 0; --i)       // from n to 0
#define REPRS(i, r, n) for (int i = (n); i >= (r); --i) // from n to r
#define REPOBJ(itr, obj)                                                       \
  for (auto itr = (obj).begin(); itr != (obj).end(); ++itr)
#define REPROBJ(itr, obj)                                                      \
  for (auto itr = (obj).rbegin(), e = (obj).rend(); itr != e; ++itr)
#define COUTB(x) cout << (x) << "\n"
#define COUTS(x) cout << (x) << " "
#define PB push_back
#define SORT(obj) sort((obj).begin(), (obj).end())
#define SORTR(obj) sort((obj).begin(), (obj).end(), greater<>())
#define ALL(obj) (obj).begin(), (obj).end()
#define MOD 1000000007
#define PI (acos(-1))
/***** MAIN *****/
signed main() {
  string s;
  int k;
  cin >> s;
  cin >> k;
  vector<string> v;
  REP(start_i, s.length()) {
    REPS(end_i, start_i, s.length()) {
      v.PB(s.substr(start_i, end_i - start_i + 1));
    }
  }
  SORT(v);
  v.erase(unique(ALL(v)), v.end());
  cout << v[k - 1];
  cout << "\n";
  return 0;
}
/***** MAIN *****/


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
// #define int long long

#define LOG(variable) cout << #variable ":\t" << (variable) << endl
#define LOGCON(i, container)                                                   \
  for (int(i) = 0; (i) < (container).size(); ++(i))                            \
  cout << (i) << ":\t" << (container)[(i)] << endl
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPS(i, r, n) for (int i = (r); i < (n); ++i)
#define REPR(i, n) for (int i = (n); i >= 0; --i)       // from n to 0
#define REPRS(i, r, n) for (int i = (n); i >= (r); --i) // from n to r
#define REPOBJ(itr, obj)                                                       \
  for (auto itr = (obj).begin(); itr != (obj).end(); ++itr)
#define REPROBJ(itr, obj)                                                      \
  for (auto itr = (obj).rbegin(), e = (obj).rend(); itr != e; ++itr)
#define COUTB(x) cout << (x) << "\n"
#define COUTS(x) cout << (x) << " "
#define PB push_back
#define SORT(obj) sort((obj).begin(), (obj).end())
#define SORTR(obj) sort((obj).begin(), (obj).end(), greater<>())
#define ALL(obj) (obj).begin(), (obj).end()
#define MOD 1000000007
#define PI (acos(-1))
/***** MAIN *****/
signed main() {
  string s;
  int k;
  cin >> s;
  cin >> k;
  vector<string> v;
  REP(start_i, s.length()) {
    REPS(end_i, start_i, s.length()) {
      int length = end_i - start_i + 1;
      if (length > k)
        continue;
      v.PB(s.substr(start_i, end_i - start_i + 1));
    }
  }
  SORT(v);
  v.erase(unique(ALL(v)), v.end());
  cout << v[k - 1];
  cout << "\n";
  return 0;
}
/***** MAIN *****/