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())
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):

import sys
sys.setrecursionlimit(10**8)
INF = float("inf")


def yes():
    print("Yes")  


def no():
    print("No")  


def solve(N: int, A: int, B: int, C: int, s: "List[str]"):
    ABC = [A, B, C]
    ans = []
    d = {"AB": (0, 1), "BC": (1, 2), "AC": (0, 2)}
    
    for i in range(N):
        j, k = d[s[i]]
        if ABC[j] == ABC[k] == 0:
            no()
            return
        if ABC[j] == ABC[k] == 1:
            if i != N-1 and (j not in d[s[i+1]]):
                j, k = k, j
            ABC[j] += 1
            ABC[k] -= 1
            ans.append("ABC"[j])
        elif ABC[j] > ABC[k]:
            ABC[j] -= 1
            ABC[k] += 1
            ans.append("ABC"[k])
        else:
            ABC[j] += 1
            ABC[k] -= 1
            ans.append("ABC"[j])

    yes()
    for a in ans:
        print(a)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = int(next(tokens))  
    B = int(next(tokens))  
    C = int(next(tokens))  
    s = [next(tokens) for _ in range(N)]  
    solve(N, A, B, C, s)


if __name__ == '__main__':
    main()

Solution (Python):
import sys,math,copy,queue,itertools,bisect,collections
from heapq import *
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
INF = 10**18
MOD = 10**9 + 7
DD = ((1,0),(-1,0),(0,1),(0,-1))

n,a,b,c = LI()
z = [a,b,c]
s = [sys.stdin.readline().rstrip() for _ in range(n)]
ans = []
for i in range(n):
    x = s[i]
    p = -1
    if x == 'AB': j=0;k=1;l=2
    elif x == 'AC': j=0;k=2;l=1
    elif x == 'BC': j=1;k=2;l=0
    if z[j] > z[k]:
        z[j] -= 1; z[k] += 1
        p = k
    else:
        z[j] += 1; z[k] -= 1
        p = j
    if (z[j] == 0 or z[k] == 0) and z[l] == 0:
        if i < n-1:
            y = s[i+1]
            if y == 'AB': j1=0;k1=1;l1=2
            elif y == 'AC': j1=0;k1=2;l1=1
            elif y == 'BC': j1=1;k1=2;l1=0
            if z[j1] == 0 and z[k1] == 0:
                if z[j] == 0:
                    z[j] += 2; z[k] -= 2
                    p = j
                else:
                    z[j] -= 2; z[k] += 2
                    p = k
    if min(z) < 0:
        print('No')
        exit(0)
    if p==0:ans.append('A')
    elif p==1:ans.append('B')
    else: ans.append('C')
print('Yes')
print(*ans,sep='\n')

Solution (Python):
n,a,b,c = map(int, input().split())
a = min(a, 4)
b = min(b, 4)
c = min(c, 4)

s = [input() for i in range(n)]

DP = [set() for i in range(n+1)]
DP[0].add((a,b,c))
for i in range(n):
    for x,y,z in DP[i]:
        if s[i] == "AB":
            nz = z

            nx = x+1
            ny = y-1
            if ny >= 0:
                DP[i+1].add((nx,ny,nz))
            nx = x-1
            ny = y+1
            if nx >= 0:
                DP[i+1].add((nx,ny,nz))
        elif s[i] == "BC":
            nx = x

            ny = y+1
            nz = z-1
            if nz >= 0:
                DP[i+1].add((nx,ny,nz))
            ny = y-1
            nz = z+1
            if ny >= 0:
                DP[i+1].add((nx,ny,nz))
        else:
            ny = y

            nz = z+1
            nx = x-1
            if nx>=0:
                DP[i+1].add((nx,ny,nz))
            nz = z-1
            nx = x+1
            if nz >=0:
                DP[i+1].add((nx,ny,nz))

if len(DP[-1]) == 0:
    print("No")
    exit()
ans = []
tmp = list(DP[-1])[0]
for i in range(n):
    x,y,z = tmp
    j = n-1-i
    if s[j] == "AB":
        nz = z

        nx = x+1
        ny = y-1
        if (nx,ny,nz) in DP[j]:
            tmp = (nx,ny,nz)
            ans.append("B")
            continue
        nx = x-1
        ny = y+1
        if (nx,ny,nz) in DP[j]:
            tmp = (nx,ny,nz)
            ans.append("A")
            continue
    elif s[j] == "BC":
        nx = x

        ny = y+1
        nz = z-1
        if (nx,ny,nz) in DP[j]:
            tmp = (nx,ny,nz)
            ans.append("C")
            continue
        ny = y-1
        nz = z+1
        if (nx,ny,nz) in DP[j]:
            tmp = (nx,ny,nz)
            ans.append("B")
            continue
    else:
        ny = y

        nz = z+1
        nx = x-1
        if (nx,ny,nz) in DP[j]:
            tmp = (nx,ny,nz)
            ans.append("A")
            continue
        nz = z-1
        nx = x+1
        if (nx,ny,nz) in DP[j]:
            tmp = (nx,ny,nz)
            ans.append("C")
            continue

ans = ans[::-1]
print("Yes")
print(*ans, sep="\n")

Solution (Python):
import sys

n,a,b,c = map(int,input().split())
d = {'A':a,'B':b,'C':c}

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

ans = []
for i in range(n):

	if d[s[i][0]] <= 0 and d[s[i][1]] <= 0:
		print('No')
		sys.exit()
	if d[s[i][0]] < d[s[i][1]]:
		d[s[i][1]] -= 1
		d[s[i][0]] += 1
		ans.append(s[i][0])
	elif d[s[i][0]] > d[s[i][1]]:
		d[s[i][0]] -= 1
		d[s[i][1]] += 1
		ans.append(s[i][1])
	else:
		if i < n-1 and s[i+1].strip(s[i]) != '' and d[s[i+1].strip(s[i])] == 0:
			string = s[i+1].strip(s[i+1].strip(s[i]))
			d[string] += 1
			d[s[i].strip(string)] -= 1
			ans.append(s[i+1].strip(s[i+1].strip(s[i])))
		else:
			d[s[i][0]] += 1
			d[s[i][1]] -= 1
			ans.append(s[i][0])
print('Yes')
for i in range(len(ans)):
	print(ans[i])

Solution (Python):
import sys, copy, math, heapq, bisect
from itertools import accumulate
from collections import deque, defaultdict, Counter
sys.setrecursionlimit(500000)


N,A,B,C = map(int,input().split())
ABC = [A,B,C]
S = []
SS = ('A','B','C')
d = [0,0,0]
flag = True
for i in range(N):
    s = input()
    if s=="AB":
        S.append(2)
        d[0] += 1
        d[1] += 1
    elif s=="BC":
        S.append(0)
        d[1] += 1
        d[2] += 1
    else:
        S.append(1)
        d[2] += 1
        d[0] += 1
ans = []
for i in range(N):
    x,y = (S[i]+1)%3,(S[i]+2)%3
    if ABC[x]<ABC[y]:
        ans.append(SS[x])
        ABC[y] -= 1
        ABC[x] += 1
    elif ABC[x]>ABC[y]:
        ans.append(SS[y])
        ABC[y] += 1
        ABC[x] -= 1
    else:
        if i < N-1:
            if S[i+1]==y:
                ans.append(SS[x])
                ABC[y] -= 1
                ABC[x] += 1
            else:
                ans.append(SS[y])
                ABC[y] += 1
                ABC[x] -= 1
        else:
            ans.append(SS[y])
            ABC[y] += 1
            ABC[x] -= 1




    if ABC[0]<0 or ABC[1]<0 or ABC[2]<0:
        flag = False
        break


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

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)

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

ans = []
def dfs(i,a,b,c):
  if i == n:
    print('Yes')
    for ai in ans:
      print(ai)
    exit()
  si = s[i]
  if si == 'AB':
    if a == 0 and b == 0:
      return
    elif a == 0 and b > 0:
      ans.append('A')
      dfs(i+1,a+1,b-1,c)
    elif a > 0 and b == 0:
      ans.append('B')
      dfs(i+1,a-1,b+1,c)
    else:
      ans.append('A')
      dfs(i+1,a+1,b-1,c)
      ans.pop()
      ans.append('B')
      dfs(i+1,a-1,b+1,c)
  elif si == 'BC':
    if b == 0 and c == 0:
      return
    elif b == 0 and c > 0:
      ans.append('B')
      dfs(i+1,a,b+1,c-1)
    elif b > 0 and c == 0:
      ans.append('C')
      dfs(i+1,a,b-1,c+1)
    else:
      ans.append('B')
      dfs(i+1,a,b+1,c-1)
      ans.pop()
      ans.append('C')
      dfs(i+1,a,b-1,c+1)
  else:
    if c == 0 and a == 0:
      return
    elif c ==0 and a > 0:
      ans.append('C')
      dfs(i+1,a-1,b,c+1)
    elif c > 0 and a == 0:
      ans.append('A')
      dfs(i+1,a+1,b,c-1)
    else:
      ans.append('C')
      dfs(i+1,a-1,b,c+1)
      ans.pop()
      ans.append('A')
      dfs(i+1,a+1,b,c-1)

dfs(0,a,b,c)
print('No')

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

DP = [[0] * 3 for _ in range(N + 1)]
DP[0] = [A, B, C]

L = [""] * N
R = {}
R[0] = "A"
R[1] = "B"
R[2] = "C"
A = []
B = []
for i in range(N):
  t = s[i]
  if t == "AB":
    x = 0
    y = 1
  if t == "BC":
    x = 1
    y = 2
  if t == "AC":
    x = 0
    y = 2
  if DP[i][y] != 0:
    DP[i + 1] = DP[i]
    DP[i + 1][x] += 1
    DP[i + 1][y] -= 1
    L[i] = R[x]
    if x == 0:
      A.append([i, y])
    else:
      B.append([i, y])
  elif DP[i][x] != 0:
    DP[i + 1] = DP[i]
    DP[i + 1][x] -= 1
    DP[i + 1][y] += 1
    L[i] = R[y]
  else:
    if x == 0:
      if len(B) == 0:
        print("No")
        exit()
      z = 1
      k, n = B.pop()
    if x == 1:
      if len(A) == 0:
        print("No")
        exit()
      z = 0
      k, n = A.pop()
    DP[i + 1] = DP[i]
    DP[i + 1][z] -= 2
    DP[i + 1][n] += 2
    if n == x:
      x, y = y, x
    DP[i + 1][x] += 1
    DP[i + 1][y] -= 1
    if DP[i + 1][z] < 0:
      print("No")
      exit()
    L[i] = R[x]
    L[k] = R[y]

print("Yes")
for i in L:
  print(i)

Solution (Python):
N,A,B,C = map(int,input().split())
ans_list= []
chk = 0
count = 1
for i in range(N):
    s = input()
    if chk == 1:
        if s == "AC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    A += 1
                    B -= 1
                    ans_list.append(["A"])
                else:
                    A -= 1
                    B += 1
                    ans_list.append(["B"])
                num -= 1
            chk = 0
            count = 1
        elif s == "BC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    A -= 1
                    B += 1
                    ans_list.append(["B"])
                else:
                    A += 1
                    B -= 1
                    ans_list.append(["A"])
                num -= 1
            chk = 0
            count = 1
        else:
            count += 1
            continue
    elif chk == 2:
        if s == "AB":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    A += 1
                    C -= 1
                    ans_list.append(["A"])
                else:
                    A -= 1
                    C += 1
                    ans_list.append(["C"])
                num -= 1
            chk = 0
            count = 1
        elif s == "BC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    A -= 1
                    C += 1
                    ans_list.append(["C"])
                else:
                    A += 1
                    C -= 1
                    ans_list.append(["A"])
                num -= 1
            chk = 0
            count = 1
        else:
            count += 1
            continue
    elif chk == 3:
        if s == "AB":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    B += 1
                    C -= 1
                    ans_list.append(["B"])
                else:
                    B -= 1
                    C += 1
                    ans_list.append(["C"])
                num -= 1
            chk = 0
            count = 1
        elif s == "AC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    B -= 1
                    C += 1
                    ans_list.append(["C"])
                else:
                    B += 1
                    C -= 1
                    ans_list.append(["B"])
                num -= 1
            chk = 0
            count = 1
        else:
            count += 1
            continue
    if s == "AB" and A == 0 and B == 0:
        print("No")
        break
    elif s == "BC" and B == 0 and C == 0:
        print("No")
        break
    elif s == "AC" and A == 0 and C == 0:
        print("No")
        break
    else:
        if s == "AB":
            if A == 0:
                A += 1
                B -= 1
                ans_list.append(["A"])
            elif B == 0:
                A -= 1
                B += 1
                ans_list.append(["B"])
            elif A == 1 and B != 1 :
                A += 1
                B -= 1
                ans_list.append(["A"])
            elif B == 1 and A != 1:
                A -= 1
                B += 1
                ans_list.append(["B"])
            elif A == 1 and B == 1:
                chk = 1
            else:
                A += 1
                B -= 1
                ans_list.append(["A"])
        elif s == "AC":
            if A == 0:
                A += 1
                C -= 1
                ans_list.append(["A"])
            elif C == 0:
                A -= 1
                C += 1
                ans_list.append(["C"])
            elif A == 1 and C != 1 :
                A += 1
                C -= 1
                ans_list.append(["A"])
            elif C == 1 and A != 1:
                A -= 1
                C += 1
                ans_list.append(["C"])
            elif A == 1 and C == 1:
                chk = 2
            else:
                A += 1
                C -= 1
                ans_list.append(["A"])
        else:
            if B == 0:
                B += 1
                C -= 1
                ans_list.append(["B"])
            elif C == 0:
                B -= 1
                C += 1
                ans_list.append(["C"])
            elif B == 1 and C != 1 :
                B += 1
                C -= 1
                ans_list.append(["B"])
            elif C == 1 and B != 1:
                B -= 1
                C += 1
                ans_list.append(["C"])
            elif B == 1 and C == 1:
                chk = 3
            else:
                B += 1
                C -= 1
                ans_list.append(["B"])
if chk == 1:
    for i in range(count):
        if i%2 == 0:
            A += 1
            B -= 1
            ans_list.append(["A"])
        else:
            A -= 1
            B += 1
            ans_list.append(["B"])
elif chk == 2:
    for i in range(count):
        if i%2 == 0:
            A += 1
            C -= 1
            ans_list.append(["A"])
        else:
            A -= 1
            C += 1
            ans_list.append(["C"])
elif chk == 3:
    for i in range(count):
        if i%2 == 0:
            B += 1
            C -= 1
            ans_list.append(["B"])
        else:
            B -= 1
            C += 1
            ans_list.append(["C"])
if len(ans_list) == N:
    print("Yes")
    for i in range(N):
        print(*ans_list[i])

Solution (Python):
import bisect
import collections
import sys

sys.setrecursionlimit(100000)
input = sys.stdin.readline
ACMOD = 1000000007
INF = 1 << 62


def lmi():
    return list(map(int, input().split()))


def llmi(n):
    return [lmi() for _ in range(n)]


N, A, B, C = lmi()
S = [input().strip() for _ in range(N)]

a = A
b = B
total = A + B + C
ans = []
if total != 2:
    for s in S:
        c = total - a - b
        if s == "AB":
            if a == 0 and b == 0:
                print("No")
                exit()
            if a > b:
                a -= 1
                b += 1
                ans.append('B')
            else:
                a += 1
                b -= 1
                ans.append('A')
        if s == "AC":
            if a == 0 and c == 0:
                print("No")
                exit()
            if a > c:
                a -= 1
                ans.append('C')
            else:
                a += 1
                ans.append('A')
        if s == "BC":
            if b == 0 and c == 0:
                print("No")
                exit()
            if b > c:
                b -= 1
                ans.append('C')
            else:
                b += 1
                ans.append('B')
        

    
    print('\n'.join(['Yes'] + ans))
    exit()

case = {(A, B, C): []}  
ans = []

for i, s in enumerate(S):
    
    
    new_case = {}
    for (a, b, c), v in case.items():
        if s == "AB":
            if a == 0 and b == 0:
                continue
            elif a == 1 and b == 1:
                new_case[(0, 2, 0)] = list(v) + ['B']
                v.append('A')
                new_case[(2, 0, 0)] = v
                
                pass
            else:
                v.append('A' if b else 'B')
                new_case[(abs(a - 1), abs(b - 1), c)] = v
        if s == "AC":
            if a == 0 and c == 0:
                continue
            if a == c:
                new_case[(2, 0, 0)] = list(v) + ['A']
                v.append('C')
                new_case[(0, 0, 2)] = v

                
                pass
            else:
                v.append('C' if a else 'A')
                new_case[(abs(a - 1), b, abs(c - 1))] = v

        if s == "BC":
            if b == 0 and c == 0:
                continue
            elif b == c:
                new_case[(0, 2, 0)] = list(v) + ['B']
                v.append('C')
                new_case[(0, 0, 2)] = v
            else:
                v.append('B' if c else 'C')
                new_case[(a, abs(b - 1), abs(c - 1))] = v

    if not new_case:
        print("No")
        exit()
    case = new_case
    if len(case) == 1:
        key = list(case.keys())[0]
        ans += case[key]
        case[key] = []

print('\n'.join(['Yes'] + ans + list(case.values())[0]))

Solution (Python):
import bisect
import collections
import sys

sys.setrecursionlimit(100000)
input = sys.stdin.readline
ACMOD = 1000000007
INF = 1 << 62


def lmi():
    return list(map(int, input().split()))


def llmi(n):
    return [lmi() for _ in range(n)]


N, A, B, C = lmi()
S = [input().strip() for _ in range(N)]

a = A
b = B
total = A + B + C
ans = []
if total != 2:
    for s in S:
        c = total - a - b
        if s == "AB":
            if a == 0 and b == 0:
                print("No")
                exit()
            if a > b:
                a -= 1
                b += 1
                ans.append('B')
            else:
                a += 1
                b -= 1
                ans.append('A')
        if s == "AC":
            if a == 0 and c == 0:
                print("No")
                exit()
            if a > c:
                a -= 1
                ans.append('C')
            else:
                a += 1
                ans.append('A')
        if s == "BC":
            if b == 0 and c == 0:
                print("No")
                exit()
            if b > c:
                b -= 1
                ans.append('C')
            else:
                b += 1
                ans.append('B')
        

    
    print('\n'.join(['Yes'] + ans))
    exit()

case = {(A, B, C): []}  
ans = []

for i, s in enumerate(S):
    
    
    new_case = {}
    for (a, b, c), v in case.items():
        if s == "AB":
            if a == 0 and b == 0:
                continue
            elif a == 1 and b == 1:
                new_case[(0, 2, 0)] = list(v) + ['B']
                v.append('A')
                new_case[(2, 0, 0)] = v
                
                pass
            else:
                v.append('A' if b else 'B')
                new_case[(abs(a - 1), abs(b - 1), c)] = v
        if s == "AC":
            if a == 0 and c == 0:
                continue
            if a == c:
                new_case[(2, 0, 0)] = list(v) + ['A']
                v.append('C')
                new_case[(0, 0, 2)] = v

                
                pass
            else:
                v.append('C' if a else 'A')
                new_case[(abs(a - 1), b, abs(c - 1))] = v

        if s == "BC":
            if b == 0 and c == 0:
                continue
            elif b == c:
                new_case[(0, 2, 0)] = list(v) + ['B']
                v.append('C')
                new_case[(0, 0, 2)] = v
            else:
                v.append('B' if c else 'C')
                new_case[(a, abs(b - 1), abs(c - 1))] = v

    if not new_case:
        print("No")
        exit()
    case = new_case
    if len(case) == 1:
        key = list(case.keys())[0]
        ans += case[key]
        case[key] = []

print('\n'.join(['Yes'] + ans + list(case.values())[0]))

Solution (Python):
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 = (set(opt) & set(opts[i + 1])).pop()
            nums[choice] += 1
            nums[choice ^ 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")))

Solution (Python):

N,A,B,C = map(int, input().split())
list_choice = [input() for i in range(N)]
list_choice.append('')

def operation(A,B,C,S,nextS):
  if S=='AB':
    if max(A,B)==0:
      return ('No','',0,0,0)
    elif (A<B) or (A==1 and B==1 and nextS=='AC'):
      return ('Yes','A',A+1,B-1,C)
    else:
      return ('Yes','B',A-1,B+1,C)
  elif S=='AC':
    if max(A,C)==0:
      return ('No','',0,0,0)
    elif (A<C) or (A==1 and C==1 and nextS=='AB'):
      return ('Yes','A',A+1,B,C-1)
    else:
      return ('Yes','C',A-1,B,C+1)
  else: 
    if max(B,C)==0:
      return ('No','',0,0,0)
    elif (B<C) or (B==1 and C==1 and nextS=='AB'):
      return ('Yes','B',A,B+1,C-1)
    else:
      return ('Yes','C',A,B-1,C+1)
  
def func(A,B,C,list_choice):
  list_add = []
  for i in range(N):
    answer,add,A,B,C = operation(A,B,C,list_choice[i],list_choice[i+1])
    if answer=='No':
      return ('No', [])
    else:
      list_add.append(add)
  return ('Yes',list_add)
  
answer, list_add = func(A,B,C,list_choice)
print(answer)
if answer == 'Yes':
  for add in list_add:
    print(add)

Solution (Python):
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 p < n - 1:
        sp1 = s[p + 1]
        if k[i] in sp1:
            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')

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, a, b, c = LI()
ans = []
D = {'A':a, 'B':b, 'C':c}
sx = SR(n)
for i in range(n):
    s = sx[i]
    s1 = s[0]
    s2 = s[1]
    if D[s1] == D[s2] == 0:
        print('No')
        exit()
    elif D[s[0]] == D[s[1]] == 1 and i + 1 < n:
        if s1 in sx[i + 1]:
            D[s1] += 1
            D[s2] -= 1
            ans += [s1]
        else:
            D[s2] += 1
            D[s1] -= 1
            ans += [s2]
    else:
        if D[s1] > D[s2]:
            ans += [s2]
            D[s1] -= 1
            D[s2] += 1
        else:
            ans += [s1]
            D[s1] += 1
            D[s2] -= 1

print('Yes')
print(*ans, sep='\n')

Solution (Python):
def g_solve(N, A, B, C):
  seq = []
  for _ in range(N):
    s = input()
    if s == 'AB':
      if A > B:
        A -= 1
        B += 1
        seq.append('B')
      else:
        A += 1
        B -= 1
        seq.append('A')
    elif s == 'AC':
      if A > C:
        A -= 1
        C += 1
        seq.append('C')
      else:
        A += 1
        C -= 1
        seq.append('A')
    elif s == 'BC':
      if B > C:
        B -= 1
        C += 1
        seq.append('C')
      else:
        B += 1
        C -= 1
        seq.append('B')
    if A < 0 or B < 0 or C < 0:
      print('No')
      return
  print('Yes')
  print('\n'.join(seq))    


def dp_solve(N, a,b,c):
  state = {}
  state[(a, b, c)] = []
  for _ in range(N):
    s = input()
    next_state = {}
    for a, b, c in state:
      if s == 'AB':
        if b-1 >= 0:
            next_state[(a+1, b-1, c)] = ['A', state[(a,b,c)]]
        if a-1 >= 0:
          next_state[(a-1, b+1, c)] = ['B', state[(a,b,c)]]
      if s == 'AC':
        if c-1 >= 0:
          next_state[(a+1, b, c-1)] = ['A', state[(a,b,c)]]
        if a-1 >= 0:
          next_state[(a-1, b, c+1)] = ['C', state[(a,b,c)]]
      if s == 'BC':
        if c-1 >= 0:
          next_state[(a, b+1, c-1)] = ['B', state[(a,b,c)]]
        if b-1 >= 0:
          next_state[(a, b-1, c+1)] = ['C', state[(a,b,c)]]
    state = next_state
    if not state:
      print('No')
      return
    
  print('Yes')
  ans = []
  seq = list(state.values())[0]
  while seq:
    ans.append(seq[0])
    
    seq = seq[1]
  ans.reverse()
  for a in ans:
    print(a)
    
  
  
N, A, B, C = [int(x) for x in input().split(' ')]
if A + B + C < 20:
  dp_solve(N, A, B, C)
else:
  g_solve(N, A, B, C)

Solution (Python):
d=dict()
n,d["A"],d["B"],d["C"]=map(int,input().split())
s=[input() for i in range(n)]
ans=[]
i=0
while i<n:
    x,y=s[i][0],s[i][1]
    if d[x]==0 and d[y]==0:
        print("No")
        quit()
    elif d[x]==0:
        ans.append(x)
        d[x]+=1
        d[y]-=1
    elif d[y]==0:
        ans.append(y)
        d[x]-=1
        d[y]+=1
    else:
        if i==n-1:
            ans.append(x)
            break
        if y in s[i+1]:
            ans.append(y)
            d[x]-=1
            d[y]+=1
        else:
            ans.append(x)
            d[x]+=1
            d[y]-=1
    i+=1
print("Yes")
for i in ans:
    print(i)

Solution (Python):
N, A, B, C = map(int, input().split())
state = {'A':A, 'B':B, 'C':C}
ans = []
if A+B+C >= 3 or A+B+C == 1:
    for _ in range(N):
        s = input()
        if state[s[0]] == 0 and state[s[1]] == 0:
            print('No')
            exit()
        elif state[s[0]] >= state[s[1]]:
            ans.append(s[1])
            state[s[0]] -= 1
            state[s[1]] += 1
        else:
            ans.append(s[0])
            state[s[0]] += 1
            state[s[1]] -= 1
elif A+B+C == 2:
    sn = 0
    for i in range(N):
        if sn != 0:
            s = sn
            sn = 0
        else:   
            s = input()
        if state[s[0]] == 0 and state[s[1]] == 0:
            print('No')
            exit()
        elif state[s[0]] == state[s[1]]:
            if i == N-1:
                ans.append(s[1])
                state[s[0]] -= 1
                state[s[1]] += 1
            else:
                sn = input()
                if s[0] in sn:
                    ans.append(s[0])
                    state[s[0]] += 1
                    state[s[1]] -= 1
                else:
                    ans.append(s[1])
                    state[s[0]] -= 1
                    state[s[1]] += 1
        elif state[s[0]] > state[s[1]]:
            ans.append(s[1])
            state[s[0]] -= 1
            state[s[1]] += 1
        else:
            ans.append(s[0])
            state[s[0]] += 1
            state[s[1]] -= 1
else:
    print('No')
    exit()

print('Yes')
for i in range(N):
    print(ans[i])