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

Solution (Python):
N, A, B, C = map(int, input().split())
nums = [A, B, C]
steps = [input() for i in range(N)]
chars = ["A", "B", "C"]
operations = []
can_select = True
for i in range(N):
    s = steps[i]
    left_idx = chars.index(s[0])
    right_idx = chars.index(s[1])
    if (nums[left_idx] == 0 and nums[right_idx] == 0) or nums[left_idx] < 0 or nums[right_idx] < 0:
        can_select = False
        break
    elif nums[left_idx] == 1 and nums[right_idx] == 1 and i != N-1:
        if chars[left_idx] in list(steps[i + 1]):
            nums[left_idx] += 1
            nums[right_idx] -= 1
            operations.append(chars[left_idx])
        else:
            nums[left_idx] -= 1
            nums[right_idx] += 1
            operations.append(chars[right_idx])
    else:
        if nums[left_idx] < nums[right_idx]:
            nums[left_idx] += 1
            nums[right_idx] -= 1
            operations.append(chars[left_idx])
        else:
            nums[left_idx] -= 1
            nums[right_idx] += 1
            operations.append(chars[right_idx])
if can_select:
    print("Yes")
    for i in operations:
        print(i)
else:
    print("No")

Solution (Python):
'''
自宅用PCでの解答
'''
import math

import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"

def main():
    n,a,b,c = map(int,ipt().split())
    s = a+b+c
    q = [input() for i in range(n)]
    ans = []

    for i,qi in enumerate(q):
        if qi == "AB":
            if a == b == 0:
                print("No")
                exit()
            elif a == 0:
                ans.append("A")
                a += 1
                b -= 1
            elif b == 0:
                ans.append("B")
                a -= 1
                b += 1
            elif i == n-1:
                ans.append("A")
            elif a == b == 1:
                if q[i+1] == "BC":
                    ans.append("B")
                    b += 1
                    a -= 1
                else:
                    ans.append("A")
                    b -= 1
                    a += 1
            else:
                if a > b:
                    ans.append("B")
                    b += 1
                    a -= 1
                else:
                    ans.append("A")
                    a += 1
                    b -= 1
        elif qi == "BC":
            if c == b == 0:
                print("No")
                exit()
            elif c == 0:
                ans.append("C")
                c += 1
                b -= 1
            elif b == 0:
                ans.append("B")
                c -= 1
                b += 1
            elif i == n-1:
                ans.append("C")
            elif c == b == 1:
                if q[i+1] == "AB":
                    ans.append("B")
                    b += 1
                    c -= 1
                else:
                    ans.append("C")
                    b -= 1
                    c += 1
            else:
                if c > b:
                    ans.append("B")
                    b += 1
                    c -= 1
                else:
                    ans.append("C")
                    c += 1
                    b -= 1
        else:
            if a == c == 0:
                print("No")
                exit()
            elif a == 0:
                ans.append("A")
                a += 1
                c -= 1
            elif c == 0:
                ans.append("C")
                a -= 1
                c += 1
            elif i == n-1:
                ans.append("A")
            elif a == c == 1:
                if q[i+1] == "BC":
                    ans.append("C")
                    c += 1
                    a -= 1
                else:
                    ans.append("A")
                    c -= 1
                    a += 1
            else:
                if a > c:
                    ans.append("C")
                    c += 1
                    a -= 1
                else:
                    ans.append("A")
                    a += 1
                    c -= 1

    print("Yes")
    for i in ans:
        print(i)
    return None

if __name__ == '__main__':
    main()

Solution (Python):
N, A, B, C = map(int, input().split())
dic = {'A':A,'B':B,'C':C}
S = [input() for _ in range(N)]

def solve(N,dic,S):
    ans = ['Yes']
    for i in range(N):
        if dic[S[i][0]]>dic[S[i][1]]:
            dic[S[i][0]]-=1
            dic[S[i][1]]+=1
            ans.append(S[i][1])
        elif dic[S[i][0]]<dic[S[i][1]] or dic[S[i][0]]>1:
            dic[S[i][1]]-=1
            dic[S[i][0]]+=1
            ans.append(S[i][0])
        elif dic[S[i][0]]==0:
            return ['No']
        elif i==N-1:
            dic[S[i][0]]+=1
            dic[S[i][1]]-=1
            ans.append(S[i][0])
        else:
            if S[i][0] in S[i+1]:
                dic[S[i][0]]+=1
                dic[S[i][1]]-=1
                ans.append(S[i][0])
            else:
                dic[S[i][1]]+=1
                dic[S[i][0]]-=1
                ans.append(S[i][1])
    return ans
print(*solve(N,dic,S),sep='\n')

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

if a+b+c == 0:
    print("No")
    exit()

move = []
if a+b+c == 1:
    for i in range(n):
        if s_list[i] == "AB":
            if a == 0:
                a+=1
                b-=1
                move.append("A")
            else:
                b+=1
                a-=1
                move.append("B")
        if s_list[i] == "BC":
            if b == 0:
                b+=1
                c-=1
                move.append("B")
            else:
                c+=1
                b-=1
                move.append("C")
        if s_list[i] == "AC":
            if a == 0:
                a+=1
                c-=1
                move.append("A")
            else:
                c+=1
                a-=1
                move.append("C")
        if min(a,b,c) < 0:
            print("No")
            exit()

    print("Yes")
    for i in move:
        print(i)            
    
else:
    for i in range(n):
        if i < n-1:
            s_next = s_list[i+1]
        if s_list[i] == "AB":
            if a == 1 and b == 1 and s_next != "AB":
                if s_next == "BC":
                    b+=1
                    a-=1
                    move.append("B")
                else:
                    a+=1
                    b-=1
                    move.append("A")
            else:
                if a < b:
                    a+=1
                    b-=1
                    move.append("A")
                else:
                    b+=1
                    a-=1
                    move.append("B")
        if s_list[i] == "BC":
            if b == 1 and c == 1 and s_next != "BC":
                if s_next == "AB":
                    b+=1
                    c-=1
                    move.append("B")
                else:
                    c+=1
                    b-=1
                    move.append("C")
            else:
                if b < c:
                    b+=1
                    c-=1
                    move.append("B")
                else:
                    c+=1
                    b-=1
                    move.append("C")
        if s_list[i] == "AC":
            if a == 1 and c == 1 and s_next != "AC":
                if s_next == "AB":
                    a+=1
                    c-=1
                    move.append("A")
                else:
                    c+=1
                    a-=1
                    move.append("C")
            else:
                if a < c:
                    a+=1
                    c-=1
                    move.append("A")
                else:
                    c+=1
                    a-=1
                    move.append("C") 

        if min(a,b,c) < 0:
            print("No")
            exit()
    print("Yes")
    for i in move:
        print(i)

Solution (Python):
d = {}
N, d['A'], d['B'], d['C'] = map(int, input().split())
S = [input() for _ in range(N)]

if sum(d.values()) == 0:
    print('No')
elif sum(d.values()) == 1:
    ans = []
    for s in S:
        if d[s[0]] == d[s[1]] == 0:
            print('No')
            exit()
        elif d[s[0]] > d[s[1]]:
            d[s[0]] -= 1
            d[s[1]] += 1
            ans.append(s[1])
        else:
            d[s[0]] += 1
            d[s[1]] -= 1
            ans.append(s[0])
    print('Yes')
    print(*ans, sep='\n')
else:
    ans = []
    for i, s in enumerate(S):
        if d[s[0]] == d[s[1]] == 0:
            print('No')
            exit()
        elif d[s[0]] > d[s[1]]:
            d[s[0]] -= 1
            d[s[1]] += 1
            ans.append(s[1])
        elif d[s[0]] < d[s[1]]:
            d[s[0]] += 1
            d[s[1]] -= 1
            ans.append(s[0])
        elif d[s[0]] == d[s[1]] == 1 and i != N-1 and S[i] != S[i+1]:
            if s[0] in S[i+1]:
                d[s[0]] += 1
                d[s[1]] -= 1
                ans.append(s[0])
            else:
                d[s[0]] -= 1
                d[s[1]] += 1
                ans.append(s[1])
        else:
            d[s[0]] -= 1
            d[s[1]] += 1
            ans.append(s[1])
    print('Yes')
    print(*ans, sep='\n')

Solution (Python):
d = {}
N, d['A'], d['B'], d['C'] = map(int, input().split())
S = [input() for _ in range(N)]

if sum(d.values()) == 0:
    print('No')
elif sum(d.values()) == 1:
    ans = []
    for s in S:
        if d[s[0]] == d[s[1]] == 0:
            print('No')
            exit()
        elif d[s[0]] > d[s[1]]:
            d[s[0]] -= 1
            d[s[1]] += 1
            ans.append(s[1])
        else:
            d[s[0]] += 1
            d[s[1]] -= 1
            ans.append(s[0])
    print('Yes')
    print(*ans, sep='\n')
else:
    ans = []
    for i, s in enumerate(S):
        if d[s[0]] == d[s[1]] == 0:
            print('No')
            exit()
        elif d[s[0]] > d[s[1]]:
            d[s[0]] -= 1
            d[s[1]] += 1
            ans.append(s[1])
        elif d[s[0]] < d[s[1]]:
            d[s[0]] += 1
            d[s[1]] -= 1
            ans.append(s[0])
        elif d[s[0]] == d[s[1]] == 1 and i != N-1 and S[i] != S[i+1]:
            
            if s[0] in S[i+1]:
                d[s[0]] += 1
                d[s[1]] -= 1
                ans.append(s[0])
            else:
                d[s[0]] -= 1
                d[s[1]] += 1
                ans.append(s[1])
        else:
            d[s[0]] -= 1
            d[s[1]] += 1
            ans.append(s[1])
    print('Yes')
    print(*ans, sep='\n')

Solution (Python):

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

x = [A, B, C]
d = {"A": 0, "B": 1, "C": 2}
if sum(x) == 0:
    print("No")
else:
    flag = True
    ans = []
    for i, s in enumerate(S):
        n, m = list(s)

        if x[d[n]] == x[d[m]] == 0:
            flag = False
            break

        if x[d[n]] > x[d[m]]:
            x[d[n]] -= 1
            x[d[m]] += 1
            ans.append(m)
        elif x[d[n]] < x[d[m]]:
            x[d[n]] += 1
            x[d[m]] -= 1
            ans.append(n)
        elif i < N - 1 and S[i] != S[i + 1]:
            
            if n in S[i + 1]:
                x[d[n]] += 1
                x[d[m]] -= 1
                ans.append(n)
            else:
                x[d[m]] += 1
                x[d[n]] -= 1
                ans.append(m)
        else:
            
            x[d[n]] += 1
            x[d[m]] -= 1
            ans.append(n)

    if flag:
        print("Yes")
        print(*ans, sep="\n")
    else:
        print("No")

Solution (Python):
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()

    def stoidx(s):
        if s == 'AB':
            return (0, 1)
        elif s == 'BC':
            return (1, 2)
        else:
            return (0, 2)


    def answer(ans):
        print('Yes')
        for a in ans:
            print(a)

    def add_answer(x):
        if s[x[0]] > s[x[1]]:
            s[x[0]] -= 1
            s[x[1]] += 1
            ans.append(abc[x[1]])
        else:
            s[x[0]] += 1
            s[x[1]] -= 1
            ans.append(abc[x[0]])
        
    n, *s = map(int, input().split())
    sl = []
    for _ in range(n):
        sl.append(stoidx(input()))
    abc = 'ABC'
    ans = []
    if sum(s) == 0:
        print('No')
        return

    elif sum(s) == 1:
        for x in sl:
            if s[x[0]] == 0 and s[x[1]] == 0:
                print('No')
                return
            add_answer(x)

    else:
        for i, x in enumerate(sl):
            if s[x[0]] == 0 and s[x[1]] == 0:
                print('No')
                return
                
            if i < n-1 and s[x[0]] == 1 and s[x[1]] == 1:
                nx = sl[i+1]
                if x != nx and sum(s)==2:
                    if x[0] in nx:
                        s[x[0]] += 1
                        s[x[1]] -= 1
                        ans.append(abc[x[0]])
                    else:
                        s[x[0]] -= 1
                        s[x[1]] += 1
                        ans.append(abc[x[1]])
                    continue
            add_answer(x)
    answer(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n,a,b,c=map(int,input().split())
if a+b+c==0:
    print("No")
elif a+b+c==1:
    d=[]
    for i in range(n):
        s=input()
        if s=="AB":
            if a+b==0:
                print("No")
                exit()
            else:
                if a==0:
                    d.append("A")
                else:
                    d.append("B")
                a,b=b,a
        if s=="BC":
            if c+b==0:
                print("No")
                exit()
            else:
                if b==0:
                    d.append("B")
                else:
                    d.append("C")
                c,b=b,c
        if s=="AC":
            if a+c==0:
                print("No")
                exit()
            else:
                if a==0:
                    d.append("A")
                else:
                    d.append("C")
                a,c=c,a 
    print("Yes")
    for i in d:
        print(i)
else:
    d=[]
    s=[input()for i in range(n)]+["AB"]
    for i in range(n):
        if s[i]=="AB":
            if a+b==0:
                print("No")
                exit()
            if a+b+c==2 and a==1 and b==1 and s[i+1]!=s[i]:
                if s[i+1]=="AC":
                    d.append("A")
                    a+=1
                    b-=1
                else:
                    d.append("B")
                    b+=1
                    a-=1
            elif a<b:
                d.append("A")
                a+=1
                b-=1
            else:
                d.append("B")
                b+=1
                a-=1
        elif s[i]=="BC":
            if c+b==0:
                print("No")
                exit()
            if a+b+c==2 and c==1 and b==1 and s[i+1]!=s[i]:
                if s[i+1]=="AB":
                    d.append("B")
                    c-=1
                    b+=1
                else:
                    d.append("C")
                    c+=1
                    b-=1
            elif b<c:
                d.append("B")
                b+=1
                c-=1
            else:
                d.append("C")
                c+=1
                b-=1
        elif s[i]=="AC":
            if c+a==0:
                print("No")
                exit()
            if a+b+c==2 and c==1and a==1 and s[i+1]!=s[i]:
                if s[i+1]=="AB":
                    d.append("A")
                    a+=1
                    c-=1
                else:
                    d.append("C")
                    c+=1
                    a-=1
            elif a<c:
                d.append("A")
                a+=1
                c-=1
            else:
                d.append("C")
                c+=1
                a-=1
    print("Yes")
    for i in d:
        print(i)

Solution (Python):
N, A, B, C = map(int, input().split())
dt = {'AB': (0, 1), 'AC': (0, 2), 'BC': (1, 2)}
Q = [dt[input()] for _ in range(N)]


class Game:
    def __init__(self, ABC):
        self.ABC = ABC
        self.ds = {0: 'A', 1: 'B', 2: 'C'}
        self.ans = []

    def add_q0(self, q0, q1):
        self.ans.append(self.ds[q0])
        self.ABC[q0] += 1
        self.ABC[q1] -= 1

    def add_q1(self, q0, q1):
        self.ans.append(self.ds[q1])
        self.ABC[q0] -= 1
        self.ABC[q1] += 1


def solve1(ABC):
    G = Game(ABC)
    for q0, q1 in Q:
        if G.ABC[q0] == G.ABC[q1] == 0:
            print('No')
            exit()
        elif G.ABC[q0] == 0:
            G.add_q0(q0, q1)
        else:
            G.add_q1(q0, q1)
    print('Yes')
    print(*G.ans, sep='\n')


def solve2(ABC):
    G = Game(ABC)
    for i, (q0, q1) in enumerate(Q):
        if G.ABC[q0] == G.ABC[q1] == 0:
            print('No')
            exit()
        elif G.ABC[q0] == 0:
            G.add_q0(q0, q1)
        elif G.ABC[q1] == 0:
            G.add_q1(q0, q1)
        elif G.ABC[q0] == G.ABC[q1] == 1 and sum(ABC) == 2 and i < N - 1:
            if q0 in Q[i + 1]:
                G.add_q0(q0, q1)
            else:
                G.add_q1(q0, q1)
        else:
            
            if G.ABC[q0] <= G.ABC[q1]:
                G.add_q0(q0, q1)
            else:
                G.add_q1(q0, q1)
    print('Yes')
    print(*G.ans, sep='\n')


S = A + B + C
if S == 0:
    print('No')
elif S == 1:
    solve1([A, B, C])
else:
    solve2([A, B, C])

Solution (Python):
N, A, B, C = map(int, input().split())
dt = {'AB': (0, 1), 'AC': (0, 2), 'BC': (1, 2)}
Q = [dt[input()] for _ in range(N)]


class Game:
    def __init__(self, ABC):
        self.ABC = ABC
        self.ds = {0: 'A', 1: 'B', 2: 'C'}
        self.ans = []

    def add_q0(self, q0, q1):
        self.ans.append(self.ds[q0])
        self.ABC[q0] += 1
        self.ABC[q1] -= 1

    def add_q1(self, q0, q1):
        self.ans.append(self.ds[q1])
        self.ABC[q0] -= 1
        self.ABC[q1] += 1


def solve1(ABC):
    G = Game(ABC)
    for q0, q1 in Q:
        if G.ABC[q0] == G.ABC[q1] == 0:
            print('No')
            exit()
        elif G.ABC[q0] == 0:
            G.add_q0(q0, q1)
        else:
            G.add_q1(q0, q1)
    print('Yes')
    print(*G.ans, sep='\n')


def solve2(ABC):
    G = Game(ABC)
    for i, (q0, q1) in enumerate(Q):
        if G.ABC[q0] == G.ABC[q1] == 0:
            print('No')
            exit()
        elif G.ABC[q0] == 0:
            G.add_q0(q0, q1)
        elif G.ABC[q1] == 0:
            G.add_q1(q0, q1)
        elif G.ABC[q0] == G.ABC[q1] == 1 and i < N - 1:
            if q0 in Q[i + 1]:
                G.add_q0(q0, q1)
            else:
                G.add_q1(q0, q1)
        else:
            
            if G.ABC[q0] <= G.ABC[q1]:
                G.add_q0(q0, q1)
            else:
                G.add_q1(q0, q1)
    print('Yes')
    print(*G.ans, sep='\n')


def solve3(ABC):
    G = Game(ABC)
    for q0, q1 in Q:
        if G.ABC[q0] == G.ABC[q1] == 0:
            print('No')
            exit()
        elif G.ABC[q0] == 0:
            G.add_q0(q0, q1)
        elif G.ABC[q1] == 0:
            G.add_q1(q0, q1)
        else:
            
            if G.ABC[q0] <= G.ABC[q1]:
                G.add_q0(q0, q1)
            else:
                G.add_q1(q0, q1)
    print('Yes')
    print(*G.ans, sep='\n')


S = A + B + C
if S == 0:
    print('No')
elif S == 1:
    solve1([A, B, C])
elif S == 2:
    solve2([A, B, C])
else:
    solve3([A, B, C])

Solution (Python):
N,*X=map(int,input().split())
S=[input() for i in range(N)]
f=1
d={'AB':(0,1),'AC':(0,2),'BC':(1,2)}
x='ABC'
a=[]
for i in range(N):
    if X[d[S[i]][0]]==1 and X[d[S[i]][1]]==1 and X.count(0)==1 and i<N-1:
        if S[i][0] in S[i+1]:
            X[d[S[i]][0]]+=1
            X[d[S[i]][1]]-=1
            a.append(x[d[S[i]][0]])
        else:
            X[d[S[i]][0]]-=1
            X[d[S[i]][1]]+=1
            a.append(x[d[S[i]][1]])
    elif X[d[S[i]][0]]<X[d[S[i]][1]]:
        X[d[S[i]][0]]+=1
        X[d[S[i]][1]]-=1
        a.append(x[d[S[i]][0]])
    else:
        X[d[S[i]][0]]-=1
        X[d[S[i]][1]]+=1
        a.append(x[d[S[i]][1]])
    if -1 in X:
        f=0;break
print(['No','Yes'][f])
if f:print(*a,sep='\n')

Solution (Python):

n,a,b,c = list(map(int, input().split()))
s = [[0]*2 for _ in range(n)]
S = []
for i in range(n):
  X = input()
  S += X
  cnt = 0
  for x in X:
    if x == 'A':
      s[i][cnt] = 0
    elif x == 'B':
      s[i][cnt] = 1
    else:
      s[i][cnt] = 2
    cnt += 1

if (a + b + c) == 0:exit(print('No'))
abc = [a,b,c]
ABC = ['A','B','C']


ans = [0] * n

def zero(xy):
  x = xy[0]
  y = xy[1]
  if abc[x] == 0 and abc[y] == 0:
    return -1,-1
  elif abc[x] == 0:
    return x,y
  elif abc[y] == 0:
    return y,x
  else:
    return 3,3

def main():
  global ans
  global abc
  
  for i in range(n):
    x,y = zero(s[i])
    if x == -1:exit(print('No'))
    if x == 3:
      if i < n-1:
        if s[i][1] in s[i+1]:
          x = s[i][1]
          y = s[i][0]
        else:
          x = s[i][0]
          y = s[i][1]
      else:
        x = s[i][0]
        y = s[i][1]
    ans[i] = x
    abc[x] += 1
    abc[y] -= 1

main()
print('Yes')
for i in range(n):
  print(ABC[ans[i]])

Solution (Python):

import sys 
from collections import Counter
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
Q, A, B, C = map(int, readline().split())
N = A + B + C
SS = []
for _ in range(Q):
    S = readline().decode().rstrip()
    a = 0 if S == 'AB' else 1 if S == 'AC' else 2 
    SS.append(a)
ans = [] 
if N == 0:
    print('No')
    sys.exit()
elif N == 1:
    nums = [A,B,C]
    for n in range(Q):
        num = SS[n]
        i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2)  
        if nums[i] == nums[j]:
            print('No')
            sys.exit()
        if nums[i] > nums[j]:
            i,j = j,i
        nums[i] += 1
        nums[j] -= 1
        ans.append(i)
else:
    nums = [A,B,C]
    num = SS[0]
    i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2) 
    if nums[i] == 0 and nums[j] == 0:
        print('No')
        sys.exit()
    if N == 2:
        for n in range(Q):
            num = SS[n]
            i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2)
            if nums[i] == 1 and nums[j] == 1 and n < Q-1:
                if SS[n] != SS[n+1]:
                    ni,nj = (0,1) if SS[n+1] == 0 else (0,2) if SS[n+1] == 1 else (1,2)
                    a = Counter([i,j,ni,nj])
                    b = a.most_common()[0][0] 
                    if j == b:
                        i,j = j,i   
            if nums[i] > nums[j]:
                i,j = j,i
            nums[i] += 1
            nums[j] -= 1
            ans.append(i)
    else:
        for n in range(Q):
            num = SS[n]
            i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2)
            if nums[i] > nums[j]:
                i,j = j,i
            nums[i] += 1
            nums[j] -= 1
            ans.append(i)
        
print('Yes')
ABC = 'ABC'
answer = list(ABC[i] for i in ans)
print('\n'.join(answer))

Solution (Python):
N,A,B,C = map(int,input().split())
s = [input() for i in range(N)]

if A+B+C == 0:
    print('No')
    exit()

D = []

if A+B+C == 1:
    for i in range(N):
        if (A == 1 and s[i] == 'BC') or (B == 1 and s[i] == 'AC') or (C == 1 and s[i] == 'AB'):
            print('No')
            exit()
        else:
            if A == 1:
                if s[i] == 'AB':
                    A = 0
                    B = 1
                    D.append('B')
                else:
                    A = 0
                    C = 1
                    D.append('C')
            elif B == 1:
                if s[i] == 'AB':
                    B = 0
                    A = 1
                    D.append('A')
                else:
                    B = 0
                    C = 1
                    D.append('C')
            else:
                if s[i] == 'BC':
                    C = 0
                    B = 1
                    D.append('B')
                else:
                    C = 0
                    A = 1
                    D.append('A')
    print('Yes')
    print(*D,sep='\n')
    exit()

if (B == 0 and C == 0 and s[0] == 'BC') or (A == 0 and C == 0 and s[0] == 'AC') or (A == 0 and B == 0 and s[0] == 'AB'):
    print('No')
    exit()

print('Yes')

for i in range(N):
    if A+B+C > 2:
        if A == 0 and s[i] == 'AB':
            D.append('A')
            A = 1
            B -= 1
        elif A == 0 and s[i] == 'AC':
            D.append('A')
            A = 1
            C -= 1
        elif B == 0 and s[i] == 'AB':
            D.append('B')
            B = 1
            A -= 1
        elif B == 0 and s[i] == 'BC':
            D.append('B')
            B = 1
            C -= 1
        elif C == 0 and s[i] == 'AC':
            D.append('C')
            C = 1
            A -= 1
        elif C == 0 and s[i] == 'BC':
            D.append('C')
            C = 1
            B -= 1
        elif A == 0 and B == 1 and s[i] == 'BC':
            D.append('B')
            B += 1
            C -= 1
        elif A == 0 and C == 1 and s[i] == 'BC':
            D.append('C')
            C += 1
            B -= 1
        elif B == 0 and A == 1 and s[i] == 'AC':
            D.append('A')
            A += 1
            C -= 1
        elif B == 0 and C == 1 and s[i] == 'AC':
            D.append('C')
            C += 1
            A -= 1
        elif C == 0 and A == 1 and s[i] == 'AB':
            D.append('A')
            A += 1
            B -= 1
        elif C == 0 and B == 1 and s[i] == 'AB':
            D.append('B')
            B += 1
            A -= 1
        elif s[i] == 'AB':
            D.append('A')
            A += 1
            B -= 1
        elif s[i] == 'AC':
            D.append('A')
            A += 1
            C -= 1
        elif s[i] == 'BC':
            D.append('B')
            B += 1
            C -= 1
    else:  
        if A == 1 and B == 1 and s[i] == 'AB':
            if i <= N-2 and s[i+1] == 'BC':
                D.append('B')
                A -= 1
                B += 1
            else:
                D.append('A')
                A += 1
                B -= 1
        elif A == 1 and C == 1 and s[i] == 'AC':
            if i <= N-2 and s[i+1] == 'AB':
                D.append('A')
                A += 1
                C -= 1
            else:
                D.append('C')
                A -= 1
                C += 1
        elif B == 1 and C == 1 and s[i] == 'BC':
            if i <= N-2 and s[i+1] == 'AB':
                D.append('B')
                B += 1
                C -= 1
            else:
                D.append('C')
                B -= 1
                C += 1
        elif A == 0 and s[i] == 'AB':
            D.append('A')
            A = 1
            B -= 1
        elif A == 0 and s[i] == 'AC':
            D.append('A')
            A = 1
            C -= 1
        elif B == 0 and s[i] == 'AB':
            D.append('B')
            B = 1
            A -= 1
        elif B == 0 and s[i] == 'BC':
            D.append('B')
            B = 1
            C -= 1
        elif C == 0 and s[i] == 'AC':
            D.append('C')
            C = 1
            A -= 1
        elif C == 0 and s[i] == 'BC':
            D.append('C')
            C = 1
            B -= 1
        elif s[i] == 'AB':
            D.append('A')
            A += 1
            B -= 1
        elif s[i] == 'AC':
            D.append('A')
            A += 1
            C -= 1
        elif s[i] == 'BC':
            D.append('B')
            B += 1
            C -= 1
print(*D,sep='\n')

Solution (Python):
def main():
    N, A, B, C = list(map(int, input().split(' ')))
    S = [input() for _ in range(N)]
    D = {
        'AB': A + B,
        'BC': B + C,
        'AC': A + C
    }
    ans = list()
    for i, s in enumerate(S):
        if D[s] == 0:
            print('No')
            return
        if s == 'AB':
            if (D['BC'] < D['AC']) or \
                (i < N - 1 and S[i + 1] == 'BC' and D['BC'] == D['AC'] == 1):
                ans.append('B')
                D['BC'] += 1
                D['AC'] -= 1
            else:
                ans.append('A')
                D['BC'] -= 1
                D['AC'] += 1
        elif s == 'BC':
            if (D['AB'] < D['AC']) or \
                (i < N - 1 and S[i + 1] == 'AB' and D['AB'] == D['AC'] == 1):
                ans.append('B')
                D['AB'] += 1
                D['AC'] -= 1
            else:
                ans.append('C')
                D['AB'] -= 1
                D['AC'] += 1
        else:  
            if (D['AB'] < D['BC']) or \
                (i < N - 1 and S[i + 1] == 'AB' and D['AB'] == D['BC'] == 1):
                ans.append('A')
                D['AB'] += 1
                D['BC'] -= 1
            else:
                ans.append('C')
                D['AB'] -= 1
                D['BC'] += 1
    print('Yes')
    for a in ans:
        print(a)

if __name__ == '__main__':
    main()