Problem p02967 (C++)

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

const int _ = 1e6 + 7;
char str[_], tmp[_];
int L, cnt[3], id[3];

void change() {
  for (int i = 0; i < 3; ++i)
    id[i] = i;
  for (int i = 1; i <= L; ++i)
    ++cnt[str[i] - 'A'];
  sort(id, id + 3, [&](int x, int y) { return cnt[x] < cnt[y]; });
  for (int i = 1; i <= L; ++i)
    for (int j = 0; j < 3; ++j)
      if (str[i] - 'A' == id[j]) {
        str[i] = 'A' + j;
        break;
      }
}

void output(char c) { putchar(id[c - 'A'] + 'A'); }
void output(string &str) {
  for (auto t : str)
    output(t);
}

#define PII pair<int, int>

int main() {
  scanf("%s", str + 1);
  for (int i = 1; str[i]; ++i)
    if (str[i] != tmp[L])
      tmp[++L] = str[i];
  memcpy(str, tmp, sizeof(tmp));
  change();
  int b1 = 0, b2 = 0, c1 = 0, c2 = 0, pre = 0;
  str[L + 1] = 'A';
  for (int i = 1; i <= L + 1; ++i)
    if (str[i] == 'A') {
      if (pre + 1 <= i - 1)
        if ((i - 1) - (pre + 1) >= 1) {
          ++b1;
          ++c1;
        } else if (str[i - 1] == 'B') {
          ++b1;
          ++b2;
        } else {
          ++c1;
          ++c2;
        }
      pre = i;
    }
  vector<string> arr;
  string tparr;
  bool flg = 0;
  if (str[L] != 'A' && str[L - 1] == 'A')
    if (str[L] == 'B' && b2 > c1) {
      --b2;
      L -= 2;
      flg = 1;
    } else if (str[L] == 'C' && c2 > b1) {
      --c2;
      L -= 2;
      flg = 1;
    }
  for (int i = 1; i <= L + 1; ++i)
    if (str[i] == 'A')
      if (tparr.size() == 1)
        if (tparr[0] == 'B' && b2 > c1) {
          --b2;
          if (i == 2) {
            arr.push_back(tparr = "");
            pre = i;
          }
          continue;
        } else if (tparr[0] == 'C' && c2 > b1) {
          --c2;
          if (i == 2) {
            arr.push_back(tparr = "");
            pre = i;
          }
          continue;
        } else {
          arr.push_back(tparr);
          tparr = "";
        }
      else {
        arr.push_back(tparr);
        tparr = "";
      }
    else if (tparr.empty() || str[i] != *--tparr.end())
      tparr.push_back(str[i]);
  if (flg)
    arr.push_back("");
  if (arr.size() == 1)
    return 0;
  int na = arr.size() - 1, nb = 0, nc = 0;
  for (auto t : arr)
    for (auto p : t)
      p == 'B' ? ++nb : ++nc;
  for (int i = 0; i < arr.size(); ++i) {
    if (i)
      output('A');
    string str = arr[i];
    if (str.size() >= 2) {
      if (nb > nc && *str.begin() == 'B') {
        --nb;
        str.erase(str.begin());
      }
      if (nb > nc && *--str.end() == 'B') {
        --nb;
        str.erase(--str.end());
      }
      if (nb < nc && *str.begin() == 'C') {
        --nc;
        str.erase(str.begin());
      }
      if (nb < nc && *--str.end() == 'C') {
        --nc;
        str.erase(--str.end());
      }
      while (min(nb, nc) > na &&
             (str.size() > 2 || i == 0 || i + 1 == arr.size())) {
        str.erase(--str.end());
        str.erase(--str.end());
        --nb;
        --nc;
      }
    }
    output(str);
  }
  return 0;
}

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

const int _ = 1e6 + 7;
char str[_], tmp[_];
int L, cnt[3], id[3];

void change() {
  for (int i = 0; i < 3; ++i)
    id[i] = i;
  for (int i = 1; i <= L; ++i)
    ++cnt[str[i] - 'A'];
  sort(id, id + 3, [&](int x, int y) { return cnt[x] < cnt[y]; });
  for (int i = 1; i <= L; ++i)
    for (int j = 0; j < 3; ++j)
      if (str[i] - 'A' == id[j]) {
        str[i] = 'A' + j;
        break;
      }
}

void output(char c) { putchar(id[c - 'A'] + 'A'); }
void output(string &str) {
  for (auto t : str)
    output(t);
}

#define PII pair<int, int>

int main() {
  scanf("%s", str + 1);
  for (int i = 1; str[i]; ++i)
    if (str[i] != tmp[L])
      tmp[++L] = str[i];
  memcpy(str, tmp, sizeof(tmp));
  change();
  int b1 = 0, b2 = 0, c1 = 0, c2 = 0, pre = 0;
  str[L + 1] = 'A';
  for (int i = 1; i <= L + 1; ++i)
    if (str[i] == 'A') {
      if (pre + 1 <= i - 1)
        if ((i - 1) - (pre + 1) >= 1) {
          ++b1;
          ++c1;
        } else if (str[i - 1] == 'B') {
          ++b1;
          ++b2;
        } else {
          ++c1;
          ++c2;
        }
      pre = i;
    }
  vector<string> arr;
  string tparr;
  bool flg = 0;
  if (str[L] != 'A' && str[L - 1] == 'A')
    if (str[L] == 'B' && b2 > c1) {
      --b2;
      L -= 2;
      flg = 1;
    } else if (str[L] == 'C' && c2 > b1) {
      --c2;
      L -= 2;
      flg = 1;
    }
  for (int i = 1; i <= L + 1; ++i)
    if (str[i] == 'A')
      if (tparr.size() == 1)
        if (tparr[0] == 'B' && b2 > c1) {
          --b2;
          if (i == 2) {
            arr.push_back(tparr = "");
            pre = i;
          }
          continue;
        } else if (tparr[0] == 'C' && c2 > b1) {
          --c2;
          if (i == 2) {
            arr.push_back(tparr = "");
            pre = i;
          }
          continue;
        } else {
          arr.push_back(tparr);
          tparr = "";
        }
      else {
        arr.push_back(tparr);
        tparr = "";
      }
    else if (tparr.empty() || str[i] != *--tparr.end())
      tparr.push_back(str[i]);
  if (flg)
    arr.push_back("");
  if (arr.size() == 1)
    return 0;
  int na = arr.size() - 1, nb = 0, nc = 0;
  for (auto t : arr)
    for (auto p : t)
      p == 'B' ? ++nb : ++nc;
  for (int i = 0; i < arr.size(); ++i) {
    if (i)
      output('A');
    string str = arr[i];
    if (str.size() >= 2) {
      if (nb > nc && *str.begin() == 'B') {
        --nb;
        str.erase(str.begin());
      }
      if (nb > nc && *--str.end() == 'B') {
        --nb;
        str.erase(--str.end());
      }
      if (nb < nc && *str.begin() == 'C') {
        --nc;
        str.erase(str.begin());
      }
      if (nb < nc && *--str.end() == 'C') {
        --nc;
        str.erase(--str.end());
      }
      while (min(nb, nc) > na &&
             (str.size() > 2 ||
              (i == 0 || i + 1 == arr.size()) && str.size() >= 2)) {
        str.erase(--str.end());
        str.erase(--str.end());
        --nb;
        --nc;
      }
    }
    output(str);
  }
  return 0;
}