Language: C++

#include <bits/stdc++.h>
using namespace std;

#define loop(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < n; ++i)
#define dump(a) cerr << #a << " = " << (a) << "(L:" << __LINE__ << ")" << endl

const string YES = "Yes";
const string NO = "No";

vector<string> s;
vector<string> ans;
bool memo[100001][8][8][8];
bool visit[100001][8][8][8];
bool dfs(int i, int a, int b, int c) {
    a = min(a, 5);
    b = min(b, 5);
    c = min(c, 5);
    if (visit[i][a][b][c]) return memo[i][a][b][c];
    if (i == s.size()) return true;
    bool ret = false;
    if (s[i] == "AB") {
        if (0 < a && dfs(i + 1, a - 1, b + 1, c)) {
            ans.push_back("B");
            return true;
        }
        if (0 < b && dfs(i + 1, a + 1, b - 1, c)) {
            ans.push_back("A");
            return true;
        }
    } else if (s[i] == "AC") {
        if (0 < a && dfs(i + 1, a - 1, b, c + 1)) {
            ans.push_back("C");
            return true;
        }
        if (0 < c && dfs(i + 1, a + 1, b, c - 1)) {
            ans.push_back("A");
            return true;
        }
    } else if (s[i] == "BC") {
        if (0 < b && dfs(i + 1, a, b - 1, c + 1)) {
            ans.push_back("C");
            return true;
        }
        if (0 < c && dfs(i + 1, a, b + 1, c - 1)) {
            ans.push_back("B");
            return true;
        }
    }
    return false;
}

void solve(long long N, int A, int B, int C, std::vector<std::string> s_) {
    memset(memo, 0, sizeof(memo));
    memset(visit, 0, sizeof(visit));
    s = move(s_);
    bool ok = dfs(0, A, B, C);
    if (!ok) {
        cout << NO << endl;
    } else {
        reverse(ans.begin(), ans.end());
        cout << YES << endl;
        rep(i, N) { cout << ans[i] << endl; }
    }
}

int main() {
    long long N;
    scanf("%lld", &N);
    long long A;
    scanf("%lld", &A);
    long long B;
    scanf("%lld", &B);
    long long C;
    scanf("%lld", &C);
    std::vector<std::string> s(N);
    for (int i = 0; i < N; i++) {
        std::cin >> s[i];
    }
    solve(N, A, B, C, std::move(s));
    return 0;
}


Compiler output:
a.cc: In function 'bool dfs(int, int, int, int)':
a.cc:19:9: error: reference to 'visit' is ambiguous
   19 |     if (visit[i][a][b][c]) return memo[i][a][b][c];
      |         ^~~~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:80,
                 from a.cc:1:
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:14:6: note:                 'bool visit [100001][8][8][8]'
   14 | bool visit[100001][8][8][8];
      |      ^~~~~
a.cc: In function 'void solve(long long int, int, int, int, std::vector<std::__cxx11::basic_string<char> >)':
a.cc:55:12: error: reference to 'visit' is ambiguous
   55 |     memset(visit, 0, sizeof(visit));
      |            ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:14:6: note:                 'bool visit [100001][8][8][8]'
   14 | bool visit[100001][8][8][8];
      |      ^~~~~
a.cc:55:29: error: reference to 'visit' is ambiguous
   55 |     memset(visit, 0, sizeof(visit));
      |                             ^~~~~
/usr/include/c++/14/variant:1855:5: note: candidates are: 'template<class _Visitor, class ... _Variants> constexpr std::__detail::__variant::__visit_result_t<_Visitor, _Variants ...> std::visit(_Visitor&&, _Variants&& ...)'
 1855 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^~~~~
a.cc:14:6: note:                 'bool visit [100001][8][8][8]'
   14 | bool visit[100001][8][8][8];
      |      ^~~~~