Problem p03353 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
    In the name of
        "ALLAH"
    And say "AL-HAMD-U-LILLAH"
*/

#include <bits/stdc++.h>
#define ll long long
#define llu unsigned long long
#define _ printf("\n")
int take() {
  int n;
  scanf("%d", &n);
  return n;
}
double ttake() {
  double n;
  scanf("%lf", &n);
  return n;
}
ll takes() {
  ll n;
  scanf("%lld", &n);
  return n;
}

#define Int take()
#define Dnt ttake()
#define LL takes()
#define done                                                                   \
  { return 0; }
#define fr(i, x, n, a) for (int i = x; i <= n; i += a)
#define FR(a, b, x) for (int i = (a); i <= (b); i += (x))
#define rfr(i, x, n, a) for (int i = x; i >= n; i--)
int cas;
#define print printf("Case %d:", ++cas)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define pii pair<int, int>
#define vii vector<pii>
#define pll pair<ll, ll>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define mk make_pair
#define fill(x, v) memset(x, v, sizeof x)

using namespace std;

bool approximatelyEqual(float a, float b, float epsilon) {
  return fabs(a - b) <= ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
}

bool essentiallyEqual(float a, float b, float epsilon) {
  return fabs(a - b) <= ((fabs(a) > fabs(b) ? fabs(b) : fabs(a)) * epsilon);
}

bool definitelyGreaterThan(float a, float b, float epsilon) {
  return (a - b) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
}

bool definitelyLessThan(float a, float b, float epsilon) {
  return (b - a) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
}
#define STR                                                                    \
  { struct info }
#define dbg(x)                                                                 \
  { cout << "ekhane" << x << endl; }
#define SS second
#define FF first
#define PQ priority_queue
#define great greatest
#define less lesser
#define mii map<int, int>
#define mil map<int, ll>
#define mll map<ll, ll>
#define msb map<string, bool>
#define V vector
#define M 100005
#define pf printf
//////////////////////////////////////////

int main() {

  string s;
  cin >> s;
  int k = Int;
  set<string> s1;

  fr(i, 0, s.size(), 1) {
    string up = "";
    fr(k, i, s.size() - 1, 1) {
      up += s[k];
      s1.insert(up);
    }
  }
  auto it = s1.begin();
  while (it != s1.end()) {
    k--;
    if (k == 0) {
      cout << *it;
      done;
    }
    it++;
  }
  done;
}


Fixed submission:
/*
    In the name of
        "ALLAH"
    And say "AL-HAMD-U-LILLAH"
*/

#include <bits/stdc++.h>
#define ll long long
#define llu unsigned long long
#define _ printf("\n")
int take() {
  int n;
  scanf("%d", &n);
  return n;
}
double ttake() {
  double n;
  scanf("%lf", &n);
  return n;
}
ll takes() {
  ll n;
  scanf("%lld", &n);
  return n;
}

#define Int take()
#define Dnt ttake()
#define LL takes()
#define done                                                                   \
  { return 0; }
#define fr(i, x, n, a) for (int i = x; i <= n; i += a)
#define FR(a, b, x) for (int i = (a); i <= (b); i += (x))
#define rfr(i, x, n, a) for (int i = x; i >= n; i--)
int cas;
#define print printf("Case %d:", ++cas)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define pii pair<int, int>
#define vii vector<pii>
#define pll pair<ll, ll>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define mk make_pair
#define fill(x, v) memset(x, v, sizeof x)

using namespace std;

bool approximatelyEqual(float a, float b, float epsilon) {
  return fabs(a - b) <= ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
}

bool essentiallyEqual(float a, float b, float epsilon) {
  return fabs(a - b) <= ((fabs(a) > fabs(b) ? fabs(b) : fabs(a)) * epsilon);
}

bool definitelyGreaterThan(float a, float b, float epsilon) {
  return (a - b) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
}

bool definitelyLessThan(float a, float b, float epsilon) {
  return (b - a) > ((fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
}
#define STR                                                                    \
  { struct info }
#define dbg(x)                                                                 \
  { cout << "ekhane" << x << endl; }
#define SS second
#define FF first
#define PQ priority_queue
#define great greatest
#define less lesser
#define mii map<int, int>
#define mil map<int, ll>
#define mll map<ll, ll>
#define msb map<string, bool>
#define V vector
#define M 100005
#define pf printf
//////////////////////////////////////////

int main() {

  string s;
  cin >> s;
  int k = Int;
  set<string> s1;

  fr(i, 0, s.size(), 1) {
    string up = "";
    fr(k, i, s.size() - 1, 1) {
      up += s[k];
      s1.insert(up);
      if (s1.size() > 5) {
        auto it = s1.end();
        it--;
        s1.erase(it);
      }
    }
  }
  auto it = s1.begin();
  while (it != s1.end()) {
    k--;
    if (k == 0) {
      cout << *it;
      done;
    }
    it++;
  }
  done;
}