Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>
// #include "cout.h"

using namespace std;

#define SZ(x) ((int)x.size())
#define MSET(x, a) memset(x, a, (int)sizeof(x))
#define PB push_back
#define VI vector<int>
#define PII pair<int, int>
#define LL long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define FIT(it, v)                                                             \
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define OUT(A) cout << #A << " = " << (A) << endl
#define OUT2(A, B)                                                             \
  cout << "(" << #A << ", " << #B << ") = (" << (A) << ", " << (B) << ")"      \
       << endl
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}

#define present(c, e) ((c).find((e)) != (c).end())
#define cpresent(c, e) (find(ALL(c), (e)) != (c).end())

string s;
int k;

void init() {}

void input() { cin >> s >> k; }

void solve() {
  vector<string> vs;
  REP(i, SZ(s)) FOR(j, 1, SZ(s) - i + 1) {
    string _s = s.substr(i, j);
    if (!cpresent(vs, _s)) {
      vs.PB(_s);
      sort(ALL(vs));
      if (SZ(vs) > k)
        vs.pop_back();
    }
  }
  cout << vs[k - 1] << endl;
}

int main() {
  init();
  input();
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>
// #include "cout.h"

using namespace std;

#define SZ(x) ((int)x.size())
#define MSET(x, a) memset(x, a, (int)sizeof(x))
#define PB push_back
#define VI vector<int>
#define PII pair<int, int>
#define LL long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define FIT(it, v)                                                             \
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define OUT(A) cout << #A << " = " << (A) << endl
#define OUT2(A, B)                                                             \
  cout << "(" << #A << ", " << #B << ") = (" << (A) << ", " << (B) << ")"      \
       << endl
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}

#define present(c, e) ((c).find((e)) != (c).end())
#define cpresent(c, e) (find(ALL(c), (e)) != (c).end())

string s;
int k;

void init() {}

void input() { cin >> s >> k; }

void solve() {
  vector<string> vs;
  REP(i, SZ(s)) FOR(j, 1, min(k + 1, SZ(s) - i + 1)) {
    string _s = s.substr(i, j);
    if (!cpresent(vs, _s)) {
      vs.PB(_s);
      sort(ALL(vs));
      if (SZ(vs) > k)
        vs.pop_back();
    }
  }
  cout << vs[k - 1] << endl;
}

int main() {
  init();
  input();
  solve();
  return 0;
}