Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, f, n) for (int(i) = (f); (i) < (n); (i)++)
#define RFOR(i, f, n) for (int(i) = (f); (i) >= (n); (i)--)
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define RREP(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define forin(i, a) for (auto i : (a))
#define p(...)                                                                 \
  printf(__VA_ARGS__);                                                         \
  cout << endl;
#define in(...)                                                                \
  __VA_ARGS__;                                                                 \
  input__(__VA_ARGS__);
#define input(...) in(__VA_ARGS__)
#define out(...) output__(__VA_ARGS__);
#define output(...) out(__VA_ARGS__)
#define list_input(x, n)                                                       \
  x[(n)];                                                                      \
  for (int(i) = 0; (i) < (n); (i)++)                                           \
    cin >> x[i];
#define lambda(a, b, c) [](auto const &a, auto const &b) { return c; }
#define key(t, x, compare)                                                     \
  [](const t &a, const t &b) { return a x compare b x; }
#define unless(a) if (!(a))

using namespace std;

using pii = pair<int, int>;

template <class... T> void input__(T &...a) {
  for (auto v : {&a...}) {
    cin >> *v;
  }
}

void output__() { cout << endl; }

template <class Head, class... T> void output__(Head const &v, T const &...a) {
  if (sizeof...(a) == 0) {
    cout << v;
  } else {
    cout << v << " ";
  }
  output__(a...);
}

int main() {
  string in(s);
  int in(n);
  vector<string> result;
  FOR(i, 0, s.length()) {
    FOR(j, 1, s.length() + 1 - i) { result.push_back(s.substr(i, j)); }
  }

  set<string> sresult(result.begin(), result.end());
  vector<string> vresult(sresult.begin(), sresult.end());
  sort(vresult.begin(), vresult.end());
  out(vresult[n - 1]);
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, f, n) for (int(i) = (f); (i) < (n); (i)++)
#define RFOR(i, f, n) for (int(i) = (f); (i) >= (n); (i)--)
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define RREP(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define forin(i, a) for (auto i : (a))
#define p(...)                                                                 \
  printf(__VA_ARGS__);                                                         \
  cout << endl;
#define in(...)                                                                \
  __VA_ARGS__;                                                                 \
  input__(__VA_ARGS__);
#define input(...) in(__VA_ARGS__)
#define out(...) output__(__VA_ARGS__);
#define output(...) out(__VA_ARGS__)
#define list_input(x, n)                                                       \
  x[(n)];                                                                      \
  for (int(i) = 0; (i) < (n); (i)++)                                           \
    cin >> x[i];
#define lambda(a, b, c) [](auto const &a, auto const &b) { return c; }
#define key(t, x, compare)                                                     \
  [](const t &a, const t &b) { return a x compare b x; }
#define unless(a) if (!(a))

using namespace std;

using pii = pair<int, int>;

template <class... T> void input__(T &...a) {
  for (auto v : {&a...}) {
    cin >> *v;
  }
}

void output__() { cout << endl; }

template <class Head, class... T> void output__(Head const &v, T const &...a) {
  if (sizeof...(a) == 0) {
    cout << v;
  } else {
    cout << v << " ";
  }
  output__(a...);
}

int main() {
  string in(s);
  int in(n);
  vector<string> result;
  FOR(i, 0, s.length()) {
    int target = min(6, (int)s.length() + 1 - i);
    FOR(j, 1, target) { result.push_back(s.substr(i, j)); }
  }

  set<string> sresult(result.begin(), result.end());
  vector<string> vresult(sresult.begin(), sresult.end());
  sort(vresult.begin(), vresult.end());
  out(vresult[n - 1]);
}