Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using ll = long long;
using vll = vector<ll>;
using pii = pair<int, int>;
using vb = vector<char>;
using vvb = vector<vb>;

#define REP(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define mp(a, b) make_pair(a, b)

#define fi first
#define sn second
#define decpii(p) p.fi--, p.sn--;

#ifdef LOCAL
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define dumpn(c) cerr << "> " << #c << " = " << (c) << ", ";
#else
#define dump(c) ;
#define dumpn(c) ;
#endif

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, (int)a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

int dxf[] = {1, 0, -1, 0};
int dyf[] = {0, -1, 0, 1};

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

inline vi uni(vi &vec) {
  sort(vec.begin(), vec.end());
  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
  return vec;
}

inline ll fpow(ll x, ll p) {
  ll ans = 1;
  while (p > 0) {
    if ((p & 1) == 1) {
      ans *= x;
    }
    x *= x;
    p >>= 1;
  }
  return ans;
}

struct ____ {
  ____() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
} ________;

string solve() {
  string s;
  cin >> s;
  int K;
  cin >> K;
  int n = s.length();

  set<string> res;
  rep(i, n) {
    stringstream ss;
    REP(j, i, n) {
      ss << s[j];
      res.insert(ss.str());
    }
  }

  auto it = begin(res);
  K--;
  rep(i, K) { ++it; }
  return *it;
}

int main() {
  cout << solve() << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using ll = long long;
using vll = vector<ll>;
using pii = pair<int, int>;
using vb = vector<char>;
using vvb = vector<vb>;

#define REP(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define mp(a, b) make_pair(a, b)

#define fi first
#define sn second
#define decpii(p) p.fi--, p.sn--;

#ifdef LOCAL
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define dumpn(c) cerr << "> " << #c << " = " << (c) << ", ";
#else
#define dump(c) ;
#define dumpn(c) ;
#endif

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, (int)a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

int dxf[] = {1, 0, -1, 0};
int dyf[] = {0, -1, 0, 1};

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

inline vi uni(vi &vec) {
  sort(vec.begin(), vec.end());
  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
  return vec;
}

inline ll fpow(ll x, ll p) {
  ll ans = 1;
  while (p > 0) {
    if ((p & 1) == 1) {
      ans *= x;
    }
    x *= x;
    p >>= 1;
  }
  return ans;
}

struct ____ {
  ____() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
  };
} ________;

string solve() {
  string s;
  cin >> s;
  int K;
  cin >> K;
  int n = s.length();

  set<string> res;
  rep(i, n) {
    stringstream ss;
    REP(j, i, min(n, i + K + 1)) {
      ss << s[j];
      res.insert(ss.str());
    }
  }

  auto it = begin(res);
  K--;
  rep(i, K) { ++it; }
  return *it;
}

int main() {
  cout << solve() << endl;
  return 0;
}