Language: C++

//https://www.hamayanhamayan.com/entry/2020/05/03/224516
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

int N, A, B, C;
string S[101010];
//---------------------------------------------------------------------------------------------------
vector<char> ans;
bool dfs(int cu, int a, int b, int c) {
    if (cu == N) return true;

    if (S[cu] == "AB") {
        if (a == 0 && b == 0) return false;
        if (0 < a && dfs(cu + 1, a - 1, b + 1, c)) {
            ans.push_back('B');
            return true;
        }
        if (0 < b && dfs(cu + 1, a + 1, b - 1, c)) {
            ans.push_back('A');
            return true;
        }
        return false;
    }

    if (S[cu] == "AC") {
        if (a == 0 && c == 0) return false;
        if (0 < a && dfs(cu + 1, a - 1, b, c + 1)) {
            ans.push_back('C');
            return true;
        }
        if (0 < c && dfs(cu + 1, a + 1, b, c - 1)) {
            ans.push_back('A');
            return true;
        }
        return false;
    }

    if (S[cu] == "BC") {
        if (b == 0 && c == 0) return false;
        if (0 < b && dfs(cu + 1, a, b - 1, c + 1)) {
            ans.push_back('C');
            return true;
        }
        if (0 < c && dfs(cu + 1, a, b + 1, c - 1)) {
            ans.push_back('B');
            return true;
        }
        return false;
    }
}
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N >> A >> B >> C;
    rep(i, 0, N) cin >> S[i];

    dfs(0, A, B, C);

    if (ans.size() == 0) cout << "No" << endl;
    else {
        printf("Yes\n");
        reverse(all(ans));
        fore(c, ans) printf("%c\n", c);
    }
}

Compiler output:
a.cc:59:16: error: macro "rep" passed 3 arguments, but takes just 2
   59 |     rep(i, 0, N) cin >> S[i];
      |                ^
a.cc:3:9: note: macro "rep" defined here
    3 | #define rep(i, n) for (int i = 0; i < (n); ++i)
      |         ^~~
a.cc: In function 'void _main()':
a.cc:59:5: error: 'rep' was not declared in this scope
   59 |     rep(i, 0, N) cin >> S[i];
      |     ^~~
a.cc:66:17: error: 'all' was not declared in this scope; did you mean 'std::filesystem::perms::all'?
   66 |         reverse(all(ans));
      |                 ^~~
      |                 std::filesystem::perms::all
In file included from /usr/include/c++/14/filesystem:51,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:200,
                 from a.cc:2:
/usr/include/c++/14/bits/fs_fwd.h:158:7: note: 'std::filesystem::perms::all' declared here
  158 |       all               =  0777,
      |       ^~~
a.cc:67:14: error: 'c' was not declared in this scope
   67 |         fore(c, ans) printf("%c\n", c);
      |              ^
a.cc:67:9: error: 'fore' was not declared in this scope; did you mean 'fork'?
   67 |         fore(c, ans) printf("%c\n", c);
      |         ^~~~
      |         fork
a.cc: In function 'bool dfs(int, int, int, int)':
a.cc:55:1: warning: control reaches end of non-void function [-Wreturn-type]
   55 | }
      | ^