Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
// {{{
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define FOR(i, a, b)                                                           \
  for (ll i = static_cast<ll>(a); i < static_cast<ll>(b); i++)
#define FORR(i, a, b)                                                          \
  for (ll i = static_cast<ll>(a); i >= static_cast<ll>(b); i--)
#define REP(i, n) for (ll i = 0ll; i < static_cast<ll>(n); i++)
#define REPR(i, n) for (ll i = static_cast<ll>(n); i >= 0ll; i--)
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> IP;
typedef pair<ll, LP> LLP;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};

constexpr int INF = 100000000;
constexpr ll LINF = 10000000000000000ll;
constexpr int MOD = static_cast<int>(1e9 + 7);
constexpr double EPS = 1e-9;
// }}}

string s;
int K;

void init() {}

void solve() {
  set<string> ss;
  int l = s.size();
  REP(i, l) {
    FOR(j, 1, l - i + 1) {
      // cout << s.substr(i, j) << endl;
      ss.insert(s.substr(i, j));
    }
  }
  auto it = ss.begin();
  advance(it, K - 1);
  cout << *it << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> s >> K;
  solve();
  return 0;
}

// vim:set foldmethod=marker:


Fixed submission:
// {{{
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define FOR(i, a, b)                                                           \
  for (ll i = static_cast<ll>(a); i < static_cast<ll>(b); i++)
#define FORR(i, a, b)                                                          \
  for (ll i = static_cast<ll>(a); i >= static_cast<ll>(b); i--)
#define REP(i, n) for (ll i = 0ll; i < static_cast<ll>(n); i++)
#define REPR(i, n) for (ll i = static_cast<ll>(n); i >= 0ll; i--)
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> IP;
typedef pair<ll, LP> LLP;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};

constexpr int INF = 100000000;
constexpr ll LINF = 10000000000000000ll;
constexpr int MOD = static_cast<int>(1e9 + 7);
constexpr double EPS = 1e-9;
// }}}

string s;
int K;

void init() {}

void solve() {
  set<string> ss;
  int l = s.size();
  REP(i, l) {
    FOR(j, 1, l - i + 1) {
      string sub = s.substr(i, j);
      if (ss.size() > K) {
        auto it = ss.begin();
        advance(it, K - 1);
        if (sub >= *it)
          break;
      }
      ss.insert(s.substr(i, j));
    }
  }
  auto it = ss.begin();
  advance(it, K - 1);
  cout << *it << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> s >> K;
  solve();
  return 0;
}

// vim:set foldmethod=marker: