Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define pb push_back
#define mp make_pair
#define pie 3.141592653589793238
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
const int INF = 1 << 29;
typedef long long ll;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n >> b) & 1; }
inline void set_bit(int &n, int b) { n |= two(b); }
inline void unset_bit(int &n, int b) { n &= ~two(b); }
/////////////////////////////////////////////////////////////////////
const ll mod = 1e9 + 7;
const int N = 1e6 + 9;
ll a[N];
vector<ll> v[105];
vector<ll> num;
ll modexp(ll a, ll n) {
  ll r = 1;
  while (n) {
    if (n & 1)
      r = (r * a) % mod;
    a = (a * a) % mod;
    n >>= 1;
  }
  return r;
}
bool cmp(const pair<double, ll> &a, const pair<double, int> &b) {
  if (a.first == b.first) {
    return a.second < b.second;
  } else
    return a.first > b.first;
}
int main() {
  fast;
  string s;
  ll n;
  cin >> s;
  cin >> n;
  set<string> se;
  for (int i = 0; i < s.size(); i++) {
    for (int j = i; j < s.size(); j++) {
      string ss;
      for (int k = i; k <= j; k++) {
        ss += s[k];
      }
      se.insert(ss);
    }
  }
  ll count = 0;
  for (auto it = se.begin(); it != se.end(); it++) {
    count++;
    if (count == n) {
      cout << *it;
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define pb push_back
#define mp make_pair
#define pie 3.141592653589793238
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
const int INF = 1 << 29;
typedef long long ll;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n >> b) & 1; }
inline void set_bit(int &n, int b) { n |= two(b); }
inline void unset_bit(int &n, int b) { n &= ~two(b); }
/////////////////////////////////////////////////////////////////////
const ll mod = 1e9 + 7;
const int N = 1e6 + 9;
ll a[N];
vector<ll> v[105];
vector<ll> num;
ll modexp(ll a, ll n) {
  ll r = 1;
  while (n) {
    if (n & 1)
      r = (r * a) % mod;
    a = (a * a) % mod;
    n >>= 1;
  }
  return r;
}
bool cmp(const pair<double, ll> &a, const pair<double, int> &b) {
  if (a.first == b.first) {
    return a.second < b.second;
  } else
    return a.first > b.first;
}
int main() {
  fast;
  string s;
  ll n;
  cin >> s;
  cin >> n;
  set<string> se;
  for (int i = 0; i < s.size(); i++) {
    string ss;
    ll kk = i + n;
    if (kk > s.size()) {
      kk = s.size();
    }
    for (int k = i; k < kk; k++) {
      ss += s[k];
      se.insert(ss);
    }
  }
  ll count = 0;
  for (auto it = se.begin(); it != se.end(); it++) {
    count++;
    if (count == n) {
      cout << *it;
    }
  }
  return 0;
}