Problem p02967 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int le[N], ri[N], a[N], n;
int A, B, C, cnt[4], h[4];
char e;
void del(int x) {
  le[ri[x]] = le[x];
  ri[le[x]] = ri[x];
  h[a[x]]--;
}
int getmin() {
  int g = -1;
  for (int i = 0; i < 3; i++)
    if (cnt[i] != -1 && (g == -1 || cnt[g] > cnt[i]))
      g = i;
  cnt[g] = -1;
  return g;
}
int main() {
  for (e = getchar(); e == 'A' || e == 'B' || e == 'C'; e = getchar())
    a[++n] = e - 'A';
  for (int i = 1; i <= n; i++)
    le[i] = i - 1, ri[i] = i + 1;
  ri[0] = 1;
  le[n + 1] = n;
  a[0] = a[n + 1] = 3;
  a[0]++;
  for (int i = ri[0]; i <= n; i = ri[i]) {
    if (a[i] == a[le[i]])
      del(i);
    else
      cnt[a[i]]++;
  }
  memcpy(h, cnt, sizeof h);
  A = getmin();
  B = getmin();
  C = getmin();
  memset(cnt, 0, sizeof cnt);
  int b1 = 0, c1 = 0;
  for (int i = ri[0]; i <= n; i = ri[i]) {
    if (a[i] == A) {
      cnt[2 * b1 + c1]++;
      b1 = c1 = 0;
    }
    if (a[i] == B)
      b1 = 1;
    if (a[i] == C)
      c1 = 1;
  }
  cnt[2 * b1 + c1]++;
  if (a[le[n + 1]] == C && a[le[le[n + 1]]] == A && cnt[3] + cnt[2] < cnt[1])
    del(le[n + 1]), cnt[1]--;
  for (int i = ri[0]; i <= n; i = ri[i]) {
    if (cnt[3] + cnt[2] >= cnt[1])
      break;
    if (a[i] == C) {
      if (!le[i] && a[ri[i]] == A)
        del(i), cnt[1]--;
      else if (a[le[i]] == A && a[ri[i]] == A)
        del(le[i]), del(i), cnt[1]--;
    }
  }
  assert(cnt[3] + cnt[2] >= cnt[1]);
  for (int i = ri[0]; i <= n; i = ri[i]) {
    if (h[B] == h[C])
      break;
    if (a[i] == C) {
      if (a[le[i]] != a[ri[i]])
        del(i);
    }
  }
  assert(h[B] == h[C]);
  for (int i = ri[0]; i <= n; i = ri[i]) {
    if (h[A] == h[B])
      break;
    if (a[i] == B) {
      if (a[le[i]] == C && a[le[le[i]]] != a[ri[i]])
        del(le[i]), del(i);
    }
    if (a[i] == C) {
      if (a[le[i]] == B && a[le[le[i]]] != a[ri[i]])
        del(le[i]), del(i);
    }
  }
  for (int i = ri[0]; i <= n; i = ri[i])
    putchar('A' + a[i]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int le[N], ri[N], a[N], n;
int A, B, C, cnt[4], h[4];
char e;
void del(int x) {
  le[ri[x]] = le[x];
  ri[le[x]] = ri[x];
  h[a[x]]--;
}
int getmin() {
  int g = -1;
  for (int i = 0; i < 3; i++)
    if (cnt[i] != -1 && (g == -1 || cnt[g] > cnt[i]))
      g = i;
  cnt[g] = -1;
  return g;
}
int main() {
  for (e = getchar(); e == 'A' || e == 'B' || e == 'C'; e = getchar())
    a[++n] = e - 'A';
  for (int i = 1; i <= n; i++)
    le[i] = i - 1, ri[i] = i + 1;
  ri[0] = 1;
  le[n + 1] = n;
  a[0] = a[n + 1] = 3;
  a[0]++;
  for (int i = ri[0]; i <= n; i = ri[i]) {
    if (a[i] == a[le[i]])
      del(i);
    else
      cnt[a[i]]++;
  }
  memcpy(h, cnt, sizeof h);
  A = getmin();
  B = getmin();
  C = getmin();
  if (!h[A])
    return 0;
  memset(cnt, 0, sizeof cnt);
  int b1 = 0, c1 = 0;
  for (int i = ri[0]; i <= n; i = ri[i]) {
    if (a[i] == A) {
      cnt[2 * b1 + c1]++;
      b1 = c1 = 0;
    }
    if (a[i] == B)
      b1 = 1;
    if (a[i] == C)
      c1 = 1;
  }
  cnt[2 * b1 + c1]++;
  if (a[le[n + 1]] == C && a[le[le[n + 1]]] == A && cnt[3] + cnt[2] < cnt[1])
    del(le[n + 1]), cnt[1]--;
  for (int i = ri[0]; i <= n; i = ri[i]) {
    if (cnt[3] + cnt[2] >= cnt[1])
      break;
    if (a[i] == C) {
      if (!le[i] && a[ri[i]] == A)
        del(i), cnt[1]--;
      else if (a[le[i]] == A && a[ri[i]] == A)
        del(le[i]), del(i), cnt[1]--;
    }
  }
  assert(cnt[3] + cnt[2] >= cnt[1]);
  for (int i = ri[0]; i <= n; i = ri[i]) {
    if (h[B] == h[C])
      break;
    if (a[i] == C) {
      if (a[le[i]] != a[ri[i]])
        del(i);
    }
  }
  assert(h[B] == h[C]);
  for (int i = ri[0]; i <= n; i = ri[i]) {
    if (h[A] == h[B])
      break;
    if (a[i] == B) {
      if (a[le[i]] == C && a[le[le[i]]] != a[ri[i]])
        del(le[i]), del(i);
    }
    if (a[i] == C) {
      if (a[le[i]] == B && a[le[le[i]]] != a[ri[i]])
        del(le[i]), del(i);
    }
  }
  for (int i = ri[0]; i <= n; i = ri[i])
    putchar('A' + a[i]);
}