Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <string>
#include <vector>

#define X first
#define Y second
#define vsort(v) sort(v.begin(), v.end())
#define vrev(v) reverse(v.begin(), v.end())
#define vmax_p(v) max_element(v.begin(), v.end())
#define vmin_p(v) min_element(v.begin(), v.end())
#define vmax_idx(v) distance(v.begin(), vmax_p(v))
#define vmin_idx(v) distance(v.begin(), vmin_p(v))
#define vcopy(src, dst) copy(src.begin(), src.end(), back_inserter(dst))
#define P pair

// stoi(s): string to int
// stod(s): string to double

// INT_MAX
// INT_MIN
// LLONG_MAX
// LLONG_MIN
// DBL_MIN
// DBL_MAX
// LDBL_MIN
// LDBL_MAX

// A-Z: 65~90
// a-z: 97~122
// |a-z| = 26

// floor(x): truncate

// 2進数変換
// string s = bitset<20>(x).to_string();

using namespace std;
using ll = long long;

void displayVec(vector<int> v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << endl;
  }
  cout << endl;
}

int binarySearch(vector<int> v, int target) {
  int left = -1;
  int right = v.size();

  while (right - left > 1) {
    int mid = left + (right - left) / 2;

    if (v[mid] >= target) {
      right = mid;
    } else {
      left = mid;
    }
  }

  if (right < v.size() && v[right] == target) {
    return right;
  }
  return -1;
}

int main(int argc, const char *argv[]) {
  string s;
  int k;
  cin >> s >> k;
  set<string> st;
  for (int i = 1; i <= s.length(); i++) {
    for (int j = 0; j + i <= s.length(); j++) {
      st.insert(s.substr(j, i));
    }
  }
  auto it = st.begin();
  for (int i = 0; i < k - 1; i++) {
    it++;
  }
  cout << *it << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <string>
#include <vector>

#define X first
#define Y second
#define vsort(v) sort(v.begin(), v.end())
#define vrev(v) reverse(v.begin(), v.end())
#define vmax_p(v) max_element(v.begin(), v.end())
#define vmin_p(v) min_element(v.begin(), v.end())
#define vmax_idx(v) distance(v.begin(), vmax_p(v))
#define vmin_idx(v) distance(v.begin(), vmin_p(v))
#define vcopy(src, dst) copy(src.begin(), src.end(), back_inserter(dst))
#define P pair

// stoi(s): string to int
// stod(s): string to double

// INT_MAX
// INT_MIN
// LLONG_MAX
// LLONG_MIN
// DBL_MIN
// DBL_MAX
// LDBL_MIN
// LDBL_MAX

// A-Z: 65~90
// a-z: 97~122
// |a-z| = 26

// floor(x): truncate

// 2進数変換
// string s = bitset<20>(x).to_string();

using namespace std;
using ll = long long;

void displayVec(vector<int> v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << endl;
  }
  cout << endl;
}

int binarySearch(vector<int> v, int target) {
  int left = -1;
  int right = v.size();

  while (right - left > 1) {
    int mid = left + (right - left) / 2;

    if (v[mid] >= target) {
      right = mid;
    } else {
      left = mid;
    }
  }

  if (right < v.size() && v[right] == target) {
    return right;
  }
  return -1;
}

int main(int argc, const char *argv[]) {
  string s;
  int k;
  cin >> s >> k;
  set<string> st;
  for (int i = 1; i <= k; i++) {
    for (int j = 0; j + i <= s.length(); j++) {
      st.insert(s.substr(j, i));
    }
  }
  auto it = st.begin();
  for (int i = 0; i < k - 1; i++) {
    it++;
  }
  cout << *it << endl;
}