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, av, bv, cv = list(map(int, input().split()))
s = []
for i in range(n):
    s.append(input())

v = {'A':av, 'B':bv, 'C':cv}

selection = []
for i in range(n):
    if v[s[i][0]] == 0:
        selection.append(s[i][0])
        v[s[i][0]] += 1
        v[s[i][1]] -= 1
    elif v[s[i][1]] == 0:
        selection.append(s[i][1])
        v[s[i][0]] -= 1
        v[s[i][1]] += 1
    elif v[s[i][0]] == 1 and v[s[i][1]] == 1:
        if i == n-1:
            selection.append(s[i][0])
            v[s[i][0]] += 1
            v[s[i][1]] -= 1       
        else:
            if s[i+1][0] == s[i][0] or s[i+1][1] == s[i][0]:
                selection.append(s[i][0])
                v[s[i][0]] += 1
                v[s[i][1]] -= 1 
            else:
                selection.append(s[i][1])
                v[s[i][0]] -= 1
                v[s[i][1]] += 1
    else:
        if v[s[i][0]] > v[s[i][1]]:
            selection.append(s[i][1])
            v[s[i][0]] -= 1
            v[s[i][1]] += 1
        else:
            selection.append(s[i][0])
            v[s[i][0]] += 1
            v[s[i][1]] -= 1
    
    if v['A'] < 0 or v['B'] < 0 or v['C'] < 0:
        print('No')
        break
else:
    print('Yes')
    for i in range(n):
        print(selection[i])

Solution (Python):
def play():
    N, A, B, C = map(int, input().split())
    S = [input() for i in range(N)]
    p = []
    for i in range(N):
        s = S[i]
        if s == "AB":
            if A + B == 0:
                print("No")
                return 0
            elif A == 1 and B == 1 and i != N-1:
                t = S[i+1]
                if t == "AC":
                    A += 1
                    B -= 1
                    p.append("A")
                else:
                    A -= 1
                    B += 1
                    p.append("B")
            elif A >= B:
                A -= 1
                B += 1
                p.append('B')
            else:
                A += 1
                B -= 1
                p.append('A')
        if s == "AC":
            if A + C == 0:
                print("No")
                return 0
            elif A == 1 and C == 1 and i != N-1:
                t = S[i+1]
                if t == "AB":
                    A += 1
                    C -= 1
                    p.append("A")
                else:
                    A -= 1
                    C += 1
                    p.append("C")
            elif A >= C:
                A -= 1
                C += 1
                p.append('C')
            else:
                A += 1
                C -= 1
                p.append('A')
        if s == "BC":
            if B + C == 0:
                print("No")
                return 0
            elif B == 1 and C == 1 and i != N-1:
                t = S[i+1]
                if t == "AB":
                    B += 1
                    C -= 1
                    p.append("B")
                else:
                    B -= 1
                    C += 1
                    p.append("C")
            elif B >= C:
                B -= 1
                C += 1
                p.append('C')
            else:
                B += 1
                C -= 1
                p.append('B')
    print('Yes')
    for i in range(N):
        print(p[i])
    return 0


if __name__ == '__main__':
    play()

Solution (Python):


import sys
sys.setrecursionlimit(300000)

YES = "Yes"  
NO = "No"  

def solve(N: int, A: int, B: int, C: int, S: "List[str]"):
    L = 'ABC'
    ll = []
    def rec(i, a, b, c):
        
        if i == N:
            return ll
        s = S[i]
        vals = [a, b, c]
        if s == 'AB':
            u, v = 0, 1
        if s == 'BC':
            u, v = 1, 2
        if s == 'AC':
            u, v = 0, 2

        if vals[u] == 0 and vals[v] == 0:
            if len(ll) > 0:
                ll.pop()
            return False
        elif vals[u] == 1 and vals[v] == 1:
            ll.append(L[u])
            vals[u] += 1
            vals[v] -= 1
            tmp = rec(i + 1, vals[0], vals[1], vals[2])
            if tmp:
                return tmp
            else:
                ll.append(L[v])
                vals[u] -= 2
                vals[v] += 2
                return rec(i + 1, vals[0], vals[1], vals[2])
        elif vals[u] == 0 or (vals[v] > 1 and vals[u] == 1):
            ll.append(L[u])
            vals[u] += 1
            vals[v] -= 1
            return rec(i + 1, vals[0], vals[1], vals[2])
        else:
            ll.append(L[v])
            vals[u] -= 1
            vals[v] += 1
            return rec(i + 1, vals[0], vals[1], vals[2])

    ret = rec(0, A, B, C)
    if ret:
        print('Yes')
        for c in ret:
            print(c)
        '''
        vals = [A, B, C]
        for i in range(N):
            x = S[i]
            c = ret[i]
            for d in x:
                k = L.find(d)
                if d == c:
                    vals[k] += 1
                else:
                    vals[k] -= 1
            print(x, c)
            print(vals)
        '''
    else:
        print('No')

    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):
N,A,B,C = map(int,input().split())



def s3(abc_dict):
    ans = []
    for _ in range(N):
        xy = input()
        x = xy[0] 
        y = xy[1]
        if abc_dict[x]==0 and abc_dict[y]==0:
            return False, []
        elif abc_dict[x]>=abc_dict[y]:
            abc_dict[x] -= 1
            abc_dict[y] += 1
            ans.append(y)
        else:
            abc_dict[y] -= 1
            abc_dict[x] += 1
            ans.append(x)
    return True, ans

def s2(abc_dict):

    ans = []
    flag = True
    q_list = []
    for _ in range(N):
        q_list.append(input())

    for i in range(N):
        xy = q_list[i]
        x = xy[0]
        y = xy[1]

        if (abc_dict[x]>=1 and abc_dict[y]==0):
            abc_dict[x]-=1
            abc_dict[y]+=1
            ans.append(y)
        elif (abc_dict[y]>=1 and abc_dict[x]==0):
            abc_dict[y]-=1
            abc_dict[x]+=1
            ans.append(x)
        elif  (abc_dict[x]==1 and abc_dict[y]==1):
            if i==N-1:
                ans.append(x)
            else:
                next_xy = q_list[i+1]
                if xy==next_xy: 
                    abc_dict[x]+=1
                    abc_dict[y]-=1
                    ans.append(x)
                elif (x in next_xy):
                    abc_dict[x]+=1
                    abc_dict[y]-=1
                    ans.append(x)
                else: 
                    abc_dict[y]+=1
                    abc_dict[x]-=1
                    ans.append(y)
        else:
            return False, []
    return True,ans

def s1(abc_dict):
    
    ans = []
    flag = True
    for _ in range(N):
        xy = input()
        x = xy[0] 
        y = xy[1]
        if abc_dict[x]==1:
            abc_dict[x]-=1
            abc_dict[y]+=1
            ans.append(y)
        elif abc_dict[y]==1:
            abc_dict[y]-=1
            abc_dict[x]+=1
            ans.append(x)
        else:
            flag = False
            return flag,[]
    return flag, ans
    
def s0(abc_dict):
    return False, []

abc_dict = {"A":A,"B":B,"C":C}
sumABC = A+B+C
if sumABC>=3:
    flag, ans = s3(abc_dict)
elif sumABC==2:
    flag, ans = s2(abc_dict)
elif sumABC==1:
    flag, ans = s1(abc_dict)
elif sumABC==0:
    flag, ans = False, []

if flag:
    print('Yes')
    for i in range(N):
        print(ans[i])
else:
    print("No")

Solution (Python):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))


n,a,b,c = mi()
s = []
for i in range(n):
    s.append(input())
z = a+ b+ c
flag = True
ans = []
if z != 2:
    for i in range(n):
        if s[i] == "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[i] == "BC":
            if c == 0 and b == 0:
                print("No")
                exit()
            if c > b:
                c -= 1
                b += 1
                ans.append("B")
            else:
                c += 1
                b -= 1
                ans.append("C")
        if s[i] == "AC":
            if c == 0 and a == 0:
                print("No")
                exit()
            if c > a:
                c -= 1
                a += 1
                ans.append("A")
            else:
                c += 1
                a -= 1
                ans.append("C")
    print("Yes")
    for l in range(n):
        print(ans[l])

else:
    for i in range(n):
        if s[i] == "AB":
            if a == 0 and b == 0:
                print("No")
                exit()
            if a > b:
                a -= 1
                b += 1
                ans.append("B")
            elif a == b:
                if i == n-1:
                    ans.append("B")
                elif s[i+1] == "BC":
                    a -= 1
                    b += 1
                    ans.append("B")
                else:
                    a += 1
                    b -= 1
                    ans.append("A")
            else:
                a += 1
                b -= 1
                ans.append("A")

        if s[i] == "BC":
            if c == 0 and b == 0:
                print("No")
                exit()
            if c > b:
                c -= 1
                b += 1
                ans.append("B")
            elif c == b:
                if i == n-1:
                    ans.append("B")
                elif s[i+1] == "AB":
                    c -= 1
                    b += 1
                    ans.append("B")
                else:
                    c += 1
                    b -= 1
                    ans.append("C")
            else:
                c += 1
                b -= 1
                ans.append("C")
        if s[i] == "AC":
            if c == 0 and a == 0:
                print("No")
                exit()
            if c > a:
                c -= 1
                a += 1
                ans.append("A")
            elif c == a:
                if i == (n-1):
                    ans.append("A")
                elif s[i+1] == "AB":
                    c -= 1
                    a += 1
                    ans.append("A")
                else:
                    c += 1
                    a -= 1
                    ans.append("C")
            else:
                c += 1
                a -= 1
                ans.append("C")
    print("Yes")
    for l in range(n):
        print(ans[l])

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

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

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

            
if ans == 'No':
    print(ans)
else:
    print(ans)
    for key in q:
        print(key)

Solution (Python):
n,a,b,c = map(int, input().split())
ans = []
ans_ = "Yes"
ss = [0]
sss = {}
for i in range(n):
  s = str(input())
  ss.append(s)
  sss[i] = s
sss[n] = "a"
for num,s in enumerate(ss):
  if num == 0:
    continue
  else:
    if s == "AB":
      if a == 0 and b == 0:
        ans_ = "No"
        break
      if a > b:
        a -= 1
        b += 1
        ans.append("B")
      elif a < b:
        a += 1
        b -= 1
        ans.append("A")
      else:
        if sss[num] == "BC":
          a -= 1
          b += 1
          ans.append("B")
        else:
          a += 1
          b -= 1
          ans.append("A")
    if s == "BC":
      if c == 0 and b == 0:
        ans_ = "No"
        break
      if c > b:
        c -= 1
        b += 1
        ans.append("B")
      elif c < b:
        c += 1
        b -= 1
        ans.append("C")
      else:
        if sss[num] == "AB":
          c -= 1
          b += 1
          ans.append("B")
        else:
          c += 1
          b -= 1
          ans.append("C") 
    if s == "AC":
      if a == 0 and c == 0:
        ans_ = "No"
        break
      if a > c:
        a -= 1
        c += 1
        ans.append("C")
      elif a < c:
        a += 1
        c -= 1
        ans.append("A")
      else:
        if sss[num] == "BC":
          a -= 1
          c += 1
          ans.append("C")
        else:
          a += 1
          c -= 1
          ans.append("A")
if ans_ == "No":
   print(ans_)
else:
  print(ans_)
  for i in ans:
    print(i)

Solution (Python):
n, a, b, c = map(int, input().split())
s = []
for i in range(n):
    s.append(input())
temp = []
flag = True
for i in range(n):
    if s[i] == "AB":
        if a == 0 and b == 0:
            flag = False
            break
        elif a == 0:
            a += 1
            b -= 1
            temp.append("A")
        elif b == 0:
            b += 1
            a -= 1
            temp.append("B")
        else:
            if i == n - 1:
                temp.append("A")
            else:
                if s[i + 1] == "BC":
                    b += 1
                    a -= 1
                    temp.append("B")
                elif s[i + 1] == "AC":
                    a += 1
                    b -= 1
                    temp.append("A")
                else:
                    if a > b:
                        a -= 1
                        b += 1
                        temp.append("B")
                    else:
                        a += 1
                        b -= 1
                        temp.append("A")
    elif s[i] == "BC":
        if c == 0 and b == 0:
            flag = False
            break
        elif c == 0:
            c += 1
            b -= 1
            temp.append("C")
        elif b == 0:
            b += 1
            c -= 1
            temp.append("B")
        else:
            if i == n - 1:
                temp.append("C")
            else:
                if s[i + 1] == "AB":
                    b += 1
                    c -= 1
                    temp.append("B")
                elif s[i + 1] == "AC":
                    c += 1
                    b -= 1
                    temp.append("C")
                else:
                    if c > b:
                        c -= 1
                        b += 1
                        temp.append("B")
                    else:
                        c += 1
                        b -= 1
                        temp.append("C")
    elif s[i] == "AC":
        if a == 0 and c == 0:
            flag = False
            break
        elif a == 0:
            a += 1
            c -= 1
            temp.append("A")
        elif c == 0:
            c += 1
            a -= 1
            temp.append("C")
        else:
            if i == n - 1:
                temp.append("A")
            else:
                if s[i + 1] == "BC":
                    c += 1
                    a -= 1
                    temp.append("C")
                elif s[i + 1] == "AB":
                    a += 1
                    c -= 1
                    temp.append("A")
                else:
                    if a > c:
                        a -= 1
                        c += 1
                        temp.append("C")
                    else:
                        a += 1
                        c -= 1
                        temp.append("A")
if flag == False:
    print("No")
else:
    print("Yes")
    for i in temp:
        print(i)

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

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

AB, AC, BC = 'AB', 'AC', 'BC'

n0 = [A, B, C].count(0)

F = [A, B, C]
H = ['A', 'B', 'C']
G = {AB: (0, 1), BC:(1,2), AC:(0, 2)}

if n0 == 3:
    print('No')
elif n0 == 2 and sum([A, B, C]) == 1:
    path = []
    for com in S:
        a,b = G[com]
        if F[a] == F[b] == 0:
            print('No')
            exit(0)
        else:
            if F[a] == 1:
                a,b = b,a
            path.append(H[a])
            F[a] += 1
            F[b] -= 1
    print('Yes')
    for p in path:
        print(p)
else:
    if (S[0] == AB and A == 0 and B == 0) or \
     (S[0] == AC and A == 0 and C == 0) or (S[0] == BC and B == 0 and C == 0):
        print('No')
        exit(0)

    path = []
    for i,com in enumerate(S):
        a,b = G[com]
        if F[a] == 0 or F[b] == 0:
            if F[b] == 0:
                a,b = b,a
            path.append(H[a])
            F[a] += 1
            F[b] -= 1
        else:
            if i < len(S) - 1 and F[a] == F[b]:
                if b in G[S[i + 1]]:
                    a, b = b, a
                path.append(H[a])
                F[a] += 1
                F[b] -= 1
            else:
                if F[a] > F[b]:
                    a,b = b,a
                path.append(H[a])
                F[a] += 1
                F[b] -= 1
        assert all(v >= 0 for v in F)
    print('Yes')
    for p in path:
        print(p)

Solution (Python):
def pattern_100():
    pass


def pattern_200_110():
    pass


n, a, b, c = map(int, input().split())
abc = [a, b, c]
total = sum(abc)
operate_pattern = [[0, 1], [1, 2], [2, 0]]
s = []
for i in range(n):
    tmp = input()
    if tmp == 'AB':
        s.append(0)
    elif tmp == 'AC':
        s.append(2)
    else:
        s.append(1)
answer = []
flag = True
for i in range(n):
    op = operate_pattern[s[i]]
    if abc[op[0]] ==1 and abc[op[1]] ==1:
        if i==n-1:
            answer.append(op[0])
        else:
            if (s[i]+1)%3 ==s[i+1]:
                answer.append(op[1])
                abc[op[1]] +=1
                abc[op[0]] -=1
            else:
                answer.append(op[0])
                abc[op[0]] += 1
                abc[op[1]] -= 1
    elif abc[op[0]] == 0:
        if abc[op[1]] == 0:
            flag =False
            break
        else:
            answer.append(op[0])
            abc[op[0]] +=1
            abc[op[1]] -=1
    elif abc[op[1]] == 0:
        if abc[op[0]] == 0:
            flag =False
            break
        else:
            answer.append(op[1])
            abc[op[1]] +=1
            abc[op[0]] -=1
    else:
        if abc[op[0]] > abc[op[1]]:
            answer.append(op[1])
            abc[op[1]] +=1
            abc[op[0]] -=1
        else:
            answer.append(op[0])
            abc[op[0]] +=1
            abc[op[1]] -=1

if flag:
    print('Yes')
    for i in answer:
        if i == 0:
            print('A')
        elif i == 1:
            print('B')
        else:
            print('C')
else:
    print('No')

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

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

def add(c, comb):
    global A, B, C
    if comb == 'AB':
        if c == 'A':
            ans.append('A')
            A += 1
            B -= 1
        else:
            ans.append('B')
            A -= 1
            B += 1
    if comb == 'BC':
        if c == 'B':
            ans.append('B')
            B += 1
            C -= 1
        else:
            ans.append('C')
            B -= 1
            C += 1
    if comb == 'AC':
        if c == 'A':
            ans.append('A')
            A += 1
            C -= 1
        else:
            ans.append('C')
            A -= 1
            C += 1

for i in range(N):
    if s[i] == 'AB':
        if A == B == 0:
            print('No')
            exit()
        if i < N - 1 and A == B == 1 and C == 0 and s[i+1] != 'AB':
            if s[i+1] == 'BC':
                add('B', 'AB')
            else:
                add('A', 'AB')
        else:
            if A > B:
                add('B', 'AB')
            else:
                add('A', 'AB')
    if s[i] == 'BC':
        if B == C == 0:
            print('No')
            exit()
        if i < N - 1 and B == C == 1 and A == 0 and s[i+1] != 'BC':
            if s[i+1] == 'AB':
                add('B', 'BC')
            else:
                add('C', 'BC')
        else:
            if B > C:
                add('C', 'BC')
            else:
                add('B', 'BC')
    if s[i] == 'AC':
        if A == C == 0:
            print('No')
            exit()
        if i < N - 1 and A == C == 1 and B == 0 and s[i+1] != 'AC':
            if s[i+1] == 'AB':
                add('A', 'AC')
            else:
                add('C', 'AC')
        else:
            if A > C:
                add('C', 'AC')
            else:
                add('A', 'AC')
print('Yes')
print(*ans, sep='\n')

Solution (Python):
N, A, B, C = map(int, input().split())
ans = "Yes\n"
def step(add, remove):
    global A
    global B
    global C
    global ans
    ans += add
    ans += "\n"
    if add == "A":
        A += 1
    elif add == "B":
        B += 1
    else:
        C += 1
    if remove == "A":
        A -= 1
    elif remove == "B":
        B -= 1
    else:
        C -= 1

def getvalue(variable):
    global A
    global B
    global C
    if variable == "A":
        return A
    elif variable == "B":
        return B
    else:
        return C

res = ""
for _ in range(N):
    act = input()
    if A == 0 and B == 0 and C == 0:
        ans = "No"
        res = ""
        break
    elif A != 0 and B != 0 and C != 0:
        step(act[0], act[1])
    elif ((A == 0) != (B == 0)) != (C == 0):
        if res == "":
            if getvalue(act[1]) == 0:
                step(act[1], act[0])
            elif getvalue(act[0]) == 0 :
                step(act[0], act[1])
            else:
                res = act
        else:
            if res[0] in act:
                i = (act.index(res[0])+1)%2
                step(res[0], res[1])
                step(act[i], res[0])
                res = ""
            else:
                i = (act.index(res[1])+1)%2
                step(res[1], res[0])
                step(act[i], res[1])
                res = ""
    else:
        if getvalue(act[0]) != 0:
            step(act[1], act[0])
        elif getvalue(act[1]) != 0:
            step(act[0], act[1])
        else:
            ans = "No"
            res = ""
            break
if res != "":
    step(res[0], res[1])
    res = ""
if ans[-1] == "\n":
    ans = ans[:-1]
print(ans)

Solution (Python):
def task(n, a, b, c, s):
    choices = []

    for i in range(0, n):
        seta = 'A' in s[i]
        setb = 'B' in s[i]
        setc = 'C' in s[i]

        picka = seta
        pickb = setb
        pickc = setc

        if seta and a - 1 < 0:
            pickb = False
            pickc = False

        if setb and b - 1 < 0:
            picka = False
            pickc = False

        if setc and c - 1 < 0:
            picka = False
            pickb = False

        nextseta = True
        nextsetb = True
        nextsetc = True

        if i < n - 1:
            nextseta = 'A' in s[i + 1]
            nextsetb = 'B' in s[i + 1]
            nextsetc = 'C' in s[i + 1]

        if picka and nextseta:
            pickb = False
            pickc = False
        elif pickb and nextsetb:
            picka = False
            pickc = False
        elif pickc and nextsetc:
            picka = False
            pickb = False

        if seta and picka:
            choices.append('A')
            a += 1
            if setb:
                b -= 1
            if setc:
                c -= 1
        elif setb and pickb:
            choices.append('B')
            b += 1
            if seta:
                a -= 1
            if setc:
                c -= 1
        elif setc and pickc:
            choices.append('C')
            c += 1
            if seta:
                a -= 1
            if setb:
                b -= 1
        else:
            print('No')
            return

    print('Yes')
    for choice in choices:
        print(choice)


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

task(n, a, b, c, s)

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

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

ans = []
for i in range(n):
    command = s[i]
    if command == 'AB':
        if a < b:
            ans.append('A')
            a += 1
            b -= 1
        elif a*b == 1 and i != n-1:
            c_tmp = s[i+1]
            if c_tmp == 'AC':
                ans.append('A')
                a += 1
                b -= 1
            else:
                ans.append('B')
                b += 1
                a -= 1
        else:
            ans.append('B')
            b += 1
            a -= 1

    elif command == 'AC':
        if a < c:
            ans.append('A')
            a += 1
            c -= 1
        elif a*c == 1 and i != n-1:
            c_tmp = s[i+1]
            if c_tmp == 'AB':
                ans.append('A')
                a += 1
                c -= 1
            else:
                ans.append('C')
                c += 1
                a -= 1
        else:
            ans.append('C')
            c += 1
            a -= 1
    else:
        if b < c:
            ans.append('B')
            b += 1
            c -= 1
        elif b*c == 1 and i != n-1:
            c_tmp = s[i+1]
            if c_tmp == 'AB':
                ans.append('B')
                b += 1
                c -= 1
            else:
                ans.append('C')
                c += 1
                b -= 1
        else:
            ans.append('C')
            c += 1
            b -= 1
    if a < 0 or b < 0 or c < 0:
        print('No')
        exit()
print('Yes')
for i in range(n):
    print(ans[i])