Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int64_t i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REV(c) reverse((c).begin(), (c).end())
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define COUTDOUBLE(d, i) cout << setprecision((i)) << (d) << endl;
#define VIN(v)                                                                 \
  for (int64_t i = 0; i < (v).size(); i++) {                                   \
    cin >> (v).at(i);                                                          \
  }
#define MREP(i, m)                                                             \
  for (auto i = (m).begin(); i != (m).end();                                   \
       ++i) // auto key = i->first;auto value = i->second;

using namespace std;
typedef int64_t I;
typedef pair<I, I> P;
typedef vector<I> V;

I MOD = pow(10, 9) + 7;
I MAX = LONG_MAX;
I MIN = LONG_MIN;

// 最大公約数
I gcd(I a, I b) { return b != 0 ? gcd(b, a % b) : a; }

// 最小公倍数
I lcm(I a, I b) { return a * b / gcd(a, b); }

int ctoi(char c) { return c - '0'; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  I k;
  cin >> s >> k;
  vector<string> v;
  REP(i, s.size()) {
    string ss;
    ss += s.at(i);
    v.push_back(ss);
    if (i < s.size() - 1) {
      for (I j = i + 1; j < s.size(); j++) {
        ss += s.at(j);
        v.push_back(ss);
      }
    }
  }
  SORT(v);
  unique(ALL(v));
  REP(i, v.size()) {
    if (i == k - 1) {
      cout << v.at(i) << endl;
      return 0;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int64_t i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REV(c) reverse((c).begin(), (c).end())
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define COUTDOUBLE(d, i) cout << setprecision((i)) << (d) << endl;
#define VIN(v)                                                                 \
  for (int64_t i = 0; i < (v).size(); i++) {                                   \
    cin >> (v).at(i);                                                          \
  }
#define MREP(i, m)                                                             \
  for (auto i = (m).begin(); i != (m).end();                                   \
       ++i) // auto key = i->first;auto value = i->second;

using namespace std;
typedef int64_t I;
typedef pair<I, I> P;
typedef vector<I> V;

I MOD = pow(10, 9) + 7;
I MAX = LONG_MAX;
I MIN = LONG_MIN;

// 最大公約数
I gcd(I a, I b) { return b != 0 ? gcd(b, a % b) : a; }

// 最小公倍数
I lcm(I a, I b) { return a * b / gcd(a, b); }

int ctoi(char c) { return c - '0'; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  I k;
  cin >> s >> k;
  vector<string> v;
  REP(i, s.size()) {
    string ss;
    ss += s.at(i);
    v.push_back(ss);
    if (i < s.size() - 1) {
      I si = s.size();
      si = min(si, i + k + 1);
      for (I j = i + 1; j < si; j++) {
        ss += s.at(j);
        v.push_back(ss);
      }
    }
  }
  SORT(v);
  unique(ALL(v));
  REP(i, v.size()) {
    if (i == k - 1) {
      cout << v.at(i) << endl;
      return 0;
    }
  }
  return 0;
}