Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#pragma region
using namespace std;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define LOWER(a, x) lower_bound(ALL(a), x) - a.begin() - 1;
#define COUNT(a, x) upper_bound(ALL(a), x) - lower_bound(ALL(a), x);
#define MCOUNT(a, x, y) upper_bound(ALL(a), y) - lower_bound(ALL(a), x);
#define INF 111111111111111111
#define MOD 1000000007
#define pb push_back
#define F first
#define S second
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef map<ll, ll> MAP;
ll n = 0, m = 0, ans = 0, sum = 0, cnt = 0, tmp = 0, ma = 0, mi = 0;
string s;
bool flag, ok;
ll dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
ll ddx[8] = {1, -1, 0, 0, 1, 1, -1, -1}, ddy[8] = {0, 0, 1, -1, 1, -1, 1, -1};
#pragma endregion
#define MAX 111111

int main(void) {
  map<string, ll> mp;
  vector<string> vs;
  string ss;
  cin >> s;
  cin >> n;

  char c = 'a';

  while (1) {
    flag = false;
    FOR(i, 0, s.length()) {
      if (c == s[i])
        flag = true;
    }

    if (!flag) {
      c++;
      continue;
    }

    FOR(i, 1, s.length() + 1) {
      ok = false;
      FOR(j, 0, s.length() - i + 1) {
        ss = "";
        if (s[j] == c) {
          FOR(k, j, j + i) { ss += s[k]; }
          if (mp[ss] == 0) {
            vs.pb(ss);
            mp[ss]++;
            cnt++;
            ok = true;
          }
        }
      }
      if (!ok) {
        break;
      }
    }
    if (cnt >= n)
      break;
    c++;
  }

  sort(ALL(vs));
  cout << vs[n - 1] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#pragma region
using namespace std;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define LOWER(a, x) lower_bound(ALL(a), x) - a.begin() - 1;
#define COUNT(a, x) upper_bound(ALL(a), x) - lower_bound(ALL(a), x);
#define MCOUNT(a, x, y) upper_bound(ALL(a), y) - lower_bound(ALL(a), x);
#define INF 111111111111111111
#define MOD 1000000007
#define pb push_back
#define F first
#define S second
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef map<ll, ll> MAP;
ll n = 0, m = 0, ans = 0, sum = 0, cnt = 0, tmp = 0, ma = 0, mi = 0;
string s;
bool flag, ok;
ll dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
ll ddx[8] = {1, -1, 0, 0, 1, 1, -1, -1}, ddy[8] = {0, 0, 1, -1, 1, -1, 1, -1};
#pragma endregion
#define MAX 111111

int main(void) {
  map<string, ll> mp;
  vector<string> vs;
  string ss;
  cin >> s;
  cin >> n;

  char c = 'a';

  while (1) {
    flag = false;
    FOR(i, 0, s.length()) {
      if (c == s[i])
        flag = true;
    }

    if (!flag) {
      c++;
      continue;
    }

    FOR(i, 1, 10) {
      ok = false;
      FOR(j, 0, s.length() - i + 1) {
        ss = "";
        if (s[j] == c) {
          FOR(k, j, j + i) { ss += s[k]; }
          if (mp[ss] == 0) {
            vs.pb(ss);
            mp[ss]++;
            cnt++;
            ok = true;
          }
        }
      }
      if (!ok) {
        break;
      }
    }
    if (cnt >= n)
      break;
    c++;
  }

  sort(ALL(vs));
  cout << vs[n - 1] << endl;

  return 0;
}