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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

int n, a[3];
vector<char> ans;

int main() {
  cin >> n >> a[0] >> a[1] >> a[2];
  vector<string> s(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];

  for (int i = 0; i < n; i++) {
    int x = s[i][0] - 'A', y = s[i][1] - 'A', z = 3 - x - y;

    if (a[x] == 0 && a[y] == 0) {
      cout << "No"
           << "\\n";
      return 0;
    }

    else if (a[x] == 1 && a[y] == 1 && a[z] == 0) {
      if (i == n - 1) {
        a[x]++, a[y]--;
        ans.push_back(s[i][0]);
      } else if (s[i + 1].find(s[i][0]) != string::npos) {
        a[x]++, a[y]--;
        ans.push_back(s[i][0]);
      } else {
        a[x]--, a[y]++;
        ans.push_back(s[i][1]);
      }
    } else if (a[x] <= a[y]) {
      a[x]++, a[y]--;
      ans.push_back(s[i][0]);
    } else if (a[x] > a[y]) {
      a[x]--, a[y]++;
      ans.push_back(s[i][1]);
    }
  }
  cout << "Yes"
       << "\\n";
  for (int i = 0; i < n; i++) {
    cout << ans[i] << "\\n";
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
// typedef __int64 LL;
typedef vector<int> ivec;
typedef vector<string> svec;
typedef pair<int, int> P;

int zero(int x, int y, int z) {
  int t = 0;
  if (x == 0)
    t++;
  if (y == 0)
    t++;
  if (z == 0)
    t++;
  return t;
}

int one(int x, int y, int z) {
  int t = 0;
  if (x == 1)
    t++;
  if (y == 1)
    t++;
  if (z == 1)
    t++;
  return t;
}

int two(int x, int y, int z) {
  int t = 0;
  if (x == 2)
    t++;
  if (y == 2)
    t++;
  if (z == 2)
    t++;
  return t;
}
P ave(int x, int y) {
  if (x >= y)
    return P(x - 1, y + 1);
  else
    return P(x + 1, y - 1);
}
const int MOD = 1000000007;

vector<string> ho;
vector<char> v;
int main() {
  int n, a, b, c;
  cin >> n >> a >> b >> c;

  if (zero(a, b, c) == 3) {
    cout << "No"
         << "\\n";
    return 0;
  }

  if ((zero(a, b, c) == 1 && one(a, b, c) == 2) ||
      (zero(a, b, c) == 2 && two(a, b, c) == 1)) {
    for (int i = 0; i < n; i++)
      cin >> ho[i];
    for (int i = 0; i < n; i++) {
      if (ho[i] == "AB") {
        if (a * b == 0) {
          if (a == 0) {
            a++, b--;
            v.push_back('A');
          } else {
            a--, b++;
            v.push_back('B');
          }
        } else {
          if (i != n - 1 && ho[i + 1] == "AC") {
            a++, b--;
            v.push_back('A');
          } else {
            a--, b++;
            v.push_back('B');
          }
        }
      }

      if (ho[i] == "BC") {
        if (b * c == 0) {
          if (b == 0) {
            b++, c--;
            v.push_back('B');
          } else {
            b--, c++;
            v.push_back('C');
          }
        } else {
          if (i != n && ho[i + 1] == "AB") {
            b++, c--;
            v.push_back('B');
          } else {
            b--, c++;
            v.push_back('C');
          }
        }
      }

      if (ho[i] == "AC") {
        if (c * a == 0) {
          if (c == 0) {
            c++, a--;
            v.push_back('C');
          } else {
            c--, a++;
            v.push_back('A');
          }
        } else {
          if (i != n && ho[i + 1] == "BC") {
            c++, a--;
            v.push_back('C');
          } else {
            c--, a++;
            v.push_back('A');
          }
        }
      }
    }
  }

  for (int ima = 0; ima < n; ima++) {
    string s;
    cin >> s;

    if (s == "AB") {
      if (a == 0 && b == 0) {
        cout << "No"
             << "\\n";
        return 0;
      } else {
        if (a >= b) {
          a--;
          b++;
          v.push_back('B');
        } else {
          a++;
          b--;
          v.push_back('A');
        }
      }
    }

    else if (s == "BC") {
      if (b == 0 && c == 0) {
        cout << "No"
             << "\\n";
        return 0;
      } else {
        if (b >= c) {
          b--;
          c++;
          v.push_back('C');
        } else {
          b++;
          c--;
          v.push_back('B');
        }
      }
    }

    else if (s == "AC") {
      if (c == 0 && a == 0) {
        cout << "No"
             << "\\n";
        return 0;
      } else {
        if (c >= a) {
          c--;
          a++;
          v.push_back('A');
        } else {
          c++;
          a--;
          v.push_back('C');
        }
      }
    }
  }

  cout << "Yes"
       << "\\n";
  for (int i = 0; i < n; i++) {
    cout << v[i] << "\\n";
  }
  return 0;
}