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())
S = []
D = [a,b,c]

for _ in range(N):
    S.append(input())

Y = []

def f(s,ns):
    s0 = ord(s[0])-65
    s1 = ord(s[1])-65
    k = 0
    if D[s0] == D[s1]:
        ns0 = ord(ns[0])-65
        ns1 = ord(ns[1])-65
        
        if s0 == ns0 and s1 == ns1:
            k = 0
        elif s0 == 3 - ns0 - ns1:
            k = 0
        else:
            k = 1
    
    elif D[s0] > D[s1]:
        k = 0
    else:
        k = 1
        
    if k==0:
        D[s0] -= 1
        D[s1] += 1
        
        Y.append(s[1])
    else:
        D[s0] += 1
        D[s1] -= 1
        
        Y.append(s[0])
        

g = True

for i in range(N):
    s = S[i]
    if i == N-1:
        ns = s
    else:
        ns = S[i+1]
    
    f(s,ns)
    
    if D[0] < 0 or D[1] < 0 or D[2] < 0:
        print("No")
        g = False
        break
    
if g:
    print("Yes")
    for y in Y:
        print(y)

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy


from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd

sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    n,a,b,c = inpm()
    S = []
    ans = []
    for i in range(n):
        s = input()
        S.append(s)
    for i in range(n):
        s = S[i]
        if s == 'AB':
            if a == 0 and b == 0:
                print('No')
                return
            if a == 0:
                a += 1
                b -= 1
                ans.append('A')
            elif b == 0:
                a -= 1
                b += 1
                ans.append('B')
            elif a == 1 and b == 1 and i < n-1 and s != S[i+1]:
                if S[i+1] == 'AC':
                    a += 1
                    b -= 1
                    ans.append('A')
                else:
                    b += 1
                    a -= 1
                    ans.append('B')
            else:
                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')
                return
            if a == 0:
                a += 1
                c -= 1
                ans.append('A')
            elif c == 0:
                a -= 1
                c += 1
                ans.append('C')
            elif a == 1 and c == 1 and i < n-1 and s != S[i+1]:
                if S[i+1] == 'AB':
                    a += 1
                    c -= 1
                    ans.append('A')
                else:
                    c += 1
                    a -= 1
                    ans.append('C')
            else:
                if a > c:
                    a -= 1
                    c += 1
                    ans.append('C')
                else:
                    a += 1
                    c -= 1
                    ans.append('A')
        if s == 'BC':
            if c == 0 and b == 0:
                print('No')
                return
            if b == 0:
                b += 1
                c -= 1
                ans.append('B')
            elif c == 0:
                b -= 1
                c += 1
                ans.append('C')
            elif b == 1 and c == 1 and i < n-1 and s != S[i+1]:
                if S[i+1] == 'AB':
                    b += 1
                    c -= 1
                    ans.append('B')
                else:
                    c += 1
                    b -= 1
                    ans.append('C')
            else:
                if b > c:
                    b -= 1
                    c += 1
                    ans.append('C')
                else:
                    b += 1
                    c -= 1
                    ans.append('B')
    print('Yes')
    for i in range(n):
        print(ans[i])

if __name__ == "__main__":
    main()

Solution (Python):

import sys
input = lambda: sys.stdin.readline().strip()
N, A, B, C = [int(x) for x in input().split()]
s = [input() for _ in range(N)]
abc = {'A': A, 'B': B, 'C': C}
can = True
moves = []
for i in range(N):
    moves.append(next(j for j in (0, 1) if abc[s[i][j]] < abc[s[i][j ^ 1]] or abc[s[i][j]] == abc[s[i][j ^ 1]] and (i + 1 == N or s[i + 1] == s[i] or s[i][j ^ 1] not in s[i + 1])))
    abc[s[i][moves[-1]]] += 1
    abc[s[i][moves[-1] ^ 1]] -= 1
    can &= all(x >= 0 for x in abc.values())
if can:
    print('Yes')
    print(*(s[i][moves[i]] for i in range(N)), sep='\n')
else:
    print('No')

Solution (Python):
def main():
    n, a, b, c = map(int, input().split())
    s_list = [input() for _ in [0]*n]

    if a == b == c == 0:
        print("No")
    elif a+b+c == 1:
        if a == 1:
            color = "A"
        elif b == 1:
            color = "B"
        else:
            color = "C"
        l = []
        for i in s_list:
            if color not in i:
                print("No")
                return
            j = list(i)
            j.remove(color)
            color = j[0]
            l.append(color)
        print("Yes")
        for i in l:
            print(i)
    else:
        s_list += ["ABC"]
        l = []
        for j, i in enumerate(s_list[:-1]):
            if i == "AB":
                if a >= 2:
                    l.append("B")
                    a -= 1
                    b += 1
                elif b >= 2:
                    l.append("A")
                    a += 1
                    b -= 1
                elif a == b == 0:
                    print("No")
                    return
                elif a == 1 and b == 0:
                    l.append("B")
                    a -= 1
                    b += 1
                elif a == 0 and b == 1:
                    l.append("A")
                    a += 1
                    b -= 1
                else:
                    if "A" in s_list[j+1]:
                        l.append("A")
                        a += 1
                        b -= 1
                    else:
                        l.append("B")
                        a -= 1
                        b += 1
            elif i == "BC":
                if b >= 2:
                    l.append("C")
                    b -= 1
                    c += 1
                elif c >= 2:
                    l.append("B")
                    b += 1
                    c -= 1
                elif b == c == 0:
                    print("No")
                    return
                elif b == 1 and c == 0:
                    l.append("C")
                    b -= 1
                    c += 1
                elif b == 0 and c == 1:
                    l.append("B")
                    b += 1
                    c -= 1
                else:
                    if "B" in s_list[j+1]:
                        l.append("B")
                        b += 1
                        c -= 1
                    else:
                        l.append("C")
                        b -= 1
                        c += 1
            else:
                if c >= 2:
                    l.append("A")
                    c -= 1
                    a += 1
                elif a >= 2:
                    l.append("C")
                    c += 1
                    a -= 1
                elif c == a == 0:
                    print("No")
                    return
                elif c == 1 and a == 0:
                    l.append("A")
                    c -= 1
                    a += 1
                elif c == 0 and a == 1:
                    l.append("C")
                    c += 1
                    a -= 1
                else:
                    if "C" in s_list[j+1]:
                        l.append("C")
                        c += 1
                        a -= 1
                    else:
                        l.append("A")
                        c -= 1
                        a += 1
        print("Yes")
        for i in l:
            print(i)


main()

Solution (Python):
n,*s=open(0)
d={i:int(j)for i,j in zip('笑ABC',n.split())}
r=[]
for(i,j,_),(k,l,_)in zip(s,s[1:]):
    if d[i]<1>d[j]:exit(print('No'))
    if d[i]<1:
        d[i]+=1
        d[j]-=1
        r+=i,
    elif d[j]<1:
        d[i]-=1
        d[j]+=1
        r+=j,
    else:
        t={i,j}&{k,l}
        if t:
            t,*_=t
            u,*_={i,j}-{t}
        else:t,u=i,j
        d[t]+=1
        d[u]-=1
        r+=t,
i,j,_=s[-1]
if d[i]<1>d[j]:exit(print('No'))
if d[i]<1:
    d[i]+=1
    d[j]-=1
    r+=i,
else:
    d[i]-=1
    d[j]+=1
    r+=j,
print('Yes',*r)

Solution (Python):
n,*s=open(0)
d={i:int(j)for i,j in zip('笑ABC',n.split())}
r=[]
for(i,j,_),(k,l,_)in zip(s,s[1:]):
    if d[i]<1>d[j]:exit(print('No'))
    if d[i]<1:
        d[i]+=1
        d[j]-=1
        r+=i,
    elif d[j]<1:
        d[i]-=1
        d[j]+=1
        r+=j,
    else:
        t={i,j}&{k,l}
        if t:
            t,*_=t
            u,*_={i,j}-{t}
        else:t,u=i,j
        d[t]+=1
        d[u]-=1
        r+=t,
i,j,_=s[-1]
if d[i]<1>d[j]:exit(print('No'))
if d[i]<1:
    d[i]+=1
    d[j]-=1
    r+=i,
else:
    d[i]-=1
    d[j]+=1
    r+=j,
print('Yes',*r)

Solution (Python):
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

n, a, b, c = na()
d = [a, b, c]
s = nsn(n)

ans = []
flag = True
for i in range(n):
    x, y = [ord(c) - ord('A') for c in s[i]]
    if d[x] == 0 and d[y] == 0:
        flag = False
        break
    elif d[x] < d[y]:
        ans.append(x)
        d[x] += 1
        d[y] -= 1
    elif d[x] > d[y]:
        ans.append(y)
        d[x] -= 1
        d[y] += 1
    elif d[x] == d[y] and d[x] >= 2:
        ans.append(x)
        d[x] += 1
        d[y] -= 1
    else:
        if i + 1 == n:
            ans.append(x)
        else:
            nx, ny = [ord(c) - ord('A') for c in s[i + 1]]
            if x == nx:
                ans.append(x)
                d[x] += 1
                d[y] -= 1
            elif y == ny:
                ans.append(y)
                d[x] -= 1
                d[y] += 1
            elif x == 1:
                ans.append(x)
                d[x] += 1
                d[y] -= 1
            else:
                ans.append(y)
                d[x] -= 1
                d[y] += 1

if flag:
    print("Yes")
    for i in range(n):
        print(chr(ans[i] + ord('A')))
else:
    print("No")

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]





def dfs(i, d, ans):
    if d[0]<0 or d[1]<0 or d[2]<0:
        return
    if i == N:
        print('Yes')
        for m in ans:
            print(m)
        exit()
    s, t = cv(S[i][0]), cv(S[i][1])
    d[s] += 1
    d[t] -= 1
    
    ans[i] = S[i][0]
    dfs(i+1, D, ans)
    d[s] -= 2
    d[t] += 2
    
    ans[i] = S[i][1]
    dfs(i+1, d, ans)
    d[s] += 1
    d[t] -= 1

def cv(m):
    if m == 'A':
        return 0
    if m == 'B':
        return 1
    if m == 'C':
        return 2
'''
def dfs(i, A, B, C, ans):
    if A<0 or B<0 or C<0:
        return
    if i == N:
        print('Yes')
        for m in ans:
            print(m)
        exit()
    if S[i] == 'AB':
        ans[i] = 'A'
        dfs(i+1, A+1, B-1, C, ans)
        ans[i] = 'B'
        dfs(i+1, A-1, B+1, C, ans)
    if S[i] == 'BC':
        ans[i] = 'B'
        dfs(i+1, A, B+1, C-1, ans)
        ans[i] = 'C'
        dfs(i+1, A, B-1, C+1, ans)
    if S[i] == 'AC':
        ans[i] = 'A'
        dfs(i+1, A+1, B, C-1, ans)
        ans[i] = 'C'
        dfs(i+1, A-1, B, C+1, ans)
'''
N, *D = mi()
S = [sys.stdin.readline()[:2] for _ in range(N)]
ans = [0]*N
dfs(0, D, ans)
print('No')

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]





N, *D = mi()
S = [sys.stdin.readline()[:2] for _ in range(N)]
ans = [0]*N

def cv(m):
    if m == 'A':
        return 0
    if m == 'B':
        return 1
    if m == 'C':
        return 2

def dfs(i, A, B, C, ans):
    if A<0 or B<0 or C<0:
        return
    if i == N:
        print('Yes')
        for m in ans:
            print(m)
        exit()
    
    '''
    D[s] += 1
    D[t] -= 1
    ans[i] = S[i][0]
    dfs(i+1, D, ans)
    D_[s] -= 1
    D_[t] += 1
    ans_[i] = S[i][1]
    dfs(i+1, D_, ans_)
    '''
    if S[i] == 'AB':
        ans[i] = 'A'
        dfs(i+1, A+1, B-1, C, ans)
        ans[i] = 'B'
        dfs(i+1, A-1, B+1, C, ans)
    if S[i] == 'BC':
        ans[i] = 'B'
        dfs(i+1, A, B+1, C-1, ans)
        ans[i] = 'C'
        dfs(i+1, A, B-1, C+1, ans)
    if S[i] == 'AC':
        ans[i] = 'A'
        dfs(i+1, A+1, B, C-1, ans)
        ans[i] = 'C'
        dfs(i+1, A-1, B, C+1, ans)

dfs(0, D[0], D[1], D[2], ans)
print('No')

Solution (Python):


N,A,B,C=map(int,input().split())

hashmap=dict()
hashmap={"A":A,"B":B,"C":C}
arr=[int(0) for i in range(N)]
for i in range(N):
    arr[i]=input()
    
output=[]

for i in range(N):
    string=arr[i]
    first=string[0]
    second=string[1]

    if hashmap[first]==0 and hashmap[second]==0:
        print("No")
        break
    elif hashmap[first]==0:
        hashmap[first]+=1
        hashmap[second]-=1
        output.append(first)
    elif hashmap[second]==0:
        hashmap[second]+=1
        hashmap[first]-=1
        output.append(second)
    elif i<N-1 and (first in arr[i+1]):
        hashmap[first]+=1
        hashmap[second]-=1
        output.append(first)
    elif i<N-1 and (second in arr[i+1]):
        hashmap[second]+=1
        hashmap[first]-=1
        output.append(second)
    else:
        output.append(first)
else:
    print("Yes")
    for i in output:
        print(i)

Solution (Python):
ABC = [0] * 3
N, ABC[0], ABC[1], ABC[2] = map(int, input().split())
s = [''] * N
for i in range(N):
	s[i] = input()
a = ''
b = True
for i in range(N):
	if not ABC[ord(s[i][0]) - ord('A')]:
		if not ABC[ord(s[i][1]) - ord('A')]:
			b = False
			break
		ABC[ord(s[i][0]) - ord('A')] += 1
		ABC[ord(s[i][1]) - ord('A')] -= 1
		a += s[i][0]
	elif not ABC[ord(s[i][1]) - ord('A')]:
		ABC[ord(s[i][0]) - ord('A')] -= 1
		ABC[ord(s[i][1]) - ord('A')] += 1
		a += s[i][1]
	else:
		if i == N - 1:
			ABC[ord(s[i][0]) - ord('A')] += 1
			ABC[ord(s[i][1]) - ord('A')] -= 1
			a += s[i][0]
		else:
			if s[i][0] == s[i + 1][0] or s[i][0] == s[i + 1][1]:
				ABC[ord(s[i][0]) - ord('A')] += 1
				ABC[ord(s[i][1]) - ord('A')] -= 1
				a += s[i][0]
			else:
				ABC[ord(s[i][0]) - ord('A')] -= 1
				ABC[ord(s[i][1]) - ord('A')] += 1
				a += s[i][1]
if b:
	print('Yes')
	for i in a:
		print(i)
else:
	print('No')

Solution (Python):
import sys
input = sys.stdin.readline
n,a,b,c = map(int,input().strip().split())
cc = []
for i in range(n):
  ff = input().strip()
  cc.append((ff))
  
chk = True
ans = []
for num,q in enumerate(cc):
  if q[0] == 'A' and q[1] == 'B':
    m = max(a,b)
    if m < 1:
      chk = False
      break
    if a == 1 and b == 1:
      if num == n-1:
        ans.append('A')
      else:
        if cc[num+1][0] == 'A':
          a += 1
          b -= 1
          ans.append('A')
        else:
          a -= 1
          b += 1
          ans.append('B')       
    elif m == a:
      a -= 1
      b += 1
      ans.append('B')
    else:
      a += 1
      b -= 1
      ans.append('A')      
  if q[0] == 'A' and q[1] == 'C':
    m = max(a,c)
    if m < 1:
      chk = False
      break
    if c == 1 and a == 1:
      if num == n-1:
        ans.append('A')
      else:
        if cc[num+1][0] == 'A':
          a += 1
          c -= 1
          ans.append('A')
        else:
          a -= 1
          c += 1
          ans.append('C')       
    elif m == a:
      a -= 1
      c += 1
      ans.append('C')       
    else:
      a += 1
      c -= 1
      ans.append('A')       
  if q[0] == 'B' and q[1] == 'C':
    m = max(b,c)
    if m < 1:
      chk = False
      break
    if c == 1 and b == 1:
      if num == n-1:
        ans.append('B')
      else:      
        if cc[num+1][1] == 'C':
          c += 1
          b -= 1
          ans.append('C')
        else:
          c -= 1
          b += 1
          ans.append('B')       
    elif m == b:
      b -= 1
      c += 1
      ans.append('C')
    else:
      b += 1
      c -= 1
      ans.append('B')
      
if not chk:
  print('No')
else:
  print('Yes')
  print(*ans,sep='\n')

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

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
N,A,B,C = map(int,input().split())
Q = []
ans = []
f = 0
def dfs(f,a,b,c):
    if f == N:return True
    if Q[f] == 'AB':
        if a+b == 0:return False
        if b>0 and dfs(f+1,a+1,b-1,c):
            ans.append('A')
            return True
        if a>0 and dfs(f+1,a-1,b+1,c):
            ans.append('B')
            return True
        
        return False
    if Q[f] == 'BC':
        if c+b == 0:return False
        if c>0 and dfs(f+1,a,b+1,c-1):
            ans.append('B')
            return True
        if b>0 and dfs(f+1,a,b-1,c+1):
            ans.append('C')
            return True
        
        return False
    if Q[f] == 'AC':
        if a+c == 0:return False
        if c>0 and dfs(f+1,a+1,b,c-1):
            ans.append('A')
            return True
        if a>0 and dfs(f+1,a-1,b,c+1):
            ans.append('C')
            return True
        
        return False
    

for i in range(N):
    s = input()
    Q.append(s)
dfs(0,A,B,C)

if len(ans) == N:
    print('Yes')
    ans = ans[::-1]
    for i in ans:
        print(i)
else:
    print('No')

Solution (Python):
n, *abc = map(int, input().split())
cond = {'AB': (0, 1), 'AC': (0, 2), 'BC': (1, 2)}
legend = ['A', 'B', 'C']
ans = []
prev_s = ''
for _ in range(n):
    s = input()
    idx1, idx2 = cond[s]
    if abc[idx1] == abc[idx2] == 0 and ans:
        prev_x = ans.pop()
        if prev_x == prev_s[0]:
            prev_y = prev_s[1]
        else:
            prev_y = prev_s[0]
        ans.append(prev_y)
        abc[legend.index(prev_x)] -= 2
        abc[legend.index(prev_y)] += 2

    if abc[idx1] < abc[idx2]:
        abc[idx1] += 1
        abc[idx2] -= 1
        ans.append(legend[idx1])
    else:
        abc[idx2] += 1
        abc[idx1] -= 1
        ans.append(legend[idx2])
    prev_s = s
    if -1 in abc:
        print('No')
        break
else:
    print('Yes')
    for x in ans:
        print(x)

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

for i in range(N):
    s=S[i]
    s0=s[0]
    s1=s[1]

    if d[s0]==d[s1]==0:
        print('No')
        break
    elif d[s0]==0:
        d[s0]+=1
        d[s1]-=1
        out.append(s0)
    elif d[s1]==0:
        d[s0]-=1
        d[s1]+=1
        out.append(s1)
    elif i == N-1:
        out.append(s0)
    elif s0 in S[i+1]:
        d[s0]+=1
        d[s1]-=1
        out.append(s0)
    else:
        d[s0]-=1
        d[s1]+=1
        out.append(s1)
else:
    print('Yes')
    for o in out:
        print(o)