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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i, k, N) for (int i = k; i < N; i++)
#define MP make_pair
#define MT make_tuple // tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(), (x).end()

using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

// 0 0 ?
vector<string> s;
ll N;
vector<char> ans; //
bool ok = false;

bool memo[3][3][3][100001];
void dfs(ll a, ll b, ll c, ll now) {
  if (ok)
    return;
  if (memo[a][b][c][now] == false)
    return;
  if (now == N) {
    cout << "Yes" << endl;
    rep(i, 0, N) { cout << ans[i] << endl; }
    ok = true;
    return;
  }
  if (s[now] == "AB") {
    if (a == 0 && b == 0) {
      return;
    } else if (a == 0) {
      ans[now] = 'A';
      dfs(a + 1, b - 1, c, now + 1);
      ans[now] = '0';
    } else if (b == 0) {
      ans[now] = 'B';
      dfs(a - 1, b + 1, c, now + 1);
      ans[now] = '0';
    } else {
      ans[now] = 'A';
      dfs(a + 1, b - 1, c, now + 1);
      ans[now] = '0';
      ans[now] = 'B';
      dfs(a - 1, b + 1, c, now + 1);
      ans[now] = '0';
    }
  } else if (s[now] == "BC") {
    if (b == 0 && c == 0) {
      return;
    } else if (b == 0) {
      ans[now] = 'B';
      dfs(a, b + 1, c - 1, now + 1);
      ans[now] = '0';
    } else if (c == 0) {
      ans[now] = 'C';
      dfs(a, b - 1, c + 1, now + 1);
      ans[now] = '0';
    } else {
      ans[now] = 'B';
      dfs(a, b + 1, c - 1, now + 1);
      ans[now] = '0';
      ans[now] = 'C';
      dfs(a, b - 1, c + 1, now + 1);
      ans[now] = '0';
    }
  } else {
    if (a == 0 && c == 0) {
      return;
    } else if (a == 0) {
      ans[now] = 'A';
      dfs(a + 1, b, c - 1, now + 1);
      ans[now] = '0';
    } else if (c == 0) {
      ans[now] = 'C';
      dfs(a - 1, b, c + 1, now + 1);
      ans[now] = '0';
    } else {
      ans[now] = 'A';
      dfs(a + 1, b, c - 1, now + 1);
      ans[now] = '0';
      ans[now] = 'C';
      dfs(a - 1, b, c + 1, now + 1);
      ans[now] = '0';
    }
  }
  memo[a][b][c][now] = false;
}
int main() {
  rep(i, 0, 3) rep(j, 0, 3) rep(k, 0, 3) rep(l, 0, 100001) {
    memo[i][j][k][l] = true;
  }
  ll A, B, C;
  cin >> N >> A >> B >> C;
  s.resize(N);
  ans.resize(N);
  rep(i, 0, N) { cin >> s[i]; }
  if (A + B + C <= 2) {
    dfs(A, B, C, 0);
    if (!ok) {
      cout << "No";
    }
  } else if (A == 0 && B == 0 && s[0] == "AB") {
    cout << "No";
  } else if (B == 0 && C == 0 && s[0] == "BC") {
    cout << "No";
  } else if (C == 0 && A == 0 && s[0] == "AC") {
    cout << "No";
  } else {
    cout << "Yes" << endl;
    rep(i, 0, N) {
      if (s[i] == "AB") {
        if (A <= B) {
          cout << "A" << endl;
          A++;
          B--;
        } else {
          cout << "B" << endl;
          B++;
          A--;
        }
      }
      if (s[i] == "BC") {
        if (C <= B) {
          cout << "C" << endl;
          C++;
          B--;
        } else {
          cout << "B" << endl;
          B++;
          C--;
        }
      }
      if (s[i] == "AC") {
        if (A <= C) {
          cout << "A" << endl;
          A++;
          C--;
        } else {
          cout << "C" << endl;
          C++;
          A--;
        }
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i, k, N) for (int i = k; i < N; i++)
#define MP make_pair
#define MT make_tuple // tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(), (x).end()

using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

// 0 0 ?
vector<string> s;
ll N;
vector<char> ans; //
bool ok = false;

bool memo[3][3][3][100001];
void dfs(ll a, ll b, ll c, ll now) {
  if (ok)
    return;
  if (memo[a][b][c][now] == false)
    return;
  if (now == N) {
    cout << "Yes" << endl;
    rep(i, 0, N) { cout << ans[i] << endl; }
    ok = true;
    return;
  }
  if (s[now] == "AB") {
    if (a == 0 && b == 0) {
      return;
    } else if (a == 0) {
      ans[now] = 'A';
      dfs(a + 1, b - 1, c, now + 1);
      ans[now] = '0';
    } else if (b == 0) {
      ans[now] = 'B';
      dfs(a - 1, b + 1, c, now + 1);
      ans[now] = '0';
    } else {
      ans[now] = 'A';
      dfs(a + 1, b - 1, c, now + 1);
      ans[now] = '0';
      ans[now] = 'B';
      dfs(a - 1, b + 1, c, now + 1);
      ans[now] = '0';
    }
  } else if (s[now] == "BC") {
    if (b == 0 && c == 0) {
      return;
    } else if (b == 0) {
      ans[now] = 'B';
      dfs(a, b + 1, c - 1, now + 1);
      ans[now] = '0';
    } else if (c == 0) {
      ans[now] = 'C';
      dfs(a, b - 1, c + 1, now + 1);
      ans[now] = '0';
    } else {
      ans[now] = 'B';
      dfs(a, b + 1, c - 1, now + 1);
      ans[now] = '0';
      ans[now] = 'C';
      dfs(a, b - 1, c + 1, now + 1);
      ans[now] = '0';
    }
  } else {
    if (a == 0 && c == 0) {
      return;
    } else if (a == 0) {
      ans[now] = 'A';
      dfs(a + 1, b, c - 1, now + 1);
      ans[now] = '0';
    } else if (c == 0) {
      ans[now] = 'C';
      dfs(a + 1, b, c - 1, now + 1);
      ans[now] = '0';
    } else {
      ans[now] = 'A';
      dfs(a + 1, b, c - 1, now + 1);
      ans[now] = '0';
      ans[now] = 'C';
      dfs(a + 1, b, c - 1, now + 1);
      ans[now] = '0';
    }
  }
  memo[a][b][c][now] = false;
}
int main() {
  rep(i, 0, 3) rep(j, 0, 3) rep(k, 0, 3) rep(l, 0, 100001) {
    memo[i][j][k][l] = true;
  }
  ll A, B, C;
  cin >> N >> A >> B >> C;
  s.resize(N);
  ans.resize(N);
  rep(i, 0, N) { cin >> s[i]; }
  if (A + B + C <= 2) {
    dfs(A, B, C, 0);
    if (!ok) {
      cout << "No";
    }
  } else if (A == 0 && B == 0 && s[0] == "AB") {
    cout << "No";
  } else if (B == 0 && C == 0 && s[0] == "BC") {
    cout << "No";
  } else if (C == 0 && A == 0 && s[0] == "AC") {
    cout << "No";
  } else {
    cout << "Yes" << endl;
    rep(i, 0, N) {
      if (s[i] == "AB") {
        if (A <= B) {
          cout << "A" << endl;
          A++;
          B--;
        } else {
          cout << "B" << endl;
          B++;
          A--;
        }
      }
      if (s[i] == "BC") {
        if (C <= B) {
          cout << "C" << endl;
          C++;
          B--;
        } else {
          cout << "B" << endl;
          B++;
          C--;
        }
      }
      if (s[i] == "AC") {
        if (A <= C) {
          cout << "A" << endl;
          A++;
          C--;
        } else {
          cout << "C" << endl;
          C++;
          A--;
        }
      }
    }
  }
  return 0;
}