Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, a, n) for (int i = int(a); i < int(n); ++i)
#define repe(i, a, n) for (int i = int(a); i <= int(n); ++i)
#define repr(i, a, n) for (int i = int(a); i > int(n); --i)
#define reper(i, a, n) for (int i = int(a); i >= int(n); --i)
#define SIZE(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl;
#define no cout << "no" << endl;
#define pb push_back
#define EPS (1e-20)
#define INF 2147483647
using llint = long long;
using VI = std::vector<int>;
using PII = std::pair<int, int>;
using SI = std::set<int>;
using namespace std;

string s;
int K;
set<string> sub;

void enumrate() {
  string subs;
  for (int i = 1; i <= SIZE(s); i++) {
    for (int j = 0; j <= SIZE(s) - i; j++) {
      for (int k = 0; k < i; k++) {
        subs.pb(s[j + k]);
      }
      sub.insert(subs);
      subs = "";
    }
  }
}

string solve() {
  enumrate();
  int i = 0;
  for (auto it = sub.begin();; it++, i++)
    if (i == K - 1)
      return *it;
  return "aaa";
}

int main() {
  cin >> s;
  cin >> K;

  cout << solve() << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, a, n) for (int i = int(a); i < int(n); ++i)
#define repe(i, a, n) for (int i = int(a); i <= int(n); ++i)
#define repr(i, a, n) for (int i = int(a); i > int(n); --i)
#define reper(i, a, n) for (int i = int(a); i >= int(n); --i)
#define SIZE(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl;
#define no cout << "no" << endl;
#define pb push_back
#define EPS (1e-20)
#define INF 2147483647
using llint = long long;
using VI = std::vector<int>;
using PII = std::pair<int, int>;
using SI = std::set<int>;
using namespace std;

string s;
int K;
set<string> sub;

void enumrate() {
  string subs;
  for (int i = 1; i <= SIZE(s); i++) {
    for (int j = 0; j <= SIZE(s) - i; j++) {
      for (int k = 0; k < i; k++) {
        subs.pb(s[j + k]);
      }
      sub.insert(subs);
      subs = "";
    }
  }
}

string solve() {
  if (SIZE(s) <= 5) {
    enumrate();
    int i = 0;
    for (auto it = sub.begin();; it++, i++)
      if (i == K - 1)
        return *it;
    return "aaa";
  } else {
    set<char> setc;
    rep(i, 0, SIZE(s)) setc.insert(s[i]);
    for (auto c : setc) {
      rep(i, 0, SIZE(s)) {
        if (s[i] == c) {
          string subs;
          rep(j, 0, 5) {
            if (i + j >= SIZE(s))
              break;
            subs.pb(s[i + j]);
            sub.insert(subs);
          }
        }
      }
    }
    int i = 0;
    for (auto it = sub.begin();; it++, i++)
      if (i == K - 1)
        return *it;
    return "aaa";
  }
}

int main() {
  cin >> s;
  cin >> K;

  cout << solve() << endl;
  return 0;
}