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

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 1000000007;
/*
void calc(stirng S, vector<vector<vector<int>>> &ABC){
    vector<vector<vector<int>>> tmp(3,vector<vector<int>>(3,vector<int>(3,0)));
    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){
            for(int k=0; k<3; k++){
                if(S=="AB"){
                    tmp[i+1][j-1][k]=ABC[i][j][k];
                    tmp[i-1][j+1][k]=ABC[i][j][k];
                }else if(S=="BC"){
                    tmp[i][j+1][k-1]=ABC[i][j][k];
                    tmp[i][j-1][k+1]=ABC[i][j][k];
                }else if(S=="AC"){
                    tmp[i+1][j][k-1]=ABC[i][j][k];
                    tmp[i-1][j][k+1]=ABC[i][j][k];
                }
            }
        }
    }
    ABC=tmp;
}
*/

vector<char> vec;
vector<string> arr;
int N;
bool dfs(int cnt, ll a, ll b, ll c) {
  if (cnt == N) {
    return true;
  }

  string S = arr[cnt];
  if (S == "AB") {
    if (a > 0) {
      vec[cnt] = 'B';
      if (dfs(cnt + 1, a - 1, b + 1, c)) {
        return true;
      }
    }
    if (b > 0) {
      vec[cnt] = 'A';
      if (dfs(cnt + 1, a + 1, b - 1, c)) {
        return true;
      }
    }
  } else if (S == "BC") {
    if (b > 0) {
      vec[cnt] = 'C';
      if (dfs(cnt + 1, a, b - 1, c + 1)) {
        return true;
      }
    }
    if (c > 0) {
      vec[cnt] = 'B';
      if (dfs(cnt + 1, a, b + 1, c - 1)) {
        return true;
      }
    }
  } else if (S == "AC") {
    if (a > 0) {
      vec[cnt] = 'C';
      if (dfs(cnt + 1, a - 1, b, c + 1)) {
        return true;
      }
    }
    if (c > 0) {
      vec[cnt] = 'A';
      if (dfs(cnt + 1, a + 1, b, c - 1)) {
        return true;
      }
    }
  }
  return false;
}

int main() {
  ll A, B, C;
  cin >> N >> A >> B >> C;
  vec = vector<char>(N);

  if (A + B + C <= 2) {
    arr = {};
    for (int i = 0; i < N; i++) {
      string S;
      cin >> S;
      arr.push_back(S);
    }
    if (dfs(0, A, B, C)) {
      cout << "Yes" << endl;
      for (int i = 0; i < N; i++) {
        cout << vec[i] << endl;
      }
      return 0;
    } else {
      cout << "No" << endl;
      return 0;
    }
  } else {
    for (int i = 0; i < N; i++) {
      string S;
      cin >> S;
      if (S == "AB") {
        if (A == 0 && B == 0) {
          cout << "No" << endl;
          return 0;
        } else {
          if (A > B) {
            vec[i] = 'B';
            A--;
            B++;
          } else {
            vec[i] = 'A';
            B--;
            A++;
          }
        }
      } else if (S == "BC") {
        if (B == 0 && C == 0) {
          cout << "No" << endl;
          return 0;
        } else {
          if (B > C) {
            vec[i] = 'C';
            B--;
            C++;
          } else {
            vec[i] = 'B';
            C--;
            B++;
          }
        }
      } else if (S == "AC") {
        if (A == 0 && C == 0) {
          cout << "No" << endl;
          return 0;
        } else {
          if (C > A) {
            vec[i] = 'A';
            C--;
            A++;
          } else {
            vec[i] = 'C';
            A--;
            C++;
          }
        }
      }
    }
  }

  cout << "Yes" << endl;
  for (int i = 0; i < N; i++) {
    cout << vec[i] << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 1000000007;
/*
void calc(stirng S, vector<vector<vector<int>>> &ABC){
    vector<vector<vector<int>>> tmp(3,vector<vector<int>>(3,vector<int>(3,0)));
    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){
            for(int k=0; k<3; k++){
                if(S=="AB"){
                    tmp[i+1][j-1][k]=ABC[i][j][k];
                    tmp[i-1][j+1][k]=ABC[i][j][k];
                }else if(S=="BC"){
                    tmp[i][j+1][k-1]=ABC[i][j][k];
                    tmp[i][j-1][k+1]=ABC[i][j][k];
                }else if(S=="AC"){
                    tmp[i+1][j][k-1]=ABC[i][j][k];
                    tmp[i-1][j][k+1]=ABC[i][j][k];
                }
            }
        }
    }
    ABC=tmp;
}
*/

vector<char> vec;
int N;
bool dfs(int cnt, ll a, ll b, ll c) {
  if (cnt == N) {
    return true;
  }

  string S;
  cin >> S;
  if (S == "AB") {
    if (a > 0) {
      vec[cnt] = 'B';
      if (dfs(cnt + 1, a - 1, b + 1, c)) {
        return true;
      }
    }
    if (b > 0) {
      vec[cnt] = 'A';
      if (dfs(cnt + 1, a + 1, b - 1, c)) {
        return true;
      }
    }
  } else if (S == "BC") {
    if (b > 0) {
      vec[cnt] = 'C';
      if (dfs(cnt + 1, a, b - 1, c + 1)) {
        return true;
      }
    }
    if (c > 0) {
      vec[cnt] = 'B';
      if (dfs(cnt + 1, a, b + 1, c - 1)) {
        return true;
      }
    }
  } else if (S == "AC") {
    if (a > 0) {
      vec[cnt] = 'C';
      if (dfs(cnt + 1, a - 1, b, c + 1)) {
        return true;
      }
    }
    if (c > 0) {
      vec[cnt] = 'A';
      if (dfs(cnt + 1, a + 1, b, c - 1)) {
        return true;
      }
    }
  }
  return false;
}

int main() {
  ll A, B, C;
  cin >> N >> A >> B >> C;
  vec = vector<char>(N);

  if (A + B + C <= 2) {
    if (dfs(0, A, B, C)) {
      cout << "Yes" << endl;
      for (int i = 0; i < N; i++) {
        cout << vec[i] << endl;
      }
      return 1;
    } else {
      cout << "No" << endl;
      return 0;
    }
  } else {
    for (int i = 0; i < N; i++) {
      string S;
      cin >> S;
      if (S == "AB") {
        if (A == 0 && B == 0) {
          cout << "No" << endl;
          return 0;
        } else {
          if (A > B) {
            vec[i] = 'B';
            A--;
            B++;
          } else {
            vec[i] = 'A';
            B--;
            A++;
          }
        }
      } else if (S == "BC") {
        if (B == 0 && C == 0) {
          cout << "No" << endl;
          return 0;
        } else {
          if (B > C) {
            vec[i] = 'C';
            B--;
            C++;
          } else {
            vec[i] = 'B';
            C--;
            B++;
          }
        }
      } else if (S == "AC") {
        if (A == 0 && C == 0) {
          cout << "No" << endl;
          return 0;
        } else {
          if (C > A) {
            vec[i] = 'A';
            C--;
            A++;
          } else {
            vec[i] = 'C';
            A--;
            C++;
          }
        }
      }
    }
  }

  cout << "Yes" << endl;
  for (int i = 0; i < N; i++) {
    cout << vec[i] << endl;
  }
}