Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<vector<int>> mati;
typedef vector<vector<double>> matd;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
const int INF = (1 << 30);
const ll INFLL = 1LL << 62;
const long double EPS = 1e-12;

template <typename T> void vec_print(vector<T> VEC) {
  rep(i, 0, VEC.size()) { cout << VEC[i] << " "; }
  cout << endl;
};

template <typename T>
void mat_print(vector<vector<T>> MAT){
    rep(i, 0, MAT.size()){rep(j, 0, MAT[i].size()){cout << MAT[i][j] << " ";
}
cout << endl;
}
}
;

class HOGE {
public:
  ll index;
  ll value;
  HOGE(ll index, ll value) {
    this->index = index;
    this->value = value;
  };
  ~HOGE(void) { return; };

  void print(void) {
    cout << "index : " << index << ", value : " << value << endl;
    return;
  };

  bool operator==(const HOGE &obj) { return (this->value == obj.value); };
  bool operator<(const HOGE &obj) { return (this->value < obj.value); };
  bool operator>(const HOGE &obj) { return (this->value > obj.value); };
};

bool compare(string str1, string str2) {
  ll compare_value = strcmp(str1.c_str(), str2.c_str());
  // strcmp は小文字比較だけ
  return (compare_value < 0);
};

bool compare(string str1, string str2, string str3) {
  return compare(str1, str2) && compare(str2, str3);
};

int main() {
  string s;
  int K;

  cin >> s;
  cin >> K;

  vector<string> s_set(K);
  rep(i, 0, K) { s_set[i] = "zzzzz"; }

  ll s_size = s.size();

  int count = 0;

  rep(s_initial, 0, s_size) {
    rep(s_length, 1, s_size + 1 - s_initial) {
      string s_sub = s.substr(s_initial, s_length);

      if (compare(s_sub, s_set[0])) {
        s_set.insert(s_set.begin(), s_sub);
        count++;
      } else {
        ll min_count = min(K - 1, count);
        rep(i, 0, min_count) {
          if (compare(s_set[i], s_sub, s_set[i + 1])) {
            s_set.insert(s_set.begin() + i + 1, s_sub);
            count++;
            break;
          }
        }
      }
    }
  }

  cout << s_set[K - 1] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<vector<int>> mati;
typedef vector<vector<double>> matd;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
const int INF = (1 << 30);
const ll INFLL = 1LL << 62;
const long double EPS = 1e-12;

template <typename T> void vec_print(vector<T> VEC) {
  rep(i, 0, VEC.size()) { cout << VEC[i] << " "; }
  cout << endl;
};

template <typename T>
void mat_print(vector<vector<T>> MAT){
    rep(i, 0, MAT.size()){rep(j, 0, MAT[i].size()){cout << MAT[i][j] << " ";
}
cout << endl;
}
}
;

class HOGE {
public:
  ll index;
  ll value;
  HOGE(ll index, ll value) {
    this->index = index;
    this->value = value;
  };
  ~HOGE(void) { return; };

  void print(void) {
    cout << "index : " << index << ", value : " << value << endl;
    return;
  };

  bool operator==(const HOGE &obj) { return (this->value == obj.value); };
  bool operator<(const HOGE &obj) { return (this->value < obj.value); };
  bool operator>(const HOGE &obj) { return (this->value > obj.value); };
};

bool compare(string str1, string str2) {
  ll compare_value = strcmp(str1.c_str(), str2.c_str());
  // strcmp は小文字比較だけ
  return (compare_value < 0);
};

bool compare(string str1, string str2, string str3) {
  return compare(str1, str2) && compare(str2, str3);
};

int main() {
  string s;
  int K;

  cin >> s;
  cin >> K;

  vector<string> s_set(K);
  rep(i, 0, K) { s_set[i] = "zzzzz"; }

  ll s_size = s.size();

  int count = 0;

  rep(s_initial, 0, s_size) {
    ll s_length_max = min((int)(s_size + 1 - s_initial), K + 1);
    rep(s_length, 1, s_length_max) {
      string s_sub = s.substr(s_initial, s_length);

      if (compare(s_sub, s_set[0])) {
        s_set.insert(s_set.begin(), s_sub);
        count++;
      } else {
        ll min_count = min(K - 1, count);
        rep(i, 0, min_count) {
          if (compare(s_set[i], s_sub, s_set[i + 1])) {
            s_set.insert(s_set.begin() + i + 1, s_sub);
            count++;
            break;
          }
        }
      }
    }
  }

  cout << s_set[K - 1] << endl;

  return 0;
}