Problem:


Score : 600 points

Problem Statement

There is a game that involves three variables, denoted A, B, and C.

As the game progresses, there will be N events where you are asked to make a choice.
Each of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.

After each choice, none of A, B, and C should be negative.

Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.

Constraints

1 \leq N \leq 10^5

0 \leq A,B,C \leq 10^9

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

If it is possible to make N choices under the condition, print Yes; otherwise, print No.

Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.

In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1.

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

8 6 9 1
AC
BC
AB
BC
AC
BC
AB
AB

Sample Output 4

Yes
C
B
B
C
C
B
A
A



Sample input:
2 1 3 0
AB
AC


Sample output:
Yes
A
C

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    n, a, b, c = LI()
    s = SR(n)
    ans = [None] * n
    dp = [deque() for i in range(3)]
    l = [[0] * 3 for i in range(n)]
    d = {"A": 0, "B": 1, "C": 2}
    for i in range(n):
        l[i][d[s[i][0]]] = 1
        l[i][d[s[i][1]]] = 1
    for i in range(n - 1, 0, -1):
        l[i - 1][0] += l[i][0]
        l[i - 1][1] += l[i][1]
        l[i - 1][2] += l[i][2]
    dd = [0] * 3
    ddd = defaultdict(int)
    for i in range(n):
        if s[i] <= "AB":
            if a < b:
                ans[i] = "A"
                a += 1
                b -= 1
            elif a > b:
                ans[i] = "B"
                a -= 1
                b += 1
            else:
                if a <= 0:
                    if len(dp[0]) - dd[0] <= 0 or len(dp[1]) - dd[1] <= 0:
                        print("No")
                        return
                    if len(dp[0]) - dd[0] > len(dp[1]) - dd[1]:
                        x = dp[0].popleft()
                        while ddd[x]:
                            x = dp[0].popleft()
                        ddd[x] = 1
                        dd[0] += 1
                        if s[x] <= "AB":
                            dd[1] += 1
                        else:
                            dd[2] += 1
                        ans[x] = "A"
                        ans[i] = "B"
                    elif len(dp[0]) - dd[0] < len(dp[1]) - dd[1]:
                        x = dp[1].popleft()
                        while ddd[x]:
                            x = dp[1].popleft()
                        ddd[x] = 1
                        dd[1] += 1
                        if s[x] <= "AB":
                            dd[0] += 1
                        else:
                            dd[2] += 1
                        ans[x] = "B"
                        ans[i] = "A"
                    else:
                        if l[i][0] < l[i][1]:
                            x = dp[0].popleft()
                            while ddd[x]:
                                x = dp[0].popleft()
                            ddd[x] = 1
                            dd[0] += 1
                            if s[x] <= "AB":
                                dd[1] += 1
                            else:
                                dd[2] += 1
                            ans[x] = "A"
                            ans[i] = "B"
                        else:
                            x = dp[1].popleft()
                            while ddd[x]:
                                x = dp[1].popleft()
                            dd[1] += 1
                            if s[x] <= "AB":
                                dd[0] += 1
                            else:
                                dd[2] += 1
                            ddd[x] = 1
                            ans[x] = "B"
                            ans[i] = "A"
                else:
                    dp[0].append(i)
                    dp[1].append(i)
                    a -= 1
                    b -= 1
        elif s[i] <= "AC":
            if a < c:
                ans[i] = "A"
                a += 1
                c -= 1
            elif a > c:
                ans[i] = "C"
                a -= 1
                c += 1
            else:
                if a <= 0:
                    if len(dp[0]) - dd[0] <= 0 or len(dp[2]) - dd[2] <= 0:
                        print("No")
                        return
                    if len(dp[0]) - dd[0] > len(dp[2]) - dd[2]:
                        x = dp[0].popleft()
                        while ddd[x]:
                            x = dp[0].popleft()
                        dd[0] += 1
                        if s[x] <= "AB":
                            dd[1] += 1
                        else:
                            dd[2] += 1
                        ddd[x] = 1
                        ans[x] = "A"
                        ans[i] = "C"
                    elif len(dp[0]) - dd[0] < len(dp[2]) - dd[2]:
                        x = dp[2].popleft()
                        while ddd[x]:
                            x = dp[2].popleft()
                        dd[2] += 1
                        if s[x] <= "AC":
                            dd[0] += 1
                        else:
                            dd[1] += 1
                        ddd[x] = 1
                        ans[x] = "C"
                        ans[i] = "A"
                    else:
                        if l[i][0] < l[i][2]:
                            x = dp[0].popleft()
                            while ddd[x]:
                                x = dp[0].popleft()
                            dd[0] += 1
                            if s[x] <= "AB":
                                dd[1] += 1
                            else:
                                dd[2] += 1
                            ddd[x] = 1
                            ans[x] = "A"
                            ans[i] = "C"
                        else:
                            x = dp[2].popleft()
                            while ddd[x]:
                                x = dp[2].popleft()
                            dd[2] += 1
                            if s[x] <= "AC":
                                dd[0] += 1
                            else:
                                dd[1] += 1
                            ddd[x] = 1
                            ans[x] = "C"
                            ans[i] = "A"
                else:
                    dp[0].append(i)
                    dp[2].append(i)
                    a -= 1
                    c -= 1
        else:
            if b < c:
                ans[i] = "B"
                b += 1
                c -= 1
            elif b > c:
                ans[i] = "C"
                b -= 1
                c += 1
            else:
                if b <= 0:
                    if len(dp[1]) - dd[1] <= 0 or len(dp[2]) - dd[2] <= 0:
                        print("No")
                        return
                    if len(dp[1]) - dd[1] > len(dp[2]) - dd[2]:
                        x = dp[1].popleft()
                        while ddd[x]:
                            x = dp[1].popleft()
                        dd[1] += 1
                        if s[x] <= "AB":
                            dd[0] += 1
                        else:
                            dd[2] += 1
                        ddd[x] = 1
                        ans[x] = "B"
                        ans[i] = "C"
                    elif len(dp[1]) - dd[1] < len(dp[2]) - dd[2]:
                        x = dp[2].popleft()
                        while ddd[x]:
                            x = dp[2].popleft()
                        dd[2] += 1
                        if s[x] <= "AC":
                            dd[0] += 1
                        else:
                            dd[1] += 1
                        ddd[x] = 1
                        ans[x] = "C"
                        ans[i] = "B"
                    else:
                        if l[i][1] < l[i][2]:
                            x = dp[1].popleft()
                            while ddd[x]:
                                x = dp[1].popleft()
                            dd[1] += 1
                            if s[x] <= "AB":
                                dd[0] += 1
                            else:
                                dd[2] += 1
                            ddd[x] = 1
                            ans[x] = "B"
                            ans[i] = "C"
                        else:
                            x = dp[2].popleft()
                            while ddd[x]:
                                x = dp[2].popleft()
                            dd[2] += 1
                            if s[x] <= "AC":
                                dd[0] += 1
                            else:
                                dd[1] += 1
                            ddd[x] = 1
                            ans[x] = "C"
                            ans[i] = "B"
                else:
                    dp[1].append(i)
                    dp[2].append(i)
                    b -= 1
                    c -= 1
    ll = ["A", "B", "C"]
    print("Yes")
    for i in range(3):
        while dp[i]:
            p = dp[i].pop()
            if ans[p] <= None:
                ans[p] = ll[i]
    for i in ans:
        print(i)
    return


#main
if __name__ <= '__main__':
    solve()

Submission status: Runtime Error

Solution code:
S = []
choice = []
for _ in range(N):
    s = input()
    if s == "AB":
        if A == 0 and B == 0:
            print("No")
            break
        else:
            if A >= B:
                A -= 1
                B += 1
                choice.append("B")
            else:
                B -= 1
                A += 1
                choice.append("A")
                
    if s == "BC":
        if B == 0 and C == 0:
            print("No")
            break
        else:
            if B >= B:
                B -= 1
                C += 1
                choice.append("C")

            else:
                C -= 1
                B += 1
                choice.append("B")

    if s == "AC":
        if A == 0 and C == 0:
            print("No")
            break
        else:
            if A >= C:
                A -= 1
                C += 1
                choice.append("C")
            else:
                C -= 1
                A += 1
                choice.append("A")
else:
    for s in choice:
        print(s)

Submission status: Runtime Error

Solution code:
n, *x = map(int, input().split())
k = 'ABC'
ans = []
s = [input() for _ in range(n)]
for p in range(n):
    sp = s[p]
    if sp == 'AB':
        i = 0
        j = 1
    elif sp == 'BC':
        i = 1
        j = 2
    else:
        i = 0
        j = 2
    if x[i] == x[j] == 0:
        print('No')
        break
    if x[i] == x[j] and i < n - 1:
        sp1 = s[p + 1]
        if k[i] in si1:
            x[i] += 1
            x[j] -= 1
            ans.append(k[i])
        else:
            x[i] -= 1
            x[j] += 1
            ans.append(k[j])
    elif x[i] < x[j]:
        x[i] += 1
        x[j] -= 1
        ans.append(k[i])
    else:
        x[i] -= 1
        x[j] += 1
        ans.append(k[j])
else:
    print('Yes')
    print(*ans, sep='\n')

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#ifdef __LOCAL
    #define DBG(X) cout << #X << " = " << (X) << endl;
    #define SAY(X) cout << (X) << endl;
    #include <filesystem>
    namespace fs = std::filesystem;
#else
    #define DBG(X)
    #define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int dx[]={1, 0, -1, 0, 1,-1, 1,-1};
int dy[]={0, 1, 0, -1, 1, 1,-1,-1};
const int INT_INF = (int)(2e9);
const ll  LL_INF = (ll)(2e18);
static mt19937 _g(time(nullptr));
 
inline ll randint(ll a, ll b) { ll w = (_g() << 31LL) ^ _g(); return a + w % (b - a + 1); }
inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); };
template<typename T, typename S> inline ostream& operator<<(ostream& os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; }
template<typename T, typename S> inline ostream& operator<<(ostream& os, const map<T, S> p) { for (auto el : p) cout << "[" << el.first << ";" << el.second << "]"; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const vector<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto& elem : ret) cin >> elem; return ret; }

int N;
ll A,B,C;
vector<string> S;
void input(){
    fast_io();
    #ifdef __LOCAL
        fs::path __p = __FILE__;
        fs::path __input, __output;
        __input = __output = __p.parent_path();
        __input += string("/input/") + string(__p.stem()) + string(".txt");
        __output += string("/output/") + string(__p.stem()) + string(".txt");
        freopen(__input.c_str(), "r", stdin);
        freopen(__output.c_str(), "w", stdout);
    #endif
    cin >> N >> A >> B >> C;
    for (int i = 0; i < N; i++)
    {
        string s; cin >> s;
        S.push_back(s);
    }    
}

void back(int i,vector<char> &ans){
    string last = ans.back();
    ans.pop_back();
    if(S[i-1]=="BC" && last=="C"){
        ans.push_back('B');
        B++,C--;
        B++,C--;
    }
    if(S[i-1]=="BC" && last=="B"){
        ans.push_back('C');
        B--,C++;
        B--,C++;
    }
    if(S[i-1]=="AB" && last=="B"){
        ans.push_back('A');
        B--,A++;
        B--,A++;
    }
    if(S[i-1]=="AB" && last=="A"){
        ans.push_back('B');
        B++,A--;
        B++,A--;
    }
    if(S[i-1]=="AC" && last=="A"){
        ans.push_back('C');
        C++,A--;
        C++,A--;
    }
    if(S[i-1]=="AC" && last=="C"){
        ans.push_back('A');
        C--,A++;
        C--,A++;
    }
}

int solve(){
    if(A==0 && B==0 && C==0){
        cout << "No" << endl;
        return 0;
    }
    if(A==0 && B==0 && S[0]=="AB"){
        cout << "No" << endl;
        return 0;
    }
    if(C==0 && B==0 && S[0]=="BC"){
        cout << "No" << endl;
        return 0;
    }
    if(A==0 && C==0 && S[0]=="AC"){
        cout << "No" << endl;
        return 0;
    }
    // 以下すべてYES
    vector<char> ans;
    for (int i = 0; i < N; i++)
    {
        if(S[i]=="AB"){
            if(A==0 && B==0){
                back(i,ans);
            }
            if(B>A){
                ans.push_back('A');
                A++,B--;
            }
            else{
                ans.push_back('B');
                A--,B++;
            }
        }
        if(S[i]=="BC"){
            if(C==0 && B==0){
                back(i,ans);
            }
            if(C>B){
                ans.push_back('B');
                B++,C--;
            }
            else{
                ans.push_back('C');
                B--,C++;
            }
        }
        if(S[i]=="AC"){
            if(C==0 && A==0){
                back(i,ans);
            }
            if(C>A){
                ans.push_back('A');
                A++,C--;
            }
            else{
                ans.push_back('C');
                A--,C++;
            }
        }
        printf("%d %d %d\n",A,B,C);
    }
    
    cout << "Yes" << endl;
    for(auto a:ans) cout << a << "\n";
    return 0;
}   

int main()
{
    input();
    solve();
    return 0;
}

Submission status: Runtime Error

Solution code:
N, A, B, C = map(int, input().split())
opts = []
for _ in range(N):
    opt = input()
    opts.append((ord(opt[0]) - ord("A"), ord(opt[1]) - ord("A")))

choices = []
nums = [A, B, C]
for i, opt in enumerate(opts):
    if nums[opt[0]] == 0 and nums[opt[1]] == 0:
        print("No")
        break
    if nums[opt[0]] == 1 and nums[opt[1]] == 1:
        if i < N - 1 and opt != opts[i + 1]:
            choice = next(set(opt) ^ set(opts[i + 1]))
            nums[opt] += 1
            nums[opt ^ opt[0] ^ opt[1]] -= 1
            choices.append(choice)
            continue
    if nums[opt[0]] >= nums[opt[1]]:
        choices.append(opt[1])
        nums[opt[1]] += 1
        nums[opt[0]] -= 1
    else:
        choices.append(opt[0])
        nums[opt[0]] += 1
        nums[opt[1]] -= 1
else:
    print("Yes")
    for c in choices:
        print(chr(c + ord("A")))

Submission status: Runtime Error

Solution code:

n,a,b,c = map(int, input().split())
ref = [a,b,c,[]]
for i in range(n):
    nxt = []
    roun = []
    s = input()
    for l in ref:
        if s == "AB":
            if l[1]>0 and ([l[0]+1,l[1]-1,l[2],l[3].append("A")] not in roun):
                nxt.append([l[0]+1,l[1]-1,l[2],l[3].append("A")])
                roun.append([l[0]+1,l[1]-1,l[2],l[3].append("A")])
            if l[0]>0 and ([l[0]-1,l[1]+1,l[2],l[3].append("B")] not in roun):
                nxt.append([l[0]-1,l[1]+1,l[2],l[3].append("B")])
                roun.append([l[0]-1,l[1]+1,l[2],l[3].append("B")])
        elif s == "AC":
            if l[2]>0 and ([l[0]+1,l[1],l[2]-1,l[3].append("A")] not in roun):
                nxt.append([l[0]+1,l[1],l[2]-1,l[3].append("A")])
                roun.append([l[0]+1,l[1],l[2]-1,l[3].append("A")])
            if l[0]>0 and ([l[0]-1,l[1],l[2]+1,l[3].append("C")] not in roun):
                nxt.append([l[0]-1,l[1],l[2]+1,l[3].append("C")])
                roun.append([l[0]-1,l[1],l[2]+1,l[3].append("C")])
        elif s == "BC":
            if l[2]>0 and ([l[0],l[1]+1,l[2]-1,l[3].append("B")] not in roun):
                nxt.append([l[0],l[1]+1,l[2]-1,l[3].append("B")])
                roun.append([l[0],l[1]+1,l[2]-1,l[3].append("B")])
            if l[1]>0 and ([l[0],l[1]-1,l[2]+1,l[3].append("C")] not in roun):
                nxt.append([l[0],l[1]-1,l[2]+1,l[3].append("C")])
                roun.append([l[0],l[1]-1,l[2]+1,l[3].append("C")])
        else: pass
    
    ref = nxt

if ref!=[]:
    for i in ref[0][3]:
        print(i)

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
n,a,b,c=map(int,input().split())
s=[input() for i in range(n)]
dp=[dict() for i in range(n+1)]
dp[0][""]=[a,b,c]
for i in range(n):
    if len(dp[i])==0:
        print("No")
        break
    if s[i]=="AB":
        for j in dp[i]:
            a,b,c=dp[i][j]
            if dp[i][j][0]>0:
                dp[i+1][j+"A"]=[a-1,b+1,c]
            if dp[i][j][1]>0:
                dp[i+1][j+"B"]=[a+1,b-1,c]
    elif s[i]=="AC":
        for j in dp[i]:
            a,b,c=dp[i][j]
            if dp[i][j][0]>0:
                dp[i+1][j+"A"]=[a-1,b,c+1]
            if dp[i][j][2]>0:
                dp[i+1][j+"C"]=[a+1,b,c-1]
    else:
        for j in dp[i]:
            a,b,c=dp[i][j]
            if dp[i][j][1]>0:
                dp[i+1][j+"B"]=[a,b-1,c+1]
            if dp[i][j][2]>0:
                dp[i+1][j+"C"]=[a,b+1,c-1]
else:
    print("Yes")
    z=list(dp[-1])
    print(z)
    for i in range(n):
        print(z[-1][i])

Submission status: Runtime Error

Solution code:
n, a, b, c, d = map(int, input().split())

ab = 0
ac = 0
bc = 0

ans = []
ng = False
for i in range(n):
    n = input()
    if n == "AB":
        if a != 0:
            a -= 1
            b += 1
            ans.append("B")
            ab = i
        elif b != 0:
            a += 1
            b -= 1
            ab = i
            ans.append("A")
        elif ac > 0 and c > 0:
            a += 2
            c -= 2
            ans[ac] = "A"
        elif bc > 0 and c > 0:
            b += 2
            c -= 2
            ans[bc] = "B"
        else:
            ng = True
            break

    elif n == "AC":
        if b != 0:
            b -= 1
            c += 1
            ans.append("C")
            ac = i
        elif c != 0:
            b += 1
            c -= 1
            ac = i
            ans.append("B")
        elif ab > 0 and a > 0:
            b += 2
            a -= 2
            ans[ab] = "B"
        elif ac > 0 and a > 0:
            c += 2
            a -= 2
            ans[bc] = "C"
        else:
            ng = True
            break

    else:
        if a != 0:
            a -= 1
            b += 1
            ans.append("B")
            ab = i
        elif b != 0:
            a += 1
            b -= 1
            ab = i
            ans.append("A")
        elif ac > 0 and c > 0:
            a += 2
            c -= 2
            ans[ac] = "A"
        elif bc > 0 and c > 0:
            b += 2
            c -= 2
            ans[bc] = "B"
        else:
            ng = True
            break

if ng and (a > 0 or b > 0 or c > 0):
    print("Yes")
    for i in range(len(ans)):
        print(ans[i])
else:
    print("No")

Submission status: Runtime Error

Solution code:
n,a,b,c=map(int,input().split())
file={"A":a,"B":b,"C":c}
flg=0
ans=[]
s=list(input())

for i in range(n-1):
    next=list(input())
    #now=list(input()) 一回前
    x,y=now
    if x==y:
        if now==prev:
            file[x]+=1
            file[y]-=1
            ans.push(x)
        else:
            if x in next:
                file[x]+=1
                file[y]-=1
                ans.push(x)
            else:
                file[x]-=1
                file[y]+=1
                ans.push(y)
    else:
        if filex>y:
            file[x]-=1
            file[y]+=1
            ans.push(y)
        else:
            file[x]+=1
            file[y]-=1
            ans.push(x)
    now=next
    if a<0 or b<0 or c<0:
        flg=1
        break
if flg==1:
    print("No")
else:
    print("Yes")
    for i in range(n):
        print(ans[i])

Submission status: Runtime Error

Solution code:
n,a,b,c = map(int,input().split())
Y = []
f = True
S = []
for i in range(n):
    S.append(input())
GB = []
i = 0
g = False
while i<n:
    s = S[i]
    if s == "AB":
        if a>b:
            a -= 1
            b += 1
            Y.append("B")
        elif a<b:
            a += 1
            b -= 1
            Y.append("A")
        else:
            if g:
                a -= 1
                b += 1
                Y.append("B")
            else:
                a += 1
                b -= 1
                Y.append("A")
                GB.append(i)
    elif s == "AC":
        if a>c:
            a -= 1
            c += 1
            Y.append("C")
        elif a<c:
            a += 1
            c -= 1
            Y.append("A")
        else:
            if g:
                a -= 1
                c += 1
                Y.append("C")
            else:
                a += 1
                c -= 1
                Y.append("A")
                GB.append(i)
    else:
        if c>b:
            c -= 1
            b += 1
            Y.append("B")
        elif c<b:
            c += 1
            b -= 1
            Y.append("C")
        else:
            if g:
                c -= 1
                b += 1
                Y.append("B")
            else:
                c += 1
                b -= 1
                Y.append("C")
                GB.append(i)
    # print("i",i)
    # print(a,b,c)
    # print(GB)
    if a<0 or b<0 or c<0:
        if GB:
            i = GB.pop(-1)
            del Y[i]
        else:
            f = False
            print("No")
            break
    
    i += 1 
        
if f:
    print("Yes")
    for y in Y:
        print(y)

Submission status: Runtime Error

Solution code:
from collections import defaultdict,deque
import sys, bisect, math, itertools
from functools import lru_cache
# @lru_cache(maxsize=None)
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp():
    '''
    一つの整数
    '''
    return int(input())
def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))

n,a,b,c = inpl()
dic = defaultdict(int)
act = [list(input()) for i in range(n)]
set_act = set(act)
ans=[]
for ind in range(n):
    i=act[ind]
    if i == "AB\n":
        if a < b:
            ans.append("A")
            a += 1
            b-=1
        elif a == b:
            if b == 0:
                print("No")
                exit()
            if ind != n - 1 and act[ind + 1] == "BC\n":
                ans.append("B")
                b += 1
                a -= 1
            else:
                ans.append("A")
                a += 1
                b -= 1
        else:
            ans.append("B")
            a-=1
            b+=1
    elif i == "BC\n":
        if b < c:
            ans.append("B")
            b += 1
            c-=1
        elif b == c:
            if b == 0:
                print("No")
                exit()
            if ind != n - 1 and act[ind + 1] == "AC\n":
                ans.append("C")
                c += 1
                b -= 1
            else:
                ans.append("B")
                b += 1
                c-=1
        else:
            ans.append("C")
            c += 1
            b-=1
    else:
        if c < a:
            ans.append("C")
            c += 1
            a -= 1
        elif a == c:
            if a == 0:
                print("No")
                exit()
            if ind != n - 1 and act[ind + 1] == "BC\n":
                ans.append("C")
                c += 1
                a -= 1
            else:
                ans.append("A")
                a += 1
                c -= 1
        else:
            if a==c and a == 0:
                print("No")
                exit()
            ans.append("A")
            a += 1
            c -= 1
print("Yes")
for i in ans:
    print(i)