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: Accepted

Solution code:
from collections import deque
from itertools import product
import sys
import math
import numpy as np
import bisect

sys.setrecursionlimit(200000)
input = sys.stdin.readline


def read():
    N, A, B, C = map(int, input().strip().split())
    S = []
    for i in range(N):
        s = input().strip()
        S.append(s)
    return N, A, B, C, S


def choose_at_corner_state(A, B, C, s):
    """次の一手を返す"""
    if A == 0 and C == 0:
        if s == "BC" and not is_error_state(A, B-1, C+1):
            return ("C", A, B-1, C+1)
        if s == "AB" and not is_error_state(A+1, B-1, C):
            return ("A", A+1, B-1, C)
    elif A == 0 and B == 0:
        if s == "AC" and not is_error_state(A+1, B, C-1):
            return ("A", A+1, B, C-1)
        if s == "BC" and not is_error_state(A, B+1, C-1):
            return ("B", A, B+1, C-1)
    elif B == 0 and C == 0:
        if s == "AC" and not is_error_state(A-1, B, C+1):
            return ("C", A-1, B, C+1)
        if s == "AB" and not is_error_state(A-1, B+1, C):
            return ("B", A-1, B+1, C)
    return ("X", -1, -1, -1)


def is_error_state(A, B, C):
    if A < 0 or B < 0 or C < 0:
        return True
    return False


def is_corner_state(A, B, C):
    if A == 0 and B == 0:
        return True
    elif A == 0 and C == 0:
        return True
    elif B == 0 and C == 0:
        return True
    return False


def choose_at_normal_state(A, B, C, s0, s1):
    """先読みして、次の一手を返す"""
    if s0 == "BC":
        op0, a0, b0, c0 = ("C", A, B-1, C+1)
        if is_error_state(a0, b0, c0):
            pass
        elif is_corner_state(a0, b0, c0):
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        else:
            return (op0, a0, b0, c0)
        op0, a0, b0, c0 = ("B", A, B+1, C-1)
        if is_error_state(a0, b0, c0):
            pass
        elif is_corner_state(a0, b0, c0):
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        else:
            return (op0, a0, b0, c0)
    elif s0 == "AC":
        op0, a0, b0, c0 = ("C", A-1, B, C+1)
        if is_error_state(a0, b0, c0):
            pass
        elif is_corner_state(a0, b0, c0):
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        else:
            return (op0, a0, b0, c0)
        op0, a0, b0, c0 = ("A", A+1, B, C-1)
        if is_error_state(a0, b0, c0):
            pass
        elif is_corner_state(a0, b0, c0):
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        else:
            return (op0, a0, b0, c0)
    elif s0 == "AB":
        op0, a0, b0, c0 = ("B", A-1, B+1, C)
        if is_error_state(a0, b0, c0):
            pass
        elif is_corner_state(a0, b0, c0):
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        else:
            return (op0, a0, b0, c0)
        op0, a0, b0, c0 = ("A", A+1, B-1, C)
        if is_error_state(a0, b0, c0):
            pass
        elif is_corner_state(a0, b0, c0):
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        else:
            return (op0, a0, b0, c0)
    return ("X", -1, -1, -1)


def trial(N, A, B, C, S):
    
    if A + B + C == 0:
        return False, []
    
    ans, choices = True, []

    S = S + [S[-1]]  # S[-1] is terminal state

    if A + B + C == 1:
        for i in range(N):
            x, A, B, C = choose_at_corner_state(A, B, C, S[i])
            if x == "X":
                return False, []
            else:
                choices.append(x)
    else:
        for i in range(N):
            if is_corner_state(A, B, C):
                x, A, B, C = choose_at_corner_state(A, B, C, S[i])
            else:
                x, A, B, C = choose_at_normal_state(A, B, C, S[i], S[i+1])
            if x == "X":
                return False, []
            else:
                choices.append(x)
    return ans, choices


def solve(N, A, B, C, S):
    ans, choices = trial(N, A, B, C, S)
    if ans:
        print("Yes")
        for c in choices:
            print(c)
    else:
        print("No")


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("{}".format(outputs))

Submission status: Accepted

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

nums = {"A": a, "B": b, "C": c}

s = []
for i in range(n):
    line = input()
    s.append(line)

def add(a, b):
    nums[a] += 1
    nums[b] -= 1
    ans.append(a)

flag = True

ans = []

for i in range(n):
    a = s[i][0]
    b = s[i][1]

    if nums[a] == 0 and nums[b] == 0:
        print("No")
        flag = False
        break

    if nums[a] == 0:
        add(a, b)
        
    elif nums[b] == 0:
        add(b, a)
        
    else:
        if i != n - 1:
            na = s[i + 1][0]
            nb = s[i + 1][1]

            if (a == na or a == nb):
                add(a, b)

            else:
                add(b, a)

        else:
            add(a, b)
if flag:
    print("Yes")

    for i in ans:
        print(i)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys

YES = "Yes"  # type: str
NO = "No"  # type: str


class Impossible(Exception):
    pass

def f(q, qq, state):
    '''
    >>> f((0, 1), None, [2, 1, 0])
    1
    >>> f((0, 1), None, [1, 2, 0])
    0
    >>> f((0, 1), None, [0, 1, 0])
    0
    >>> f((1, 2), None, [0, 1, 0])
    2
    >>> f((0, 2), None, [1, 0, 0])
    2
    >>> f((0, 1), (0, 3), [1, 1, 0])
    0
    >>> f((0, 1), (0, 3), [1, 2, 0])
    0
    >>> f((0, 1), [], [1, 1, 0])
    1
    >>> f((0, 1), None, [0, 0, 0])
    -1
    >>> f((1, 2), None, [0, 0, 0])
    -1
    >>> f((0, 1), (0, 3), [1, 0, 0])
    1
    '''
    n, m = q
    if state[n] == state[m] == 0:
        return -1
    if state[n] == state[m] == 1:
        if n in qq:
            return n
        return m
    if state[n] > state[m]:
        return m
    return n
        

def solve(N: int, A: int, B: int, C: int, s: "List[str]"):
    D = {'AB': (0, 1), 'AC': (0, 2), 'BC': (1, 2)}
    E = 'ABC'
    Q = tuple(map(lambda x: D[x], s))
    state = [A, B, C]
    for q, qq in zip(Q, Q[1:] + (tuple(),)):
        r = f(q, qq, state)
        if r == -1:
            raise Impossible
        yield E[r]
        state[r] += 1
        state[q[q.index(r)^1]] -= 1


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(N)]  # type: "List[str]"
    try:
        ans = list(solve(N, A, B, C, s))
        print(YES)
        print(*ans, sep='\n')
    except Impossible:
        print(NO)

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    test()
    main()

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
if A + B + C == 0:
    print('No')
elif A + B + C == 1:
    res = []
    for _ in range(N):
        s = input()
        if s == 'AB':
            if A == 0 and B == 0:
                print('No')
                break
            elif A == 0:
                A += 1
                B -= 1
                res.append('A')
            else:
                A -= 1
                B += 1
                res.append('B')
        elif s == 'AC':
            if A == 0 and C == 0:
                print('No')
                break
            elif A == 0:
                A += 1
                C -= 1
                res.append('A')
            else:
                A -= 1
                C += 1
                res.append('C')
        else:
            if B == 0 and C == 0:
                print('No')
                break
            elif B == 0:
                B += 1
                C -= 1
                res.append('B')
            else:
                B -= 1
                C += 1
                res.append('C')
    else:
        print('Yes')
        print('\n'.join(res))
else:
    res = []
    s = input()
    if (s == 'AB' and A == 0 and B == 0) or (s == 'AC' and A == 0 and C == 0) or (s == 'BC' and B == 0 and C == 0):
        print('No')
    else:
        for _ in range(N - 1):
            next = input()
            if s == 'AB':
                if A == 0:
                    B -= 1
                    A += 1
                    res.append('A')
                elif B == 0:
                    B += 1
                    A -= 1
                    res.append('B')
                else:
                    if next == 'AB':
                        A += 1
                        B -= 1
                        res.append('A')
                    elif next == 'AC':
                        A += 1
                        B -= 1
                        res.append('A')
                    else:
                        A -= 1
                        B += 1
                        res.append('B')
            elif s == 'AC':
                if A == 0:
                    C -= 1
                    A += 1
                    res.append('A')
                elif C == 0:
                    C += 1
                    A -= 1
                    res.append('C')
                else:
                    if next == 'AC':
                        A += 1
                        C -= 1
                        res.append('A')
                    elif next == 'AB':
                        A += 1
                        C -= 1
                        res.append('A')
                    else:
                        A -= 1
                        C += 1
                        res.append('C')
            else:
                if C == 0:
                    B -= 1
                    C += 1
                    res.append('C')
                elif B == 0:
                    B += 1
                    C -= 1
                    res.append('B')
                else:
                    if next == 'CB':
                        C += 1
                        B -= 1
                        res.append('C')
                    elif next == 'AC':
                        C += 1
                        B -= 1
                        res.append('C')
                    else:
                        C -= 1
                        B += 1
                        res.append('B')
            s = next
        if s == 'AB':
            if A == 0:
                A += 1
                B -= 1
                res.append('A')
            else:
                A -= 1
                B += 1
                res.append('B')
        elif s == 'AC':
            if A == 0:
                A += 1
                C -= 1
                res.append('A')
            else:
                A -= 1
                C += 1
                res.append('C')
        else:
            if B == 0:
                B += 1
                C -= 1
                res.append('B')
            else:
                B -= 1
                C += 1
                res.append('C')
        print('Yes')
        print('\n'.join(res))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict

N,A,B,C=map(int, sys.stdin.readline().split())
S=[ sys.stdin.readline().strip() for _ in range(N) ]

L=[]    #i回目の選択で1を足す変数の名前を順番に格納

for i,s in enumerate(S): #0-indexed
    if s=="AB":
        if A<B:
            A+=1
            B-=1
            L.append("A")
        elif B<A:
            A-=1
            B+=1
            L.append("B")
        elif A==B==0:
            print "No"
            quit()
        elif A==B:
            if i<N-1: #next_sが取れるときは
                next_s=S[i+1]
                if next_s=="AB":    #次が2文字とも一致している場合は、どっちの文字に1を足しても良い
                    A+=1
                    B-=1
                    L.append("A")
                elif next_s=="AC":    #次に同じ文字が1文字出てくる場合は、その文字の方に+1
                    A+=1
                    B-=1
                    L.append("A")
                elif next_s=="BC":    #次に同じ文字が1文字出てくる場合は、その文字の方に+1
                    A-=1
                    B+=1
                    L.append("B")
            else: #最終行のときはどっちを取っても良い
                A+=1
                B-=1
                L.append("A")
    elif s=="AC":
        if A<C:
            A+=1
            C-=1
            L.append("A")
        elif C<A:
            A-=1
            C+=1
            L.append("C")
        elif A==C==0:
            print "No"
            quit()
        elif A==C:
            if i<N-1: #next_sが取れるときは
                next_s=S[i+1]
                if next_s=="AC":    #次が2文字とも一致している場合は、どっちの文字に1を足しても良い
                    A+=1
                    C-=1
                    L.append("A")
                elif next_s=="AB":    #次に同じ文字が1文字出てくる場合は、その文字の方に+1
                    A+=1
                    C-=1
                    L.append("A")
                elif next_s=="BC":    #次に同じ文字が1文字出てくる場合は、その文字の方に+1
                    A-=1
                    C+=1
                    L.append("C")
            else: #最終行のときはどっちを取っても良い
                A+=1
                C-=1
                L.append("A")
    elif s=="BC":
        if B<C:
            C-=1
            B+=1
            L.append("B")
        elif C<B:
            B-=1
            C+=1
            L.append("C")
        elif B==C==0:
            print "No"
            quit()
        elif B==C:
            if i<N-1: #next_sが取れるときは
                next_s=S[i+1]
                if next_s=="BC":    #次が2文字とも一致している場合は、どっちの文字に1を足しても良い
                    B+=1
                    C-=1
                    L.append("B")
                elif next_s=="AB":    #次に同じ文字が1文字出てくる場合は、その文字の方に+1
                    B+=1
                    C-=1
                    L.append("B")
                elif next_s=="AC":    #次に同じ文字が1文字出てくる場合は、その文字の方に+1
                    B-=1
                    C+=1
                    L.append("C")
            else: #最終行のときはどっちを取っても良い
                A+=1
                C-=1
                L.append("A")
print "Yes"
for x in L:
    print x

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict

N,A,B,C=map(int, sys.stdin.readline().split())
S=[ sys.stdin.readline().strip() for _ in range(N) ]

L=[]    #i回目の選択で1を足す変数の名前を順番に格納

for i,s in enumerate(S): #0-indexed
    if s=="AB":
        if A<B:
            A+=1
            B-=1
            L.append("A")
        elif B<A:
            A-=1
            B+=1
            L.append("B")
        elif A==B==0:
            print "No"
            quit()
        elif A==B:
            try: #next_sが取れるときは
                next_s=S[i+1]
                if next_s=="AB":    #次が2文字とも一致している場合は、どっちの文字に1を足しても良い
                    A+=1
                    B-=1
                    L.append("A")
                elif next_s=="AC":    #次に同じ文字が1文字出てくる場合は、その文字の方に+1
                    A+=1
                    B-=1
                    L.append("A")
                elif next_s=="BC":    #次に同じ文字が1文字出てくる場合は、その文字の方に+1
                    A-=1
                    B+=1
                    L.append("B")
            except: #最終行のときはどっちを取っても良い
                A+=1
                B-=1
                L.append("A")
    elif s=="AC":
        if A<C:
            A+=1
            C-=1
            L.append("A")
        elif C<A:
            A-=1
            C+=1
            L.append("C")
        elif A==C==0:
            print "No"
            quit()
        elif A==C:
            try: #next_sが取れるときは
                next_s=S[i+1]
                if next_s=="AC":    #次が2文字とも一致している場合は、どっちの文字に1を足しても良い
                    A+=1
                    C-=1
                    L.append("A")
                elif next_s=="AB":    #次に同じ文字が1文字出てくる場合は、その文字の方に+1
                    A+=1
                    C-=1
                    L.append("A")
                elif next_s=="BC":    #次に同じ文字が1文字出てくる場合は、その文字の方に+1
                    A-=1
                    C+=1
                    L.append("C")
            except: #最終行のときはどっちを取っても良い
                A+=1
                C-=1
                L.append("A")
    elif s=="BC":
        if B<C:
            C-=1
            B+=1
            L.append("B")
        elif C<B:
            B-=1
            C+=1
            L.append("C")
        elif B==C==0:
            print "No"
            quit()
        elif B==C:
            try: #next_sが取れるときは
                next_s=S[i+1]
                if next_s=="BC":    #次が2文字とも一致している場合は、どっちの文字に1を足しても良い
                    B+=1
                    C-=1
                    L.append("B")
                elif next_s=="AB":    #次に同じ文字が1文字出てくる場合は、その文字の方に+1
                    B+=1
                    C-=1
                    L.append("B")
                elif next_s=="AC":    #次に同じ文字が1文字出てくる場合は、その文字の方に+1
                    B-=1
                    C+=1
                    L.append("C")
            except: #最終行のときはどっちを取っても良い
                A+=1
                C-=1
                L.append("A")
print "Yes"
for x in L:
    print x

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, *abc = map(int, input().split())
s = [input().rstrip()for _ in range(n)]
cond = {'AB': (0, 1), 'AC': (0, 2), 'BC': (1, 2)}
legend = ('A', 'B', 'C')
ans = []


def dfs(i):
    if -1 in abc:
        return 0
    elif i == n:
        print('Yes')
        print(*ans, sep='\n')
        exit()
        # return 0
    else:
        idx1, idx2 = cond[s[i]]
        abc[idx1] += 1
        abc[idx2] -= 1
        ans.append(legend[idx1])
        dfs(i+1)
        ans.pop()
        abc[idx1] -= 1
        abc[idx2] += 1

        abc[idx1] -= 1
        abc[idx2] += 1
        ans.append(legend[idx2])
        dfs(i+1)
        ans.pop()
        abc[idx1] += 1
        abc[idx2] -= 1


dfs(0)
print('No')

Submission status: Accepted

Solution code:
# F - Three Variables Game

n, a, b, c = map(int, input().split())
s = [input() for i in range(n)]

def decode(c):
    return ord(c) - ord('A')

state = [a, b, c]
choice = []
for i in range(n):
    # 'AC' -> (0, 2)
    j, k = map(decode, s[i])

    # 失敗
    if state[j] == state[k] == 0:
        print('No')
        break

    # state[j] <= state[k] にする
    if state[j] > state[k]:
        j, k = k, j

    # 1手先読み
    if state[j] == state[k] == 1 and i < n - 1:
        xj, xk = map(decode, s[i + 1])
        if state[xj] + state[xk] == 1 and k in {xj, xk}:
            j, k = k, j

    # jを選択
    state[j] += 1
    state[k] -= 1
    choice.append('ABC'[j])
else:
    print('Yes')
    for c in choice:
        print(c)

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
if A + B + C == 0:
    print('No')
elif A + B + C == 1:
    res = []
    for _ in range(N):
        s = input()
        if s == 'AB':
            if A == 0 and B == 0:
                print('No')
                break
            elif A == 0:
                A += 1
                B -= 1
                res.append('A')
            else:
                A -= 1
                B += 1
                res.append('B')
        elif s == 'AC':
            if A == 0 and C == 0:
                print('No')
                break
            elif A == 0:
                A += 1
                C -= 1
                res.append('A')
            else:
                A -= 1
                C += 1
                res.append('C')
        else:
            if B == 0 and C == 0:
                print('No')
                break
            elif B == 0:
                B += 1
                C -= 1
                res.append('B')
            else:
                B -= 1
                C += 1
                res.append('C')
    else:
        print('Yes')
        print('\n'.join(res))
else:
    res = []
    s = input()
    if (s == 'AB' and A == 0 and B == 0) or (s == 'AC' and A == 0 and C == 0) or (s == 'BC' and B == 0 and C == 0):
        print('No')
    else:
        for _ in range(N - 1):
            next = input()
            if s == 'AB':
                if A == 0:
                    B -= 1
                    A += 1
                    res.append('A')
                elif B == 0:
                    B += 1
                    A -= 1
                    res.append('B')
                else:
                    if next == 'AB':
                        A += 1
                        B -= 1
                        res.append('A')
                    elif next == 'AC':
                        A += 1
                        B -= 1
                        res.append('A')
                    else:
                        A -= 1
                        B += 1
                        res.append('B')
            elif s == 'AC':
                if A == 0:
                    C -= 1
                    A += 1
                    res.append('A')
                elif C == 0:
                    C += 1
                    A -= 1
                    res.append('C')
                else:
                    if next == 'AC':
                        A += 1
                        C -= 1
                        res.append('A')
                    elif next == 'AB':
                        A += 1
                        C -= 1
                        res.append('A')
                    else:
                        A -= 1
                        C += 1
                        res.append('C')
            else:
                if C == 0:
                    B -= 1
                    C += 1
                    res.append('C')
                elif B == 0:
                    B += 1
                    C -= 1
                    res.append('B')
                else:
                    if next == 'CB':
                        C += 1
                        B -= 1
                        res.append('C')
                    elif next == 'AC':
                        C += 1
                        B -= 1
                        res.append('C')
                    else:
                        C -= 1
                        B += 1
                        res.append('B')
            s = next
        if s == 'AB':
            if A == 0:
                A += 1
                B -= 1
                res.append('A')
            else:
                A -= 1
                B += 1
                res.append('B')
        elif s == 'AC':
            if A == 0:
                A += 1
                C -= 1
                res.append('A')
            else:
                A -= 1
                C += 1
                res.append('C')
        else:
            if B == 0:
                B += 1
                C -= 1
                res.append('B')
            else:
                B -= 1
                C += 1
                res.append('C')
        print('Yes')
        print('\n'.join(res))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
abc = ["A","B","C"]

def calculate(x,y, q1,q2):
    flag = 0
    ans = ""
    if x == 1 and y == 1:
        if q1 == q2:
            y -= 1
            x += 1
            ans = abc[q1[0]]

        elif q2 != "":
            t = set(q1) & set(q2)
            s = set(q1) - t
            #print(q2)
            #print(int(list(t)[0]),int(list(s)[0]))
            if int(list(t)[0]) < int(list(s)[0]):
                y -= 1
                x += 1
                ans = abc[q1[0]]
            else:
                x -= 1
                y += 1
                ans = abc[q1[1]]
        else:
            y -= 1
            x += 1
            ans = abc[q1[0]]

    elif x <= y:
        y -= 1
        x += 1
        #print(y)
        if y < 0:
            print("No")
            flag = 1
            return(x, y, ans, flag)
        ans = abc[q1[0]]
    else:
        x -= 1
        y += 1
        ans = ""
        #print(x)
        if x < 0:
            print("No")
            flag = 1
            return(x, y, ans, flag)
        ans = abc[q1[1]]
    return (x, y, ans, flag)

def main():
    N, A, B, C  =  [int(i) for i in input().split()]
    s = []
    for i in range(N):
        t = input()
        if t == "AB":
            s.append([0,1])
        if t == "AC":
            s.append([0,2])
        if t == "BC":
            s.append([1,2])
    #print(s)
    answers  = []
    for i, q1 in enumerate(s):
        ans = ""
        f = 0
        q2 = ""
        if i+1 != N:
            q2 = s[i+1]

        if q1 == [0,1]:
            #print(1)
            A, B, ans, f = calculate(A,B,q1,q2)
        if q1 == [1,2]:
            #print(2)
            B, C, ans,f  = calculate(B,C,q1,q2)
        if q1 == [0,2]:
            #print(3)
            A, C, ans,f  = calculate(A,C,q1,q2)
        answers.append(ans)
        if f == 1:
            return
    print("Yes")

    for a in answers:
        print(a)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7
INF = 10 ** 11
import sys
sys.setrecursionlimit(100000000)

def main():
    N,A,B,C = map(int,input().split())
    S = [input() for _ in range(N)]

    ans = []
    for i in range(N):
        s = S[i]
        if s == 'AB':
            if A > 1 and B > 1:
                if A > B:
                    A -= 1
                    B += 1
                    ans.append('B')
                else:
                    B -= 1
                    A += 1
                    ans.append('A')
            elif A == 1 and B > 1:
                B -= 1
                A += 1
                ans.append('A')
            elif A > 1  and B == 1:
                A -= 1
                B += 1
                ans.append('B')
            elif A == 1 and B == 1:
                if i + 1 < N:
                    if S[i + 1] == 'AB':
                        A += 1
                        B -= 1
                        ans.append('A')
                    elif S[i + 1] == 'BC':
                        B += 1
                        A -= 1
                        ans.append('B')
                    else:
                        A += 1
                        B -= 1
                        ans.append('A')
                else:
                    ans.append('A')
            elif A == 0 and B == 0:
                print('No')
                return
            elif A == 0:
                A += 1
                B -= 1
                ans.append('A')
            elif B == 0:
                B += 1
                A -= 1
                ans.append('B')
        
        elif s == 'BC':
            if C > 1 and B > 1:
                if C > B:
                    C -= 1
                    B += 1
                    ans.append('B')
                else:
                    B -= 1
                    C += 1
                    ans.append('C')
            elif C == 1 and B > 1:
                B -= 1
                C += 1
                ans.append('C')
            elif C > 1  and B == 1:
                C -= 1
                B += 1
                ans.append('B')
            elif C == 1 and B == 1:
                if i + 1 < N:
                    if S[i + 1] == 'AB':
                        C -= 1
                        B += 1
                        ans.append('B')
                    elif S[i + 1] == 'BC':
                        B += 1
                        C -= 1
                        ans.append('B')
                    else:
                        C += 1
                        B -= 1
                        ans.append('C')
                else:
                    ans.append('B')
            elif C == 0 and B == 0:
                print('No')
                return
            elif C == 0:
                C += 1
                B -= 1
                ans.append('C')
            elif B == 0:
                B += 1
                C -= 1
                ans.append('B')
        
        elif s == 'AC':
            if A > 1 and C > 1:
                if A > C:
                    A -= 1
                    C += 1
                    ans.append('C')
                else:
                    C -= 1
                    A += 1
                    ans.append('A')
            elif A == 1 and C > 1:
                C -= 1
                A += 1
                ans.append('A')
            elif A > 1  and C == 1:
                A -= 1
                C += 1
                ans.append('C')
            elif A == 1 and C == 1:
                if i + 1 < N:
                    if S[i + 1] == 'AB':
                        A += 1
                        C -= 1
                        ans.append('A')
                    elif S[i + 1] == 'BC':
                        C += 1
                        A -= 1
                        ans.append('C')
                    else:
                        A += 1
                        C -= 1
                        ans.append('A')
                else:
                    ans.append('A')
            elif A == 0 and C == 0:
                print('No')
                return
            elif A == 0:
                A += 1
                C -= 1
                ans.append('A')
            elif C == 0:
                C += 1
                A -= 1
                ans.append('C')

    print('Yes')
    print(*ans,sep = '\n')
if __name__ == '__main__':
    main()