Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef long long ll;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INT_INF = 2147483647;
const long long LL_INF = 1LL << 60;
const long long MOD = 1000000007;

#define CLR(a) memset((a), 0, sizeof(a))

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

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

int main(void) {
  string s;
  cin >> s;
  int K;
  cin >> K;
  set<string> ans;
  int used[26];
  REP(i, 26) used[i] = 0;
  REP(i, s.size()) used[s[i] - 'a'] = 1;

  REP(i, 26) {
    if (used[i]) {
      REP(j, s.size()) {
        if (s[j] == i + 'a') {
          string sub = s.substr(j);
          REP(k, sub.size()) {
            ans.insert(sub.substr(0, k + 1));
            if (ans.size() > K)
              ans.erase(*ans.rbegin());
          }
        }
      }
    }
    if (ans.size() == K)
      break;
  }
  cout << *ans.rbegin() << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef long long ll;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INT_INF = 2147483647;
const long long LL_INF = 1LL << 60;
const long long MOD = 1000000007;

#define CLR(a) memset((a), 0, sizeof(a))

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

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

int main(void) {
  string s;
  cin >> s;
  int K;
  cin >> K;
  set<string> ans;
  int used[26];
  REP(i, 26) used[i] = 0;
  REP(i, s.size()) used[s[i] - 'a'] = 1;

  REP(i, 26) {
    if (used[i]) {
      REP(j, s.size()) {
        if (s[j] == i + 'a') {
          string sub = s.substr(j);
          REP(k, min((int)sub.size(), K)) {
            ans.insert(sub.substr(0, k + 1));
            if (ans.size() > K)
              ans.erase(*ans.rbegin());
          }
        }
      }
    }
    if (ans.size() == K)
      break;
  }
  cout << *ans.rbegin() << endl;
  return 0;
}