Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("avx")
#include <bits/stdc++.h>
#define re return
#define pb push_back
#define po pop_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define ss second.second
#define sf second.first
#define ff first.first
#define fs first.second
#define si size
#define sqrt(x) sqrt(abs(x))
#define mp make_pair
#define pi 3.14159265359
#define er erase
#define in insert
#define em empty
#define ft front
#define fo(i, n) for (i = 0; i < n; i++)
#define ro(i, n) for (i = n - 1; i >= 0; i--)
#define fr(i, j, n) for (i = j; i < n; i++)
#define rf(i, j, n) for (i = n - 1; i >= j; i--)
typedef std::vector<int> vi;
typedef std::vector<vi> vvi;
typedef std::pair<int, int> ii;
typedef std::vector<ii> vii;
typedef std::vector<std::string> vs;
typedef double dl;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<ll, ll> pll;
typedef std::vector<ll> vll;
#define filename ""
using namespace std;
string a[5];
int main() {
  /*
      freopen(filename".in","r",stdin);
      freopen(filename".out","w",stdout);
      */
  /*
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
*/
  /*
  ios::sync_with_stdio(0);
  */
  string s;
  cin >> s;
  int i, j;
  int k;
  fo(i, 5) { a[i] = "{"; }
  cin >> k;
  string t;
  int q, w;
  fo(i, s.length()) {
    fr(j, 1, s.length() + 1) {
      t = s.substr(i, j);
      fo(q, 5) {
        if (t == a[q])
          break;
        if (t < a[q]) {
          rf(w, q + 1, 5) { a[w] = a[w - 1]; }
          a[q] = t;
          break;
        }
      }
    }
  }
  /*fo(i,5)
  {
      cout<<a[i]<<' ';
  }*/
  cout << a[k - 1];
}


Fixed submission:
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("avx")
#include <bits/stdc++.h>
#define re return
#define pb push_back
#define po pop_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define ss second.second
#define sf second.first
#define ff first.first
#define fs first.second
#define si size
#define sqrt(x) sqrt(abs(x))
#define mp make_pair
#define pi 3.14159265359
#define er erase
#define in insert
#define em empty
#define ft front
#define fo(i, n) for (i = 0; i < n; i++)
#define ro(i, n) for (i = n - 1; i >= 0; i--)
#define fr(i, j, n) for (i = j; i < n; i++)
#define rf(i, j, n) for (i = n - 1; i >= j; i--)
typedef std::vector<int> vi;
typedef std::vector<vi> vvi;
typedef std::pair<int, int> ii;
typedef std::vector<ii> vii;
typedef std::vector<std::string> vs;
typedef double dl;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<ll, ll> pll;
typedef std::vector<ll> vll;
#define filename ""
using namespace std;
string a[5];
int main() {
  /*
      freopen(filename".in","r",stdin);
      freopen(filename".out","w",stdout);
      */
  /*
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
*/
  /*
  ios::sync_with_stdio(0);
  */
  string s;
  cin >> s;
  int i, j;
  int k;
  fo(i, 5) { a[i] = "{"; }
  cin >> k;
  string t;
  int q, w;
  fo(i, s.length()) {
    fr(j, 1, 6) {
      t = s.substr(i, j);
      fo(q, 5) {
        if (t == a[q])
          break;
        if (t < a[q]) {
          rf(w, q + 1, 5) { a[w] = a[w - 1]; }
          a[q] = t;
          break;
        }
      }
    }
  }
  /*fo(i,5)
  {
      cout<<a[i]<<' ';
  }*/
  cout << a[k - 1];
}