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

#include "bits/stdc++.h"
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
const ll MOD = (ll)(1e9 + 7);
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int N;
ll A, B, C;
string S[100000];

bool memo[100000][5][5][5];
bool vis[100000][5][5][5];
bool solve(int n, int a, int b, int c) {
  if (a < 0 || b < 0 || c < 0)
    return false;
  if (a >= 5 || b >= 5 || c >= 5)
    return false;
  if (n == N)
    return true;
  if (vis[n][a][b][c])
    return memo[n][a][b][c];
  vis[n][a][b][c] = true;
  memo[n][a][b][c] = false;
  if (S[n] == "AB") {
    memo[n][a][b][c] |= solve(n + 1, a + 1, b - 1, c);
    memo[n][a][b][c] |= solve(n + 1, a - 1, b + 1, c);
  }
  if (S[n] == "BC") {
    memo[n][a][b][c] |= solve(n + 1, a, b + 1, c - 1);
    memo[n][a][b][c] |= solve(n + 1, a, b - 1, c + 1);
  }
  if (S[n] == "AC") {
    memo[n][a][b][c] |= solve(n + 1, a + 1, b, c - 1);
    memo[n][a][b][c] |= solve(n + 1, a - 1, b, c + 1);
  }
  return memo[n][a][b][c];
}

void print(int n, int a, int b, int c) {
  if (n == N)
    exit(0);
  if (S[n] == "AB") {
    if (solve(n + 1, a + 1, b - 1, c)) {
      cout << "A" << endl;
      print(n + 1, a + 1, b - 1, c);
    }
    if (solve(n + 1, a - 1, b + 1, c)) {
      cout << "B" << endl;
      print(n + 1, a - 1, b + 1, c);
    }
  }
  if (S[n] == "BC") {
    if (solve(n + 1, a, b + 1, c - 1)) {
      cout << "B" << endl;
      print(n + 1, a, b + 1, c - 1);
    }
    if (solve(n + 1, a, b - 1, c + 1)) {
      cout << "C" << endl;
      print(n + 1, a, b - 1, c + 1);
    }
  }
  if (S[n] == "AC") {
    if (solve(n + 1, a + 1, b, c - 1)) {
      cout << "A" << endl;
      print(n + 1, a + 1, b, c - 1);
    }
    if (solve(n + 1, a - 1, b, c + 1)) {
      cout << "C" << endl;
      print(n + 1, a - 1, b, c + 1);
    }
  }
}

signed main() {
  cin >> N >> A >> B >> C;
  rep(i, N) cin >> S[i];

  if (solve(0, min(A, 3LL), min(B, 3LL), min(C, 3LL))) {
    cout << "Yes" << endl;
    print(0, min(A, 3LL), min(B, 3LL), min(C, 3LL));
  } else {
    cout << "No" << endl;
    return 0;
  }

  if (A + B + C <= 3) {
    if (solve(0, A, B, C)) {
      cout << "Yes" << endl;
      print(0, A, B, C);
    }
  } else {
    rep(i, 3) {
      char AB;
      if (i == 0)
        AB = 'A';
      if (i == 1)
        AB = 'B';
      if (i == 2)
        AB = 'C';
      rep(j, 3) {
        char BC;
        if (j == 0)
          BC = 'A';
        if (j == 1)
          BC = 'B';
        if (j == 2)
          BC = 'C';
        rep(k, 3) {
          char AC;
          if (k == 0)
            AC = 'A';
          if (k == 1)
            AC = 'B';
          if (k == 2)
            AC = 'C';

          if (A < (ll)(AB == 'A') + (BC == 'A') + (AC == 'A'))
            continue;
          if (B < (ll)(AB == 'B') + (BC == 'B') + (AC == 'B'))
            continue;
          if (C < (ll)(AB == 'C') + (BC == 'C') + (AC == 'C'))
            continue;
          vector<char> ans;
          rep(l, N) {
            if (S[l] == "AB") {
              if (BC == 'A') {
                ans.pb(BC = 'B');
                continue;
              }
              if (AC == 'B') {
                ans.pb(AC = 'A');
                continue;
              }
              if (AB == 'A')
                ans.pb(AB = 'B');
              else if (AB == 'B')
                ans.pb(AB = 'A');
              else
                break;
            }
            if (S[l] == "BC") {
              if (AB == 'C') {
                ans.pb(AB = 'B');
                continue;
              }
              if (AC == 'B') {
                ans.pb(AC = 'C');
                continue;
              }
              if (BC == 'B')
                ans.pb(BC = 'C');
              else if (BC == 'C')
                ans.pb(BC = 'B');
              else
                break;
            }
            if (S[l] == "AC") {
              if (AB == 'C') {
                ans.pb(AB = 'A');
                continue;
              }
              if (BC == 'A') {
                ans.pb(BC = 'C');
                continue;
              }
              if (AC == 'A')
                ans.pb(AC = 'C');
              else if (AC == 'C')
                ans.pb(AC = 'A');
              else
                break;
            }
          }

          if (ans.size() == N) {
            cout << "Yes" << endl;
            rep(l, N) cout << ans[l] << endl;
            return 0;
          }
        }
      }
    }
  }
  cout << "No" << endl;
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
const ll MOD = (ll)(1e9 + 7);
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int N;
ll A, B, C;
string S[100000];

bool memo[100000][4][4][4];
bool vis[100000][4][4][4];
bool solve(int n, int a, int b, int c) {
  if (a < 0 || b < 0 || c < 0)
    return false;
  if (n == N)
    return true;
  if (vis[n][a][b][c])
    return memo[n][a][b][c];
  vis[n][a][b][c] = true;
  memo[n][a][b][c] = false;
  if (S[n] == "AB") {
    memo[n][a][b][c] |= solve(n + 1, a + 1, b - 1, c);
    memo[n][a][b][c] |= solve(n + 1, a - 1, b + 1, c);
  }
  if (S[n] == "BC") {
    memo[n][a][b][c] |= solve(n + 1, a, b + 1, c - 1);
    memo[n][a][b][c] |= solve(n + 1, a, b - 1, c + 1);
  }
  if (S[n] == "AC") {
    memo[n][a][b][c] |= solve(n + 1, a + 1, b, c - 1);
    memo[n][a][b][c] |= solve(n + 1, a - 1, b, c + 1);
  }
  return memo[n][a][b][c];
}

void print(int n, int a, int b, int c) {
  if (n = N)
    exit(0);
  if (S[n] == "AB") {
    if (solve(n + 1, a + 1, b - 1, c)) {
      cout << "A" << endl;
      print(n + 1, a + 1, b - 1, c);
    }
    if (solve(n + 1, a - 1, b + 1, c)) {
      cout << "B" << endl;
      print(n + 1, a - 1, b + 1, c);
    }
  }
  if (S[n] == "BC") {
    if (solve(n + 1, a, b + 1, c - 1)) {
      cout << "B" << endl;
      print(n + 1, a, b + 1, c - 1);
    }
    if (solve(n + 1, a, b - 1, c + 1)) {
      cout << "C" << endl;
      print(n + 1, a, b - 1, c + 1);
    }
  }
  if (S[n] == "AC") {
    if (solve(n + 1, a + 1, b, c - 1)) {
      cout << "A" << endl;
      print(n + 1, a + 1, b, c - 1);
    }
    if (solve(n + 1, a - 1, b, c + 1)) {
      cout << "C" << endl;
      print(n + 1, a - 1, b, c + 1);
    }
  }
}

signed main() {
  cin >> N >> A >> B >> C;
  rep(i, N) cin >> S[i];

  if (solve(0, max(A, 3LL), max(B, 3LL), max(C, 3LL))) {
    cout << "Yes" << endl;
    print(0, max(A, 3LL), max(B, 3LL), max(C, 3LL));
  } else {
    cout << "No" << endl;
    return 0;
  }

  if (A + B + C <= 3) {
    if (solve(0, A, B, C)) {
      cout << "Yes" << endl;
      print(0, A, B, C);
    }
  } else {
    rep(i, 3) {
      char AB;
      if (i == 0)
        AB = 'A';
      if (i == 1)
        AB = 'B';
      if (i == 2)
        AB = 'C';
      rep(j, 3) {
        char BC;
        if (j == 0)
          BC = 'A';
        if (j == 1)
          BC = 'B';
        if (j == 2)
          BC = 'C';
        rep(k, 3) {
          char AC;
          if (k == 0)
            AC = 'A';
          if (k == 1)
            AC = 'B';
          if (k == 2)
            AC = 'C';

          if (A < (ll)(AB == 'A') + (BC == 'A') + (AC == 'A'))
            continue;
          if (B < (ll)(AB == 'B') + (BC == 'B') + (AC == 'B'))
            continue;
          if (C < (ll)(AB == 'C') + (BC == 'C') + (AC == 'C'))
            continue;
          vector<char> ans;
          rep(l, N) {
            if (S[l] == "AB") {
              if (BC == 'A') {
                ans.pb(BC = 'B');
                continue;
              }
              if (AC == 'B') {
                ans.pb(AC = 'A');
                continue;
              }
              if (AB == 'A')
                ans.pb(AB = 'B');
              else if (AB == 'B')
                ans.pb(AB = 'A');
              else
                break;
            }
            if (S[l] == "BC") {
              if (AB == 'C') {
                ans.pb(AB = 'B');
                continue;
              }
              if (AC == 'B') {
                ans.pb(AC = 'C');
                continue;
              }
              if (BC == 'B')
                ans.pb(BC = 'C');
              else if (BC == 'C')
                ans.pb(BC = 'B');
              else
                break;
            }
            if (S[l] == "AC") {
              if (AB == 'C') {
                ans.pb(AB = 'A');
                continue;
              }
              if (BC == 'A') {
                ans.pb(BC = 'C');
                continue;
              }
              if (AC == 'A')
                ans.pb(AC = 'C');
              else if (AC == 'C')
                ans.pb(AC = 'A');
              else
                break;
            }
          }

          if (ans.size() == N) {
            cout << "Yes" << endl;
            rep(l, N) cout << ans[l] << endl;
            return 0;
          }
        }
      }
    }
  }
  cout << "No" << endl;
}