Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
// #define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3fLL
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1};
const int SIZE = 100050;
// ここまでテンプレ
int main() {
  set<string> V;
  string S;
  cin >> S;
  int K;
  cin >> K;
  int alpha[26] = {};
  for (int i = 0; i < S.length(); i++) {
    alpha[S[i] - 'a']++;
  }
  /*
  for(int i=0;i<26;i++){
          if(alpha[i]>=K){
                  cout<<(char)('a'+i)<<endl;
                  return 0;
          }
  }
  */
  for (int i = 0; i < 26; i++) {
    for (int j = 0; j < S.length(); j++) {
      if (S[j] == 'a' + i) {
        for (int k = 1; k + j <= S.length() || k <= 5; k++)
          V.insert(S.substr(j, k));
      }
    }
    if (V.size() >= K)
      break;
  }
  /*
  for(string T:V)
          cout<<T<<endl;
          */
  int c = 1;
  for (string T : V) {
    if (c == K) {
      cout << T << endl;
      return 0;
    }
    c++;
  }
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
// #define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3fLL
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1};
const int SIZE = 100050;
// ここまでテンプレ
int main() {
  set<string> V;
  string S;
  cin >> S;
  int K;
  cin >> K;
  int alpha[26] = {};
  for (int i = 0; i < S.length(); i++) {
    alpha[S[i] - 'a']++;
  }
  /*
  for(int i=0;i<26;i++){
          if(alpha[i]>=K){
                  cout<<(char)('a'+i)<<endl;
                  return 0;
          }
  }
  */
  for (int i = 0; i < 26; i++) {
    for (int j = 0; j < S.length(); j++) {
      if (S[j] == 'a' + i) {
        for (int k = 1; k + j <= S.length() && k <= 5; k++)
          V.insert(S.substr(j, k));
      }
    }
    if (V.size() >= K)
      break;
  }
  /*
  for(string T:V)
          cout<<T<<endl;
          */
  int c = 1;
  for (string T : V) {
    if (c == K) {
      cout << T << endl;
      return 0;
    }
    c++;
  }
  return 0;
}