Problem p02692 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  int N, A, B, C;
  cin >> N >> A >> B >> C;
  vector<string> S(N);
  for (auto &x : S)
    cin >> x;
  int t = 0;
  if (!A || !B || !C) {
    vector<map<vector<int>, pair<int, int>>> a(1);
    a[0][{A, B, C}] = {-1, -1};
    vector<int> cc;
    for (t = 0; t < N; ++t) {
      a.push_back({});
      int i = S[t][0] - 'A';
      int j = S[t][1] - 'A';
      for (auto p : a[t]) {
        if (p.first[i]) {
          auto c = p.first;
          --c[i];
          ++c[j];
          if (!a.back().count(c))
            a.back()[c] = {i, j};
          if (c[0] && c[1] && c[2]) {
            cc = c;
            goto out;
          }
        }
        if (p.first[j]) {
          auto c = p.first;
          --c[j];
          ++c[i];
          if (!a.back().count(c))
            a.back()[c] = {j, i};
          if (c[0] && c[1] && c[2]) {
            cc = c;
            goto out;
          }
        }
      }
      if (a.back().empty()) {
        cout << "No" << endl;
        return 0;
      }
    }
  out:;
    if (cc.empty()) {
      cc = a[N].begin()->first;
      t = N + 1;
    }
    vector<int> res;
    for (int tt = t + 1; tt > 0; --tt) {
      auto ij = a[tt][cc];
      res.push_back(ij.second);
      --cc[ij.second];
      ++cc[ij.first];
    }
    reverse(res.begin(), res.end());
    cout << "Yes" << endl;
    for (auto i : res)
      cout << "ABC"[i] << endl;
    t = (int)res.size();
  } else {
    cout << "Yes" << endl;
  }
  vector<int> b(3);
  for (; t < N; ++t) {
    int x = 0, y = 1;
    if (b[S[t][x] - 'A'] < b[S[t][y] - 'A'])
      swap(x, y);
    cout << S[t][x] << endl;
    --b[S[t][x] - 'A'];
    ++b[S[t][y] - 'A'];
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  int N, A, B, C;
  cin >> N >> A >> B >> C;
  vector<string> S(N);
  for (auto &x : S)
    cin >> x;
  int t = 0;
  if (!A || !B || !C) {
    vector<map<vector<int>, pair<int, int>>> a(1);
    a[0][{A, B, C}] = {-1, -1};
    vector<int> cc;
    for (t = 0; t < N; ++t) {
      a.push_back({});
      int i = S[t][0] - 'A';
      int j = S[t][1] - 'A';
      for (auto p : a[t]) {
        if (p.first[i]) {
          auto c = p.first;
          --c[i];
          ++c[j];
          if (!a.back().count(c))
            a.back()[c] = {i, j};
          if (c[0] && c[1] && c[2]) {
            cc = c;
            goto out;
          }
        }
        if (p.first[j]) {
          auto c = p.first;
          --c[j];
          ++c[i];
          if (!a.back().count(c))
            a.back()[c] = {j, i};
          if (c[0] && c[1] && c[2]) {
            cc = c;
            goto out;
          }
        }
      }
      if (a.back().empty()) {
        cout << "No" << endl;
        return 0;
      }
    }
  out:;
    if (cc.empty()) {
      cc = a[N].begin()->first;
      t = N - 1;
    }
    vector<int> res;
    for (int tt = t + 1; tt > 0; --tt) {
      auto ij = a[tt][cc];
      res.push_back(ij.second);
      --cc[ij.second];
      ++cc[ij.first];
    }
    reverse(res.begin(), res.end());
    cout << "Yes" << endl;
    for (auto i : res)
      cout << "ABC"[i] << endl;
    t = (int)res.size();
  } else {
    cout << "Yes" << endl;
  }
  vector<int> b(3);
  for (; t < N; ++t) {
    int x = 0, y = 1;
    if (b[S[t][x] - 'A'] < b[S[t][y] - 'A'])
      swap(x, y);
    cout << S[t][x] << endl;
    --b[S[t][x] - 'A'];
    ++b[S[t][y] - 'A'];
  }
}