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

#include <algorithm>
#include <cassert>
#include <iostream>
#include <string>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int N, A, B, C;
  cin >> N >> A >> B >> C;
  char ptr[3], ans[N];
  string op;
  if (A + B + C < 2) {
    for (int i = 0; i < N; ++i) {
      cin >> op;
      if (op == "AB") {
        if (A == 0 && B == 0) {
          cout << "No";
          return 0;
        } else
          ans[i] = A > B ? 'B' : 'A', swap(A, B);
      } else if (op == "AC") {
        if (A == 0 && C == 0) {
          cout << "No";
          return 0;
        } else
          ans[i] = A > C ? 'C' : 'A', swap(A, C);
      } else {
        if (C == 0 && B == 0) {
          cout << "No";
          return 0;
        } else
          ans[i] = B > C ? 'C' : 'B', swap(C, B);
      }
    }
  } else if (A + B + C == 2) {
    string ops[N];
    for (int i = 0; i < N; ++i)
      cin >> ops[i];
    for (int i = 0; i < N; ++i) {
      if (ops[i] == "AB") {
        if (A == 0 && B == 0) {
          cout << "No";
          return 0;
        } else if (A == 0)
          ans[i] = 'A', ++A, --B;
        else if (B == 0)
          ans[i] = 'B', ++B, --A;
        else {
          if (i != N - 1 && ops[i + 1] == "AC")
            ans[i] = 'A', ++A, --B;
          else
            ans[i] = 'B', ++B, --A;
        }
      } else if (ops[i] == "AC") {
        if (A == 0 && C == 0) {
          cout << "No";
          return 0;
        } else if (A == 0)
          ans[i] = 'A', ++A, --C;
        else if (C == 0)
          ans[i] = 'C', ++C, --A;
        else {
          if (i != N - 1 && ops[i + 1] == "AB")
            ans[i] = 'A', ++A, --C;
          else
            ans[i] = 'C', ++C, --A;
        }
      } else {
        if (C == 0 && B == 0) {
          cout << "No";
          return 0;
        } else if (B == 0)
          ans[i] = 'B', ++B, --C;
        else if (C == 0)
          ans[i] = 'C', ++C, --B;
        else {
          if (i != N - 1 && ops[i + 1] == "AB")
            ans[i] = 'B', ++B, --C;
          else
            ans[i] = 'C', ++C, --B;
        }
      }
    }
  } else {
    if (A != 0 && B != 0 && C != 0)
      ptr[0] = 'A', ptr[1] = 'C', ptr[2] = 'B';
    else if (B != 0 && C != 0)
      ptr[0] = 'B', ptr[1] = 'C', ptr[2] = B > C ? 'B' : 'C';
    else if (A != 0 && C != 0)
      ptr[0] = 'A', ptr[1] = A > C ? 'A' : 'C', ptr[2] = 'C';
    else if (A != 0 && B != 0)
      ptr[0] = A > B ? 'A' : 'B', ptr[1] = 'A', ptr[2] = 'B';
    else if (A != 0)
      ptr[0] = ptr[1] = ptr[2] = 'A';
    else if (B != 0)
      ptr[0] = ptr[1] = ptr[2] = 'B';
    else
      ptr[0] = ptr[1] = ptr[2] = 'C';
    for (int i = 0; i < N; ++i) {
      cin >> op;
      if (op == "AB") {
        if (ptr[0] == 'C') {
          cout << "No";
          return 0;
        } else if (ptr[2] == 'A')
          ans[i] = 'B', ++B, --A, ptr[2] = 'B';
        else if (ptr[1] == 'B')
          ans[i] = 'A', ++A, --B, ptr[1] = 'A';
        else if (ptr[0] == 'A')
          ans[i] = 'B', ++B, --A, ptr[0] = 'B';
        else
          ans[i] = 'A', ++A, --B, ptr[0] = 'A';
      } else if (op == "AC") {
        if (ptr[1] == 'B') {
          cout << "No";
          return 0;
        } else if (ptr[2] == 'A')
          ans[i] = 'C', ++C, --A, ptr[2] = 'C';
        else if (ptr[0] == 'C')
          ans[i] = 'A', ++A, --C, ptr[0] = 'A';
        else if (ptr[1] == 'A')
          ans[i] = 'C', ++C, --A, ptr[1] = 'C';
        else
          ans[i] = 'A', ++A, --C, ptr[1] = 'A';
      } else {
        if (ptr[2] == 'A') {
          cout << "No";
          return 0;
        } else if (ptr[0] == 'C')
          ans[i] = 'B', ++B, --C, ptr[0] = 'B';
        else if (ptr[1] == 'B')
          ans[i] = 'C', ++C, --B, ptr[1] = 'C';
        else if (ptr[2] == 'C')
          ans[i] = 'B', ++B, --C, ptr[2] = 'B';
        else
          ans[i] = 'C', ++C, --B, ptr[2] = 'C';
      }
    }
  }
  cout << "Yes\\n";
  for (int i = 0; i < N; ++i)
    cout << (char)ans[i] << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <string>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int N, A, B, C;
  cin >> N >> A >> B >> C;
  char ptr[3], ans[N];
  string op;
  if (A + B + C < 2) {
    for (int i = 0; i < N; ++i) {
      cin >> op;
      if (op == "AB") {
        if (A == 0 && B == 0) {
          cout << "No";
          return 0;
        } else
          ans[i] = A > B ? 'B' : 'A', swap(A, B);
      } else if (op == "AC") {
        if (A == 0 && C == 0) {
          cout << "No";
          return 0;
        } else
          ans[i] = A > C ? 'C' : 'A', swap(A, C);
      } else {
        if (C == 0 && B == 0) {
          cout << "No";
          return 0;
        } else
          ans[i] = B > C ? 'C' : 'B', swap(C, B);
      }
    }
  } else if (A + B + C == 2) {
    string ops[N];
    for (int i = 0; i < N; ++i)
      cin >> ops[i];
    for (int i = 0; i < N; ++i) {
      if (ops[i] == "AB") {
        if (A == 0 && B == 0) {
          cout << "No";
          return 0;
        } else if (A == 0)
          ans[i] = 'A', ++A, --B;
        else if (B == 0)
          ans[i] = 'B', ++B, --A;
        else {
          if (i != N - 1 && ops[i + 1] == "AC")
            ans[i] = 'A', ++A, --B;
          else
            ans[i] = 'B', ++B, --A;
        }
      } else if (ops[i] == "AC") {
        if (A == 0 && C == 0) {
          cout << "No";
          return 0;
        } else if (A == 0)
          ans[i] = 'A', ++A, --C;
        else if (C == 0)
          ans[i] = 'C', ++C, --A;
        else {
          if (i != N - 1 && ops[i + 1] == "AB")
            ans[i] = 'A', ++A, --C;
          else
            ans[i] = 'C', ++C, --A;
        }
      } else {
        if (C == 0 && B == 0) {
          cout << "No";
          return 0;
        } else if (B == 0)
          ans[i] = 'B', ++B, --C;
        else if (C == 0)
          ans[i] = 'C', ++C, --B;
        else {
          if (i != N - 1 && ops[i + 1] == "AB")
            ans[i] = 'B', ++B, --C;
          else
            ans[i] = 'C', ++C, --B;
        }
      }
    }
  } else {
    if (A != 0 && B != 0 && C != 0)
      ptr[0] = 'A', ptr[1] = 'C', ptr[2] = 'B';
    else if (B != 0 && C != 0)
      ptr[0] = 'B', ptr[1] = 'C', ptr[2] = B > C ? 'B' : 'C';
    else if (A != 0 && C != 0)
      ptr[0] = 'A', ptr[1] = A > C ? 'A' : 'C', ptr[2] = 'C';
    else if (A != 0 && B != 0)
      ptr[0] = A > B ? 'A' : 'B', ptr[1] = 'A', ptr[2] = 'B';
    else if (A != 0)
      ptr[0] = ptr[1] = ptr[2] = 'A';
    else if (B != 0)
      ptr[0] = ptr[1] = ptr[2] = 'B';
    else
      ptr[0] = ptr[1] = ptr[2] = 'C';
    for (int i = 0; i < N; ++i) {
      cin >> op;
      if (op == "AB") {
        if (ptr[0] == 'C') {
          cout << "No";
          return 0;
        } else if (ptr[0] == 'A')
          ans[i] = 'B', ++B, --A;
        else
          ans[i] = 'A', ++A, --B;
      } else if (op == "AC") {
        if (ptr[1] == 'B') {
          cout << "No";
          return 0;
        } else if (ptr[1] == 'A')
          ans[i] = 'C', ++C, --A;
        else
          ans[i] = 'A', ++A, --C;
      } else {
        if (ptr[2] == 'A') {
          cout << "No";
          return 0;
        } else if (ptr[2] == 'C')
          ans[i] = 'B', ++B, --C;
        else
          ans[i] = 'C', ++C, --B;
      }
      assert(A >= 0 && B >= 0 && C >= 0);
    }
  }
  cout << "Yes\\n";
  for (int i = 0; i < N; ++i)
    cout << (char)ans[i] << '\\n';
  return 0;
}