Problem p02692 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, A, B, C;
  cin >> N >> A >> B >> C;
  vector<string> s(N);
  string res;
  REP(i, N) cin >> s[i];
  REP(i, N) {
    if (s[i] == "AB") {
      if (A == 0 && B == 0)
        break;
      if (A == 0) {
        ++A;
        --B;
        res.push_back('A');
      } else if (B == 0) {
        ++B;
        --A;
        res.push_back('B');
      } else if (i < N - 1) {
        if (s[i + 1].find('A') != string::npos) {
          ++A;
          --B;
          res.push_back('A');
        } else {
          ++B;
          --A;
          res.push_back('B');
        }
      } else {
        ++A;
        --B;
        res.push_back('A');
      }
    } else if (s[i] == "BC") {
      if (B == 0 && C == 0)
        break;
      if (B == 0) {
        ++B;
        --C;
        res.push_back('B');
      } else if (C == 0) {
        ++C;
        --B;
        res.push_back('C');
      } else if (i < N - 1) {
        if (s[i + 1].find('B') != string::npos) {
          ++B;
          --C;
          res.push_back('B');
        } else {
          ++C;
          --B;
          res.push_back('C');
        }
      } else {
        ++B;
        --C;
        res.push_back('B');
      }
    } else {
      if (A == 0 && C == 0)
        break;
      if (A == 0) {
        ++A;
        --C;
        res.push_back('A');
      } else if (C == 0) {
        ++C;
        --A;
        res.push_back('C');
      } else if (i < N - 1) {
        if (s[i + 1].find('A') != string::npos) {
          ++A;
          --C;
          res.push_back('A');
        } else {
          ++C;
          --A;
          res.push_back('C');
        }
      } else {
        ++A;
        --C;
        res.push_back('A');
      }
    }
  }
  if (SZ(res) == N) {
    cout << "Yes" << endl;
    REP(i, N) { cout << res[i] << endl; }
  } else {
    cout << "No" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, A, B, C;
  cin >> N >> A >> B >> C;
  vector<string> s(N);
  string res;
  REP(i, N) cin >> s[i];
  REP(i, N) {
    if (s[i] == "AB") {
      if (A == 0 && B == 0)
        break;
      if (A == 0) {
        ++A;
        --B;
        res.push_back('A');
      } else if (B == 0) {
        ++B;
        --A;
        res.push_back('B');
      } else if (i < N - 1) {
        if (s[i + 1].find('A') != string::npos) {
          ++A;
          --B;
          res.push_back('A');
        } else {
          ++B;
          --A;
          res.push_back('B');
        }
      } else {
        ++A;
        --B;
        res.push_back('A');
      }
    } else if (s[i] == "BC") {
      if (B == 0 && C == 0)
        break;
      if (B == 0) {
        ++B;
        --C;
        res.push_back('B');
      } else if (C == 0) {
        ++C;
        --B;
        res.push_back('C');
      } else if (i < N) {
        if (s[i + 1].find('B') != string::npos) {
          ++B;
          --C;
          res.push_back('B');
        } else {
          ++C;
          --B;
          res.push_back('C');
        }
      } else {
        ++B;
        --C;
        res.push_back('B');
      }
    } else {
      if (A == 0 && C == 0)
        break;
      if (A == 0) {
        ++A;
        --C;
        res.push_back('A');
      } else if (C == 0) {
        ++C;
        --A;
        res.push_back('C');
      } else if (i < N) {
        if (s[i + 1].find('A') != string::npos) {
          ++A;
          --C;
          res.push_back('A');
        } else {
          ++C;
          --A;
          res.push_back('C');
        }
      } else {
        ++A;
        --C;
        res.push_back('A');
      }
    }
  }
  if (SZ(res) == N) {
    cout << "Yes" << endl;
    REP(i, N) { cout << res[i] << endl; }
  } else {
    cout << "No" << endl;
  }

  return 0;
}