Problem p03353 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define FORS(i, a, b) for (int i = a + 1; i <= b; i++)
#define REP(i, N) FOR(i, 0, N)
#define REPS(i, N) FORS(i, 0, N)
#define debug(x) cout << #x << "=" << x << endl
#define debugarr(arr, N, M)                                                    \
  {                                                                            \
    REP(i, N) {                                                                \
      REP(j, M) {                                                              \
        if (arr[i][j] == INF)                                                  \
          printf(" INF");                                                      \
        else                                                                   \
          printf(" %3d", arr[i][j]);                                           \
      }                                                                        \
      cout << endl;                                                            \
    }                                                                          \
  }
#define pb push_back
#define pob pop_back
#define cint(a)                                                                \
  int a;                                                                       \
  cin >> a
#define cint2(a, b)                                                            \
  int a, b;                                                                    \
  cin >> a >> b
#define cint3(a, b, c)                                                         \
  int a, b, c;                                                                 \
  cin >> a >> b >> c
typedef long long ll;
const int INF = 100000000;
//--------------------------------------//
int main() {
  string s;
  cin >> s;
  cint(k);

  vector<pair<char, int>> pairs;

  REP(i, s.size()) { pairs.pb(make_pair(s[i], i)); }
  sort(pairs.begin(), pairs.end());
  queue<int> q;
  int r = 0;
  while (true) {
    q.push(pairs[r].second);
    r++;
    if (r > 5 && pairs[r - 1].first != pairs[r].first || r == s.size() - 1)
      break;
  }
  vector<string> vs;
  while (!q.empty()) {
    REPS(i, 5) {
      if (q.front() + i > s.size())
        break;
      vs.pb(s.substr(q.front(), i));
    }
    q.pop();
  }
  sort(vs.begin(), vs.end());
  vs.erase(std::unique(vs.begin(), vs.end()), vs.end());
  /*REP(i,vs.size()){
          cout<<vs[i]<<endl;
  }*/
  cout << vs[k - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define FORS(i, a, b) for (int i = a + 1; i <= b; i++)
#define REP(i, N) FOR(i, 0, N)
#define REPS(i, N) FORS(i, 0, N)
#define debug(x) cout << #x << "=" << x << endl
#define debugarr(arr, N, M)                                                    \
  {                                                                            \
    REP(i, N) {                                                                \
      REP(j, M) {                                                              \
        if (arr[i][j] == INF)                                                  \
          printf(" INF");                                                      \
        else                                                                   \
          printf(" %3d", arr[i][j]);                                           \
      }                                                                        \
      cout << endl;                                                            \
    }                                                                          \
  }
#define pb push_back
#define pob pop_back
#define cint(a)                                                                \
  int a;                                                                       \
  cin >> a
#define cint2(a, b)                                                            \
  int a, b;                                                                    \
  cin >> a >> b
#define cint3(a, b, c)                                                         \
  int a, b, c;                                                                 \
  cin >> a >> b >> c
typedef long long ll;
const int INF = 100000000;
//--------------------------------------//
int main() {
  string s;
  cin >> s;
  cint(k);

  vector<pair<char, int>> pairs;

  REP(i, s.size()) { pairs.pb(make_pair(s[i], i)); }
  sort(pairs.begin(), pairs.end());
  queue<int> q;
  int r = 0;
  while (true) {
    q.push(pairs[r].second);
    r++;
    if (r > 5 && pairs[r - 1].first != pairs[r].first || r == s.size())
      break;
  }
  vector<string> vs;
  while (!q.empty()) {
    REPS(i, 5) {
      if (q.front() + i > s.size())
        break;
      vs.pb(s.substr(q.front(), i));
    }
    q.pop();
  }
  sort(vs.begin(), vs.end());
  vs.erase(std::unique(vs.begin(), vs.end()), vs.end());
  /*REP(i,vs.size()){
          cout<<vs[i]<<endl;
  }*/
  cout << vs[k - 1] << endl;
  return 0;
}