Problem p03355 (C++)

Buggy submission (status: Runtime Error):

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

#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0)
#define mod 1000000007
#define setbits __builtin_popcount
#define gcd __gcd
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, b, a) for (int i = b; i >= a; i--)
#define foreach(itr, c)                                                        \
  for (__typeof__((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr)
#define foreachr(itr, c)                                                       \
  for (__typeof__((c).rbegin()) itr = (c).rbegin(); itr != (c).rend(); ++itr)
#define forn(i, n) for (int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
#define in(a, b) ((b).find(a) != (b).end())
#define pb push_back
#define fill(a, v) memset(a, v, sizeof a)
#define endl "\n"
#define ff first
#define ss second
#define mp make_pair
#define minin(a) *min_element(all(a))
#define maxin(a) *max_element(all(a))
#define asort(c) sort(all(c))
#define dsort(c) sort(all(c), greater<int>())
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef set<int> si;
typedef unordered_map<int, int> mii;

int main() {
  FIO;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  string s, sb;
  int k, n;
  cin >> s >> k;
  n = s.size();

  set<string> st;

  rep(i, 1, min(5, n)) {
    rep(j, 0, n - i) {
      sb = s.substr(j, i);
      st.insert(sb);
    }
  }

  int cnt = 1;

  for (auto str : st) {
    if (cnt == k) {
      cout << str << endl;
      break;
    }
    cnt++;
  }
  return 0;
}

Fixed submission:

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

#define FIO                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0)
#define mod 1000000007
#define setbits __builtin_popcount
#define gcd __gcd
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, b, a) for (int i = b; i >= a; i--)
#define foreach(itr, c)                                                        \
  for (__typeof__((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr)
#define foreachr(itr, c)                                                       \
  for (__typeof__((c).rbegin()) itr = (c).rbegin(); itr != (c).rend(); ++itr)
#define forn(i, n) for (int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
#define in(a, b) ((b).find(a) != (b).end())
#define pb push_back
#define fill(a, v) memset(a, v, sizeof a)
#define endl "\n"
#define ff first
#define ss second
#define mp make_pair
#define minin(a) *min_element(all(a))
#define maxin(a) *max_element(all(a))
#define asort(c) sort(all(c))
#define dsort(c) sort(all(c), greater<int>())
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef set<int> si;
typedef unordered_map<int, int> mii;

int main() {
  FIO;

  string s, sb;
  int k, n;
  cin >> s >> k;
  n = s.size();

  set<string> st;

  rep(i, 1, min(5, n)) {
    rep(j, 0, n - i) {
      sb = s.substr(j, i);
      st.insert(sb);
    }
  }

  int cnt = 1;

  for (auto str : st) {
    if (cnt == k) {
      cout << str << endl;
      break;
    }
    cnt++;
  }
  return 0;
}