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())
ans=[]
ks = [input() for _ in range(n)]
for i in range(n):
  k = ks[0]
  if k=="AB":
    if a==0 and b==0:
      print("No")
      quit()
    else:
      if a>b:
        a -= 1
        b+=1
        ans.append("B")
      else:
        if a == 1 and b == 1 and len(ks) > 1:
          if ks[1] == "AC":
            b-=1
            a+=1
            ans.append("A")
          else:
            a -= 1
            b+=1
            ans.append("B")
        else: 
          b-=1
          a+=1
          ans.append("A")
  if k=="BC":
    if c==0 and b==0:
      print("No")
      quit()
    else:
      if c>b:
        c -= 1
        b+=1
        ans.append("B")
      else:
        if c == 1 and b == 1 and len(ks) > 1:
          if ks[1] == "AC":
            b-=1
            c+=1
            ans.append("C")
          else:
            c -= 1
            b+=1
            ans.append("B")
        else: 
          b-=1
          c+=1
          ans.append("C")
  if k=="AC":
    if a==0 and c==0:
      print("No")
      quit()
    else:
      if a>c:
        a -= 1
        c+=1
        ans.append("C")
      else:
        if a == 1 and c == 1 and len(ks) > 1:
          if ks[1] == "BC":
            a-=1
            c+=1
            ans.append("C")
          else:
            c -= 1
            a+=1
            ans.append("A")
        else: 
          c-=1
          a+=1
          ans.append("A")
  del(ks[0])
print("Yes")
for i in ans:
  print(i)

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

path = []
inputs = [input() for _ in range(N)]

for i in range(N):
	target = inputs[i]
	if target == 'AB':
		if A > B:
			A -= 1
			B += 1
			path.append('B')
		elif A == 1 and B == 1:
			if i != N - 1 and inputs[i + 1] == 'AC':
				A += 1
				B -= 1
				path.append('A')
			else:
				A -= 1
				B += 1
				path.append('B')
		else:
			A += 1
			B -= 1
			path.append('A')
	if target == 'AC':
		if A > C:
			A -= 1
			C += 1
			path.append('C')
		elif A == 1 and C == 1:
			if i != N - 1 and inputs[i + 1] == 'AB':
				A += 1
				C -= 1
				path.append('A')
			else:
				A -= 1
				C += 1
				path.append('C')
		else:
			A += 1
			C -= 1
			path.append('A')
	if target == 'BC':
		if B > C:
			B -= 1
			C += 1
			path.append('C')
		elif B == 1 and C == 1:
			if i != N - 1 and inputs[i + 1] == 'AB':
				B += 1
				C -= 1
				path.append('B')
			else:
				B -= 1
				C += 1
				path.append('C')
		else:
			B += 1
			C -= 1
			path.append('B')
	if A < 0 or B < 0 or C < 0:
		print('No')
		exit(0)

print('Yes')
for i in path:
	print(i)

Solution (Python):


import sys
from collections import defaultdict


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


def psum(pair):
    if pair == 'AB':
        return a + b
    elif pair == 'AC':
        return a + c
    elif pair == 'BC':
        return b + c

def select(pair, npair):
    global a, b, c
    added = None
    if pair == 'AB':
        if a < b:
            a += 1
            b -= 1
            added = 'A'
        elif a > b:
            a -= 1
            b += 1
            added = 'B'            
        else:
            
            if a + b == 2 and c == 0 and npair:
                if npair == 'AB':
                    a += 1
                    b -= 1
                    added = 'A'
                elif npair == 'AC':
                    a += 1
                    b -= 1
                    added = 'A'                    
                elif npair == 'BC':
                    a -= 1
                    b += 1
                    added = 'B'                    
            else:
                a += 1
                b -= 1
                added = 'A'
            
    elif pair == 'AC':
        if a < c:
            a += 1
            c -= 1
            added = 'A'
        elif a > c:
            a -= 1
            c += 1
            added = 'C'            
        else:
            
            if a + c == 2 and b == 0 and npair:
                if npair == 'AB':
                    a += 1
                    c -= 1
                    added = 'A'                    
                elif npair == 'AC':
                    a += 1
                    c -= 1
                    added = 'A'                    
                elif npair == 'BC':
                    a -= 1
                    c += 1
                    added = 'C'
            else:
                a += 1
                c -= 1
                added = 'A'                
        
    elif pair == 'BC':
        if b < c:
            b += 1
            c -= 1
            added = 'B'            
        elif b > c:
            b -= 1
            c += 1
            added = 'C'            
        else:
            
            if b + c == 2 and a == 0 and npair:
                if npair == 'AB':
                    b += 1
                    c -= 1
                    added = 'B'                    
                elif npair == 'AC':
                    b -= 1
                    c += 1
                    added = 'C'                    
                elif npair == 'BC':
                    b += 1
                    c -= 1
                    added = 'B'
            else:
                b += 1
                c -= 1
                added = 'B'
    return added

    
ans = []
if a + b + c == 0:
    print('No')
    exit()
else:
    for i, s in enumerate(ss):
        if psum(s) == 0:
            print('No')
            exit()
        else:
            ns = None
            if i < len(ss) - 1:
                ns = ss[i + 1]
            ans.append(select(s, ns))

        
print('Yes')
for l in ans:
    print(l)

Solution (Python):
import sys
n,a,b,c=map(int,input().split())
slist=[]
for i in range(n):
    slist.append(input())
output=[]
ynflag=True

def abc1(s):
    global a
    global b
    global c
    global output
    global ynflag

    
    if s == "AB":
        if a ==1:
            a -=1
            b +=1
            output.append("B")
            return True
        elif b == 1:
            a +=1
            b -=1
            output.append("A")
            return True
        else:
            return False
    if s == "AC":
        if a ==1:
            a -=1
            c +=1
            output.append("C")
            return True
        elif c == 1:
            a +=1
            c -=1
            output.append("A")
            return True
        else:
            return False
    if s == "BC":
        if b ==1:
            b -=1
            c +=1
            output.append("C")
            return True
        elif c == 1:
            b +=1
            c -=1
            output.append("B")
            return True
        else:
            return False

def abc2(i):
    global a
    global b
    global c
    global output
    global slist
    s=slist[i]
    
    if i == len(slist)-1:
        tsugi="DUMMY"
    else:
        tsugi=slist[i+1]
    
    if s == "AB":
        if a + b == 0:
            return False
        elif a == 1 and b == 1 and c == 0:
            if tsugi == "BC":
                a -=1
                b +=1
                output.append("B")
                return True
            else:
                a +=1
                b -=1
                output.append("A")
                return True
        else:
            if a >=b:
                a -=1
                b +=1
                output.append("B")
                return True
            else:
                a +=1
                b -=1
                output.append("A")
                return True
    elif s == "AC":
        if a + c == 0:
            return False
        elif a == 1 and c == 1 and b == 0:
            if tsugi == "BC":
                a -=1
                c +=1
                output.append("C")
                return True
            else:
                a +=1
                c -=1
                output.append("A")
                return True
        else:
            if a >=c:
                a -=1
                c +=1
                output.append("C")
                return True
            else:
                a +=1
                c -=1
                output.append("A")
                return True
    else:
        if b + c == 0:
            return False
        elif b == 1 and c == 1 and a == 0:
            if tsugi == "AC":
                b -=1
                c +=1
                output.append("C")
                return True
            else:
                b +=1
                c -=1
                output.append("B")
                return True
        else:
            if b >=c:
                b -=1
                c +=1
                output.append("C")
                return True
            else:
                b +=1
                c -=1
                output.append("B")
                return True

if a == 0 and b == 0 and c == 0:
    print("No")
    sys.exit()

else:
    if a+b+c == 1:
        for s in slist:
            ynflag=abc1(s)
            if not ynflag:
                print("No")
                sys.exit()
    else:
        for i in range(n):
            ynflag=abc2(i)
            if not ynflag:
                print("No")
                sys.exit()
    print("Yes")
    for out in output:
        print(out)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def main():
    N, A, B, C = map(int, readline().split())
    s = read().decode().split()
    s.append('AB')
    ans = []
    for i in range(N):
        if s[i] == 'AB':
            if A == 0 and B == 0:
                print('No')
                break
            else:
                if A < B:
                    A += 1
                    B -= 1
                    ans.append('A')
                elif A > B:
                    A -= 1
                    B += 1
                    ans.append('B')
                else:
                    if s[i+1] == 'AB' or s[i+1] == 'AC':
                        A += 1
                        B -= 1
                        ans.append('A')
                    else:
                        A -= 1
                        B += 1
                        ans.append('B')
        if s[i] == 'AC':
            if A == 0 and C == 0:
                print('No')
                break
            else:
                if A < C:
                    A += 1
                    C -= 1
                    ans.append('A')
                elif A > C:
                    A -= 1
                    C += 1
                    ans.append('C')
                else:
                    if s[i + 1] == 'AB' or s[i + 1] == 'AC':
                        A += 1
                        C -= 1
                        ans.append('A')
                    else:
                        A -= 1
                        C += 1
                        ans.append('C')
        if s[i] == 'BC':
            if C == 0 and B == 0:
                print('No')
                break
            else:
                if C < B:
                    C += 1
                    B -= 1
                    ans.append('C')
                elif C > B:
                    C -= 1
                    B += 1
                    ans.append('B')
                else:
                    if s[i + 1] == 'AB' or s[i + 1] == 'BC':
                        B += 1
                        C -= 1
                        ans.append('B')
                    else:
                        B -= 1
                        C += 1
                        ans.append('C')
    else:
        print('Yes')
        print('\n'.join(ans))
    return

if __name__ == '__main__':
    main()

Solution (Python):

import sys
sysread = sys.stdin.readline
sys.setrecursionlimit(10**7)
def run():
    pri = []
    N, A, B, C = map(int, sysread().split())
    vals = {'A':A, 'B':B, 'C':C}
    S = [sysread() for _ in range(N)]

    pri = []
    for idx, s in enumerate(S):
        if vals[s[0]] == vals[s[1]] and vals[s[0]] == 1 and idx !=len(S)-1:
            if s[0] in S[idx+1]:
                vals[s[0]] += 1
                vals[s[1]] -= 1
                pri.append(s[0])
            else:
                vals[s[0]] -= 1
                vals[s[1]] += 1
                pri.append(s[1])
        else:
            if vals[s[0]] <= vals[s[1]]:
                vals[s[0]] += 1
                vals[s[1]] -= 1
                pri.append(s[0])
            else:
                vals[s[0]] -= 1
                vals[s[1]] += 1
                pri.append(s[1])

        if not check_vals(vals):
            print('No')
            return None
    print('Yes')
    for st in pri:
        print(st)

def check_vals(vals):
    for v in vals.values():
        if v < 0:
            return False
    return True

if __name__ == "__main__":
    run()

Solution (Python):
from heapq import *
import sys
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import math
sys.setrecursionlimit(4100000)
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
input=lambda :sys.stdin.readline().rstrip()
N,A,B,C=map(int,input().split())
S=[input() for i in range(N)]
idleA=10**20
idleB=10**10
idleC=1
stack=[[0,A,B,C]]
lst={}
dp=[defaultdict(str) for i in range(N+1)]
def main(a,b,c):
    return(a*idleA+b*idleB+c)

while stack:
    
    n,a,b,c=stack.pop()
    if n==N:
        stack.append([n,a,b,c])
        break
    s=S[n]
    if s=="AB":
        if a>0:
            Q=main(a-1,b+1,c)
            if not dp[n+1][Q]:
                dp[n+1][Q]="B"
                stack.append([n+1,a-1,b+1,c])
        if b>0:
            Q=main(a+1,b-1,c)
            if not dp[n+1][Q]:
                dp[n+1][Q]="A"
                stack.append([n+1,a+1,b-1,c])
    elif s=="BC":
        if b>0:
            Q=main(a,b-1,c+1)
            if not dp[n+1][Q]:
                dp[n+1][Q]="C"
                stack.append([n+1,a,b-1,c+1])
        if c>0:
            Q=main(a,b+1,c-1)
            if not dp[n+1][Q]:
                dp[n+1][Q]="B"
                stack.append([n+1,a,b+1,c-1])
    elif s=="AC":
        if a>0:
            Q=main(a-1,b,c+1)
            if not dp[n+1][Q]:
                dp[n+1][Q]="C"
                stack.append([n+1,a-1,b,c+1])
        if c>0:
            Q=main(a+1,b,c-1)
            if not dp[n+1][Q]:
                dp[n+1][Q]="A"
                stack.append([n+1,a+1,b,c-1])

if stack:
    print("Yes")
    lst=[]
    n,a,b,c=stack.pop()

    lst.append([dp[n][main(a,b,c)],a,b,c])
    
    for n in range(N-1)[::-1]:
        s=S[n+1]
        X,a,b,c=lst[-1]
        if s=="AB":
            if X=="A":
                Q=main(a-1,b+1,c)
                if dp[n+1][Q]:
                    lst.append([dp[n+1][Q],a-1,b+1,c])
                    continue
            if X=="B":
                Q=main(a+1,b-1,c)
                if dp[n+1][Q]:
                    lst.append([dp[n+1][Q],a+1,b-1,c])
                    continue
        elif s=="BC":
            if X=="B":
                Q=main(a,b-1,c+1)
                if dp[n+1][Q]:
                    lst.append([dp[n+1][Q],a,b-1,c+1])
                    continue
            if X=="C":
                Q=main(a,b+1,c-1)
                if dp[n+1][Q]:
                    lst.append([dp[n+1][Q],a,b+1,c-1])
                    continue
        elif s=="AC":
            if X=="A":
                Q=main(a-1,b,c+1)
                if dp[n+1][Q]:
                    lst.append([dp[n+1][Q],a-1,b,c+1])
                    continue
            if X=="C":
                Q=main(a+1,b,c-1)
                if dp[n+1][Q]:
                    lst.append([dp[n+1][Q],a+1,b,c-1])
                    continue
        
    for X,a,b,c in list(reversed(lst)):
        print(X)
else:
    print("No")

Solution (Python):
import sys


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


def solve():
    def distribute(fm, to):
        count[fm] -= 1
        count[to] += 1
        ans.append(to)

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

    count = {'A': a, 'B': b, 'C': c}
    ans = ['Yes']

    for i in range(n):
        x, y = queries[i]
        if count[x] == count[y] == 0:
            return 'No'
        elif count[x] == count[y] == 1:
            if i == n - 1:
                distribute(x, y)
            else:
                if x in queries[i + 1]:
                    distribute(y, x)
                else:
                    distribute(x, y)
        elif count[y] > count[x]:
            distribute(y, x)
        else:
            distribute(x, y)
    return '\n'.join(ans)


print(solve())

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

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

ans = []

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


def dfs(i):
    if min(d.values()) < 0:
        return

    if i == n:
        print("Yes")
        print(*ans, sep="\n")
        exit()

    for j in range(2):
        add = s[i][j]
        sub = s[i][1-j]
        d[add] += 1
        d[sub] -= 1
        ans.append(add)
        dfs(i + 1)
        d[add] -= 1
        d[sub] += 1
        ans.pop()


dfs(0)

print("No")

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

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

ans = []

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


def dfs(i):
    if min(d.values()) < 0:
        return

    if i == n:
        print("Yes")
        print(*ans, sep="\n")
        exit()

    k1, k2 = s[i]

    d[k1] += 1
    d[k2] -= 1
    ans.append(k1)
    dfs(i+1)
    d[k1] -= 1
    d[k2] += 1
    ans.pop()

    d[k2] += 1
    d[k1] -= 1
    ans.append(k2)
    dfs(i+1)
    d[k2] -= 1
    d[k1] += 1
    ans.pop()


dfs(0)

print("No")

Solution (Python):
import sys
import queue

q = queue.Queue()
sys.setrecursionlimit(10**7)
INF = 10**10
MOD = 10**9 + 7
S = []
history = []
N, A, B ,C = map(int,input().split())
d = {}
d['A'] = A
d['B'] = B
d['C'] = C

for n in range(N):
    s = input()
    S.append(s)

def select(L,R,x):
    diff = 1
    if L!=x:
        diff *= -1
    d[L] += diff
    d[R] -= diff
    history.append(x)

for n in range(N):
    L, R = S[n]
    if d[L] >= 2:
        select(L,R,R)
    elif d[L] == 0:
        if d[R] == 0:
            print ('No')
            exit()
        else:
            select(L,R,L)
    elif d[L] == 1:
        if d[R] >= 2:
            select(L,R,L)
        elif d[R] == 0:
            select(L,R,R)
        elif d[R] == 1:
            if n!=N-1:
                if L in S[n+1]:
                    select(L,R,L)
                else:
                    select(L,R,R)
            else:
                select(L,R,L) 
print ('Yes')
for h in history:
    print (h)

Solution (Python):
def add_i1():
	abc[i1]+=1
	abc[i2]-=1
	res[i]=i1
	return

def add_i2():
	abc[i1]-=1
	abc[i2]+=1
	res[i]=i2

n,*abc=map(int,input().split())
ip=[0]*n

for i in range(n):
	ip[i]=list(map(lambda x:ord(x)-ord("A"),list(input())))

ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		ABC=[abc[i1],abc[i2]]
		if ABC==[0,0]:
			ans="No"
		elif ABC==[0,1]:
			add_i1()
		else:
			add_i2()
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if [abc[i1],abc[i2]]==[0,0]:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]==0:
				add_i1()
			elif abc[i2]==0:
				add_i2()
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					add_i1()
				else:
					add_i2()
			else:
				add_i1()

dic={0:"A",1:"B",2:"C"}
print(ans)
if ans=="Yes":
	for i in range(n):
		print(dic[res[i]])

Solution (Python):
def add_i1():
	abc[i1]+=1
	abc[i2]-=1
	res[i]=i1
	return

def add_i2():
	abc[i1]-=1
	abc[i2]+=1
	res[i]=i2

n,*abc=map(int,input().split())
ip=[0]*n
dic={0:"A",1:"B",2:"C"}
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		ABC=[abc[i1],abc[i2]]
		if ABC==[0,0]:
			ans="No"
		elif ABC==[0,1]:
			add_i1()
		else:
			add_i2()
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if [abc[i1],abc[i2]]==[0,0]:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]==0:
				add_i1()
			elif abc[i2]==0:
				add_i2()
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					add_i1()
				else:
					add_i2()
			else:
				add_i1()
print(ans)
if ans=="Yes":
	for i in range(n):
		print(dic[res[i]])

Solution (Python):
def add_i1():
	abc[i1]+=1
	abc[i2]-=1
	res[i]=i1
	return

n,*abc=map(int,input().split())
ip=[0]*n
dic={0:"A",1:"B",2:"C"}
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		ABC=[abc[i1],abc[i2]]
		if ABC==[0,0]:
			ans="No"
		elif ABC==[0,1]:
			add_i1()
			'''
			abc[i1]+=1
			abc[i2]-=1
			res[i]=i1
			'''
		else:
			abc[i1]-=1
			abc[i2]+=1
			res[i]=i2
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if [abc[i1],abc[i2]]==[0,0]:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]==0:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
			elif abc[i2]==0:
				abc[i1]-=1
				abc[i2]+=1
				res[i]=i2
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			else:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
print(ans)
if ans=="Yes":
	for i in range(n):
		print(dic[res[i]])

Solution (Python):
n,*abc=map(int,input().split())
ip=[0]*n
dic={0:"A",1:"B",2:"C"}
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		ABC=[abc[i1],abc[i2]]
		if ABC==[0,0]:
			ans="No"
		elif ABC==[0,1]:
			abc[i1]+=1
			abc[i2]-=1
			res[i]=i1
		else:
			abc[i1]-=1
			abc[i2]+=1
			res[i]=i2
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if [abc[i1],abc[i2]]==[0,0]:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]==0:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
			elif abc[i2]==0:
				abc[i1]-=1
				abc[i2]+=1
				res[i]=i2
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			else:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
print(ans)
if ans=="Yes":
	for i in range(n):
		print(dic[res[i]])

Solution (Python):
n,*abc=map(int,input().split())
ip=[0]*n
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		ABC=[abc[i1],abc[i2]]
		if ABC==[0,0]:
			ans="No"
		elif ABC==[0,1]:
			abc[i1]+=1
			abc[i2]-=1
			res[i]=i1
		else:
			abc[i1]-=1
			abc[i2]+=1
			res[i]=i2
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if [abc[i1],abc[i2]]==[0,0]:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]*abc[i2]==0:
				if abc[i1]==0:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			else:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
print(ans)
dic={0:"A",1:"B",2:"C"}
if ans=="Yes":
	for i in range(n):
		print(dic[res[i]])
		'''
		if res[i]==0:
			print("A")
		elif res[i]==1:
			print("B")
		else:
			print("C")
		'''

Solution (Python):
n,*abc=map(int,input().split())
ip=[0]*n
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		ABC=[abc[i1],abc[i2]]
		if ABC==[0,0]:
			ans="No"
		elif ABC==[0,1]:
			abc[i1]+=1
			abc[i2]-=1
			res[i]=i1
		else:
			abc[i1]-=1
			abc[i2]+=1
			res[i]=i2
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if abc[i1]+abc[i2]==0:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]*abc[i2]==0:
				if abc[i1]==0:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			else:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
print(ans)
dic={0:"A",1:"B",2:"C"}
if ans=="Yes":
	for i in range(n):
		print(dic[res[i]])
		'''
		if res[i]==0:
			print("A")
		elif res[i]==1:
			print("B")
		else:
			print("C")
		'''

Solution (Python):
n,*abc=map(int,input().split())
ip=[0]*n
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		if abc[i1]+abc[i2]==0:
			ans="No"
		elif abc[i1]+abc[i2]==1:
			if abc[i1]==0:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
			else:
				abc[i1]-=1
				abc[i2]+=1
				res[i]=i2
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if abc[i1]+abc[i2]==0:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]*abc[i2]==0:
				if abc[i1]==0:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			else:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
print(ans)
dic={0:"A",1:"B",2:"C"}
if ans=="Yes":
	for i in range(n):
		'''
		if res[i]==0:
			print("A")
		elif res[i]==1:
			print("B")
		else:
			print("C")
		'''
		print(dic[res[i]])

Solution (Python):
n, a, b, c = map(int, input().split())
abc = list((a, b, c))
ans = [0] * n
dic = {'A': 0, 'B': 1, 'C': 2}
flag = True
s = [input() for i in range(n)]
for i in range(n):
    if abc[dic[s[i][0]]] == 0 and abc[dic[s[i][1]]] == 0:
        flag = False
        break
    elif sum(abc) == 2 and abc[dic[s[i][0]]] == 1 and abc[dic[s[i][1]]] == 1 and i != n - 1:
        ps = (set(s[i]) & set(s[i + 1])).pop()
        ms = (set(s[i]) - set(ps)).pop()
        abc[dic[ps]] += 1
        abc[dic[ms]] -= 1
        ans[i] = ps
    elif abc[dic[s[i][0]]] <= abc[dic[s[i][1]]]:
        abc[dic[s[i][0]]] += 1
        abc[dic[s[i][1]]] -= 1
        ans[i] = s[i][0]
    else:
        abc[dic[s[i][0]]] -= 1
        abc[dic[s[i][1]]] += 1
        ans[i] = s[i][1]
if flag:
    print('Yes')
    print(*ans, sep='\n')
else:
    print('No')

Solution (Python):
import sys
input = sys.stdin.readline

n, a, b, c = [int(x) for x in input().split()]
score = {}
score["A"] = a
score["B"] = b
score["C"] = c     
S = [input().rstrip() for _ in range(n)]
ans = []

for i in range(n - 1):
    s = S[i]
    next_s = S[i + 1]
    s1 = s[0]
    s2 = s[1]
    if score[s1] == 0 and score[s2] == 0:
        print("No")
        sys.exit()
    elif score[s1] == 0:
        ans.append(s1)
        score[s1] += 1
        score[s2] -= 1
    elif score[s2] == 0:
        ans.append(s2)
        score[s1] -= 1
        score[s2] += 1
    else:
        if next_s == s:
            ans.append(s1)
            score[s1] += 1
            score[s2] -= 1
        elif s1 in next_s:
            ans.append(s1)
            score[s1] += 1
            score[s2] -= 1
        else:
            ans.append(s2)
            score[s2] += 1
            score[s1] -= 1
s = S[-1]
s1 = s[0]
s2 = s[1]

if score[s1] == 0 and score[s2] == 0:
        print("No")
        sys.exit()
elif score[s1] == 0:
    ans.append(s1)
    score[s1] += 1
    score[s2] -= 1
elif score[s2] == 0:
    ans.append(s2)
    score[s1] -= 1
    score[s2] += 1
else:
    ans.append(s1)
print("Yes")
for i in ans:
    print(i)