Problem p03353 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define Int int64_t

void printVec(std::vector<int> &vec) {
  std::cout << "";
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    std::cout << *it << " ";
  }
  std::cout << std::endl;
}
void printVec1(std::vector<string> &vec) {
  std::cout << "";
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    std::cout << *it << " ";
  }
  std::cout << std::endl;
}

template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

int vector_finder(std::vector<string> vec, string number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}

#include <sstream>
std::vector<std::string> StringSplit(const std::string &str, char sep) {
  std::vector<std::string> v;
  std::stringstream ss(str);
  std::string buffer;
  while (std::getline(ss, buffer, sep)) {
    v.push_back(buffer);
  }
  return v;
}

Int gcd(Int a, Int b) { return b != 0 ? gcd(b, a % b) : a; }
Int lcm(Int a, Int b) { return a * b / gcd(a, b); }

int main() {
  string str;
  cin >> str;
  int k;
  cin >> k;
  vector<string> sub;

  for (int i = str.size(); i > 0; i--) {
    for (int j = 0; j < str.size(); j++) {
      if (i <= str.size() - j) {
        sub.push_back(str.substr(j, i));
      }
    }
  }
  sort(sub.begin(), sub.end());
  sub.erase(unique(sub.begin(), sub.end()), sub.end());

  // printVec1(sub);
  // cout << sub[3] << " " << sub[4];
  cout << sub[k - 1];

  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define Int int64_t

void printVec(std::vector<int> &vec) {
  std::cout << "";
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    std::cout << *it << " ";
  }
  std::cout << std::endl;
}
void printVec1(std::vector<string> &vec) {
  std::cout << "";
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    std::cout << *it << " ";
  }
  std::cout << std::endl;
}

template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

int vector_finder(std::vector<string> vec, string number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance(vec.begin(), itr);
  if (index != vec.size()) { // 発見できたとき
    return 1;
  } else { // 発見できなかったとき
    return 0;
  }
}

#include <sstream>
std::vector<std::string> StringSplit(const std::string &str, char sep) {
  std::vector<std::string> v;
  std::stringstream ss(str);
  std::string buffer;
  while (std::getline(ss, buffer, sep)) {
    v.push_back(buffer);
  }
  return v;
}

Int gcd(Int a, Int b) { return b != 0 ? gcd(b, a % b) : a; }
Int lcm(Int a, Int b) { return a * b / gcd(a, b); }

int main() {
  string str;
  cin >> str;
  int k;
  cin >> k;
  vector<string> sub;

  for (int i = 1; i < 20; i++) {
    for (int j = 0; j < str.size(); j++) {
      if (i <= str.size() - j) {
        sub.push_back(str.substr(j, i));
      }
    }
  }
  sort(sub.begin(), sub.end());
  sub.erase(unique(sub.begin(), sub.end()), sub.end());

  // printVec1(sub);
  // cout << sub[3] << " " << sub[4];
  cout << sub[k - 1];

  cout << endl;
  return 0;
}