Problem p03355 (C++)

Buggy submission (status: Runtime Error):
/////////// <----------------------  Quicksilver_ --------------------->
//////////////
#include <algorithm>
#include <bits/stdc++.h>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <utility>

using namespace std;
#define endl "\n"
#define ll long long int
#define c(x)                                                                   \
  ll x;                                                                        \
  cin >> x
#define cc(x, y)                                                               \
  ll x, y;                                                                     \
  cin >> x >> y
#define ccc(x, y, z)                                                           \
  ll x, y, z;                                                                  \
  cin >> x >> y >> z
#define db long double
#define fast                                                                   \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);                                                              \
  ios_base::sync_with_stdio(false)
#define inf 200000000000000
#define mod 1000000007
#define eps 1e-7
#define PI 3.1415926535897932385
#define pb push_back
#define bitc __builtin_popcountll
#define mp make_pair
#define ff first
#define ss second
#define all(ar) ar.begin(), ar.end()
#define len(x) (ll)(x).size()
#define fr(i, a, b) for (ll i = (a), _b = (b); i <= _b; i++)
#define rep(i, n) for (ll i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define frr(i, a, b) for (ll i = (a), _b = (b); i >= _b; i--)
#define foreach(it, ar) for (auto it = ar.begin(); it != ar.end(); it++)
#define fill(ar, val) memset(ar, val, sizeof(ar))
#define print(arr, n)                                                          \
  cout << " :: " << #arr << " :: " << endl;                                    \
  rep(i, n) cout << arr[i] << " ";                                             \
  cout << endl
#define print2(arr, n, m)                                                      \
  cout << " :: " << #arr << " :: " << endl;                                    \
  rep(i, n) {                                                                  \
    rep(j, m) cout << arr[i][j] << " ";                                        \
    cout << endl;                                                              \
  }
#define dyn1(arr, n)                                                           \
  ll *arr = new ll[n];                                                         \
  rep(i, n) arr[i] = 0;
#define dyn2(arr, n, m)                                                        \
  ll **arr = new ll *[(n)];                                                    \
  rep(i, n) arr[i] = new ll[m];                                                \
  rep(i, n) rep(j, m) arr[i][j] = 0
#define carr(arr, n) rep(i, n) cin >> arr[i]
#define carr2(arr, n, m) rep(i, n) rep(j, m) cin >> arr[i][j]
#define debug(a) cout << " :: " << #a << " == " << a << endl
#define debug2(a, b)                                                           \
  cout << " :: " << #a << " == " << a << " :: " << #b << " == " << b << endl
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

inline ll maxim(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}
inline ll minim(ll a, ll b) {
  if (a < b)
    return a;
  else
    return b;
}
inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

bool prime[10002];
void SieveOfEratosthenes() {
  memset(prime, true, sizeof(prime));
  prime[1] = false;
  for (int p = 2; p * p <= 10001; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= 10001; i += p)
        prime[i] = false;
    }
  }
}

ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}

ll modpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

int modInverse(int a, int m) {
  int m0 = m;
  int y = 0, x = 1;
  if (m == 1)
    return 0;
  while (a > 1) {
    // q is quotient
    int q = a / m;
    int t = m;
    // m is remainder now, process same as
    // Euclid's algo
    m = a % m, a = t;
    t = y;
    // Update y and x
    y = x - q * y;
    x = t;
  }
  // Make x positive
  if (x < 0)
    x += m0;
  return x;
}

vector<ll> adj[1000010];
void adj_former(ll n) {
  rep(o, n - 1) {
    cc(a, b);
    adj[a].pb(b);
    adj[b].pb(a);
  }
}

ll arr[1000010];
ll visited[1000010] = {0};
ll mat[50][50];

vector<pair<db, ll>> cont;

int main() {
  fast;

#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif

  cout << setprecision(10) << fixed;
  string s;
  cin >> s;
  ll n = s.length();
  c(k);
  vector<string> cont;
  fr(o, 0, n - 1) {
    fr(p, 1, k) {
      if (o + p <= n)
        cont.pb(s.substr(o, p));
    }
  }
  sort(cont.begin(), cont.end());
  vector<string>::iterator ip = std::unique(cont.begin(), cont.end());
  cont.resize(std::distance(cont.begin(), ip));
  cout << cont[k - 1] << endl;

  return 0;
}


Fixed submission:
/////////// <----------------------  Quicksilver_ --------------------->
//////////////
#include <algorithm>
#include <bits/stdc++.h>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <utility>

using namespace std;
#define endl "\n"
#define ll long long int
#define c(x)                                                                   \
  ll x;                                                                        \
  cin >> x
#define cc(x, y)                                                               \
  ll x, y;                                                                     \
  cin >> x >> y
#define ccc(x, y, z)                                                           \
  ll x, y, z;                                                                  \
  cin >> x >> y >> z
#define db long double
#define fast                                                                   \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);                                                              \
  ios_base::sync_with_stdio(false)
#define inf 200000000000000
#define mod 1000000007
#define eps 1e-7
#define PI 3.1415926535897932385
#define pb push_back
#define bitc __builtin_popcountll
#define mp make_pair
#define ff first
#define ss second
#define all(ar) ar.begin(), ar.end()
#define len(x) (ll)(x).size()
#define fr(i, a, b) for (ll i = (a), _b = (b); i <= _b; i++)
#define rep(i, n) for (ll i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define frr(i, a, b) for (ll i = (a), _b = (b); i >= _b; i--)
#define foreach(it, ar) for (auto it = ar.begin(); it != ar.end(); it++)
#define fill(ar, val) memset(ar, val, sizeof(ar))
#define print(arr, n)                                                          \
  cout << " :: " << #arr << " :: " << endl;                                    \
  rep(i, n) cout << arr[i] << " ";                                             \
  cout << endl
#define print2(arr, n, m)                                                      \
  cout << " :: " << #arr << " :: " << endl;                                    \
  rep(i, n) {                                                                  \
    rep(j, m) cout << arr[i][j] << " ";                                        \
    cout << endl;                                                              \
  }
#define dyn1(arr, n)                                                           \
  ll *arr = new ll[n];                                                         \
  rep(i, n) arr[i] = 0;
#define dyn2(arr, n, m)                                                        \
  ll **arr = new ll *[(n)];                                                    \
  rep(i, n) arr[i] = new ll[m];                                                \
  rep(i, n) rep(j, m) arr[i][j] = 0
#define carr(arr, n) rep(i, n) cin >> arr[i]
#define carr2(arr, n, m) rep(i, n) rep(j, m) cin >> arr[i][j]
#define debug(a) cout << " :: " << #a << " == " << a << endl
#define debug2(a, b)                                                           \
  cout << " :: " << #a << " == " << a << " :: " << #b << " == " << b << endl
typedef pair<ll, ll> pii;
typedef vector<pii> vii;

inline ll maxim(ll a, ll b) {
  if (a > b)
    return a;
  else
    return b;
}
inline ll minim(ll a, ll b) {
  if (a < b)
    return a;
  else
    return b;
}
inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

bool prime[10002];
void SieveOfEratosthenes() {
  memset(prime, true, sizeof(prime));
  prime[1] = false;
  for (int p = 2; p * p <= 10001; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= 10001; i += p)
        prime[i] = false;
    }
  }
}

ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}

ll modpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

int modInverse(int a, int m) {
  int m0 = m;
  int y = 0, x = 1;
  if (m == 1)
    return 0;
  while (a > 1) {
    // q is quotient
    int q = a / m;
    int t = m;
    // m is remainder now, process same as
    // Euclid's algo
    m = a % m, a = t;
    t = y;
    // Update y and x
    y = x - q * y;
    x = t;
  }
  // Make x positive
  if (x < 0)
    x += m0;
  return x;
}

vector<ll> adj[1000010];
void adj_former(ll n) {
  rep(o, n - 1) {
    cc(a, b);
    adj[a].pb(b);
    adj[b].pb(a);
  }
}

ll arr[1000010];
ll visited[1000010] = {0};
ll mat[50][50];

vector<pair<db, ll>> cont;

int main() {
  fast;

  // #ifndef ONLINE_JUDGE
  // // for getting input from input.txt
  // freopen("input.txt", "r", stdin);
  // // for writing output to output.txt
  // freopen("output.txt", "w", stdout);
  // #endif

  cout << setprecision(10) << fixed;
  string s;
  cin >> s;
  ll n = s.length();
  c(k);
  vector<string> cont;
  fr(o, 0, n - 1) {
    fr(p, 1, k) {
      if (o + p <= n)
        cont.pb(s.substr(o, p));
    }
  }
  sort(cont.begin(), cont.end());
  vector<string>::iterator ip = std::unique(cont.begin(), cont.end());
  cont.resize(std::distance(cont.begin(), ip));
  cout << cont[k - 1] << endl;

  return 0;
}