Problem p03300 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a, b) for (register int i = (a); i <= (b); ++i)
#define per(i, a, b) for (register int i = (a); i >= (b); --i)
#define loop(it, v) for (auto it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (register int i = head[x]; i; i = edge[i].nex)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, cnt) memset(a, cnt, sizeof(a))
#define cop(a, b) memcpy(a, b, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define SC(t, x) static_cast<t>(x)
#define ub upper_bound
#define lb lower_bound
#define pqueue priority_queue
#define mp make_pair
#define pb push_back
#define pof pop_front
#define pob pop_back
#define fi first
#define se second
#define y1 y1_
#define Pi acos(-1.0)
#define iv inline void
#define enter putchar('\n')
#define siz(x) ((int)x.size())
#define file(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout)
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef queue<int> qi;
typedef queue<pii> qii;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<string, int> msi;
const int maxn = 4e3 + 100;
const int inf = 0x3f3f3f3f;
const int iinf = 1 << 30;
const ll linf = 2e18;
const ll mod = 998244353;
const double eps = 1e-7;
template <class T = int> T read() {
  T f = 1, a = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    a = (a << 3) + (a << 1) + ch - '0';
    ch = getchar();
  }
  return a * f;
}

int n, last = 1, lpos = 1;

char s[maxn];

int posa[maxn], posb[maxn], suma, sumb, rnk[maxn];

pair<string, int> c[maxn];

string ans;

int tot;

void solve(int l, int r) {
  ++tot;
  c[tot].se = tot;
  if (s[l] == 'a') {
    int pos = l;
    while (pos < r) {
      c[tot].fi += "ab";
      pos = posb[rnk[pos]] + 1;
      while (s[pos] == 'b' && pos <= r)
        pos++;
    }
  } else {
    rep(i, lpos, rnk[r]) {
      string tmp = "";
      rep(j, l, r) if (rnk[j] >= i) tmp += s[j];
      c[tot].fi = max(c[tot].fi, tmp);
    }
  }
}

signed main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  rep(i, 1, 2 * n) {
    if (s[i] == 'a')
      posa[++suma] = i, rnk[i] = suma;
    else
      posb[++sumb] = i, rnk[i] = sumb;
    if (suma == sumb)
      solve(last, i), last = i + 1, lpos = suma + 1;
  }
  per(i, n, 1) ans = max(ans, c[i].fi + ans);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a, b) for (register int i = (a); i <= (b); ++i)
#define per(i, a, b) for (register int i = (a); i >= (b); --i)
#define loop(it, v) for (auto it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (register int i = head[x]; i; i = edge[i].nex)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, cnt) memset(a, cnt, sizeof(a))
#define cop(a, b) memcpy(a, b, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define SC(t, x) static_cast<t>(x)
#define ub upper_bound
#define lb lower_bound
#define pqueue priority_queue
#define mp make_pair
#define pb push_back
#define pof pop_front
#define pob pop_back
#define fi first
#define se second
#define y1 y1_
#define Pi acos(-1.0)
#define iv inline void
#define enter putchar('\n')
#define siz(x) ((int)x.size())
#define file(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout)
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef queue<int> qi;
typedef queue<pii> qii;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<string, int> msi;
const int maxn = 6e3 + 100;
const int inf = 0x3f3f3f3f;
const int iinf = 1 << 30;
const ll linf = 2e18;
const ll mod = 998244353;
const double eps = 1e-7;
template <class T = int> T read() {
  T f = 1, a = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    a = (a << 3) + (a << 1) + ch - '0';
    ch = getchar();
  }
  return a * f;
}

int n, last = 1, lpos = 1;

char s[maxn];

int posa[maxn], posb[maxn], suma, sumb, rnk[maxn];

pair<string, int> c[maxn];

string ans;

int tot;

void solve(int l, int r) {
  ++tot;
  c[tot].se = tot;
  if (s[l] == 'a') {
    int pos = l;
    while (pos < r) {
      c[tot].fi += "ab";
      pos = posb[rnk[pos]] + 1;
      while (s[pos] == 'b' && pos <= r)
        pos++;
    }
  } else {
    rep(i, lpos, rnk[r]) {
      string tmp = "";
      rep(j, l, r) if (rnk[j] >= i) tmp += s[j];
      c[tot].fi = max(c[tot].fi, tmp);
    }
  }
}

signed main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  rep(i, 1, 2 * n) {
    if (s[i] == 'a')
      posa[++suma] = i, rnk[i] = suma;
    else
      posb[++sumb] = i, rnk[i] = sumb;
    if (suma == sumb)
      solve(last, i), last = i + 1, lpos = suma + 1;
  }
  per(i, n, 1) ans = max(ans, c[i].fi + ans);
  cout << ans << endl;
  return 0;
}