Problem p03353 (C++)

Buggy submission (status: Runtime Error):

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a, b) for (int i = a; i >= b; --i)
#define fore(i, a) for (auto &i : a)
#define INF INT_MAX / 2;
typedef long long ll;
using namespace std;
using vi = vector<int>;
using vll = vector<ll>;
ll mod = 1e9 + 7;
// ll llmax = 10000000000000000000ll;
using namespace std;
using Graph = vector<vector<int>>;

int cnt_digit(ll N) {
  int digit = 0;

  while (N > 0) {
    N /= 10;
    digit++;
  }
  return digit;
}

// 最大公約数計算

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}
// 最小公倍数の計算

ll lcm(ll a, ll b) {
  ll g = gcd(a, b);

  return a / g * b;
}

struct union_find {
  vector<int> par, r;
  union_find(int n) {
    par.resize(n);
    r.resize(n);
    init(n);
  }

  void init(int n) {
    for (int i = 0; i < n; i++)
      par[i] = i;
    for (int i = 0; i < n; i++)
      r[i] = 0;
  }

  int find(int x) {
    if (par[x] == x)
      return x;
    else
      return find(par[x]);
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);

    if (r[x] < r[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (r[x] == r[y]) {
        r[x]++;
      }
    }
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int a;
  string s;
  cin >> s;
  cin >> a;
  vector<string> kiyo;
  rep(i, 0, s.size()) {
    rep(j, i, s.size()) {

      string ses = s.substr(i, j);
      kiyo.push_back(ses);
    }
  }
  sort(kiyo.begin(), kiyo.end());
  kiyo.erase(unique(kiyo.begin(), kiyo.end()), kiyo.end());

  cout << kiyo[a - 1] << endl;
}


Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a, b) for (int i = a; i >= b; --i)
#define fore(i, a) for (auto &i : a)
#define INF INT_MAX / 2;
typedef long long ll;
using namespace std;
using vi = vector<int>;
using vll = vector<ll>;
ll mod = 1e9 + 7;
// ll llmax = 10000000000000000000ll;
using namespace std;
using Graph = vector<vector<int>>;

int cnt_digit(ll N) {
  int digit = 0;

  while (N > 0) {
    N /= 10;
    digit++;
  }
  return digit;
}

// 最大公約数計算

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}
// 最小公倍数の計算

ll lcm(ll a, ll b) {
  ll g = gcd(a, b);

  return a / g * b;
}

struct union_find {
  vector<int> par, r;
  union_find(int n) {
    par.resize(n);
    r.resize(n);
    init(n);
  }

  void init(int n) {
    for (int i = 0; i < n; i++)
      par[i] = i;
    for (int i = 0; i < n; i++)
      r[i] = 0;
  }

  int find(int x) {
    if (par[x] == x)
      return x;
    else
      return find(par[x]);
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);

    if (r[x] < r[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (r[x] == r[y]) {
        r[x]++;
      }
    }
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int a;
  string s;
  cin >> s;
  cin >> a;
  vector<string> kiyo;
  rep(i, 0, s.size()) {
    rep(j, 1, a + 1) {

      string ses = s.substr(i, j);
      kiyo.push_back(ses);
    }
  }
  sort(kiyo.begin(), kiyo.end());
  kiyo.erase(unique(kiyo.begin(), kiyo.end()), kiyo.end());

  cout << kiyo[a - 1] << endl;
}