Problem p03300 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;

const int MN = 3010;

int N;
string S;
int chk[MN], inva[MN], invb[MN];
vector<pii> seg;

bool cmp(string &a, string &b) {
  int n = a.size();
  for (int i = 0; i < b.size(); i++) {
    if (a[i % n] != b[i])
      return a[i % n] > b[i];
  }
  return true;
}

int main() {
  std::ios::sync_with_stdio(false);

  cin >> N >> S;

  int cnt1 = 0;
  int cnt2 = 0;
  for (int i = 0; i < 2 * N; i++) {
    if (S[i] == 'a')
      chk[i] = cnt1++, inva[chk[i]] = i;
    else
      chk[i] = cnt2++, invb[chk[i]] = i;
  }

  int sum = 0, st = 0;
  for (int i = 0; i < 2 * N; i++) {
    if (S[i] == 'a')
      sum++;
    else
      sum--;
    if (sum == 0) {
      seg.push_back({st, i});
      st = i + 1;
    }
  }
  seg.push_back({st, 2 * N - 1});

  string ans;
  for (int i = (int)seg.size() - 1; i >= 0; i--) {
    int l = seg[i].first;
    int r = seg[i].second;

    if (S[l] == 'a') {
      string tmp;
      for (int j = l; j <= r; j++) {
        if (S[j] == 'a') {
          tmp.push_back('a');
          tmp.push_back('b');
          j = invb[chk[j]];
        }
      }
      if (cmp(tmp, ans))
        ans = tmp + ans;
    } else {
      int ll = chk[l];
      int rr = chk[r];

      string mx;
      for (int j = rr; j >= ll; j--) {
        string tmp;
        for (int k = l; k <= r; k++) {
          if (chk[k] >= j)
            tmp.push_back(S[k]);
        }
        if (mx < tmp)
          mx = tmp;
      }
      if (cmp(mx, ans))
        ans = mx + ans;
    }
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;

const int MN = 3010;

int N;
string S;
int chk[MN << 1], inva[MN << 1], invb[MN << 1];
vector<pii> seg;

bool cmp(string &a, string &b) {
  int n = a.size();
  for (int i = 0; i < b.size(); i++) {
    if (a[i % n] != b[i])
      return a[i % n] > b[i];
  }
  return true;
}

int main() {
  std::ios::sync_with_stdio(false);

  cin >> N >> S;

  int cnt1 = 0;
  int cnt2 = 0;
  for (int i = 0; i < 2 * N; i++) {
    if (S[i] == 'a')
      chk[i] = cnt1++, inva[chk[i]] = i;
    else
      chk[i] = cnt2++, invb[chk[i]] = i;
  }

  int sum = 0, st = 0;
  for (int i = 0; i < 2 * N; i++) {
    if (S[i] == 'a')
      sum++;
    else
      sum--;
    if (sum == 0) {
      seg.push_back({st, i});
      st = i + 1;
    }
  }
  seg.push_back({st, 2 * N - 1});

  string ans;
  for (int i = (int)seg.size() - 1; i >= 0; i--) {
    int l = seg[i].first;
    int r = seg[i].second;

    if (S[l] == 'a') {
      string tmp;
      for (int j = l; j <= r; j++) {
        if (S[j] == 'a') {
          tmp.push_back('a');
          tmp.push_back('b');
          j = invb[chk[j]];
        }
      }
      if (cmp(tmp, ans))
        ans = tmp + ans;
    } else {
      int ll = chk[l];
      int rr = chk[r];

      string mx;
      for (int j = rr; j >= ll; j--) {
        string tmp;
        for (int k = l; k <= r; k++) {
          if (chk[k] >= j)
            tmp.push_back(S[k]);
        }
        if (mx < tmp)
          mx = tmp;
      }
      if (cmp(mx, ans))
        ans = mx + ans;
    }
  }
  cout << ans;
}