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



Sample input:
2 1 3 0
AB
AC


Sample output:
Yes
A
C

Submission status: Accepted

Solution code:
import sys
N, A, B, C = map(int, input().split())
d=dict()
d["A"] = A
d["B"] = B
d["C"] = C
h = {"AB":"C","BC":"A","AC":"B"}
S = []
for _ in range(N):
    S.append(input().strip())
if A+B+C == 0:
    print("No")
elif A+B+C == 1:
    R = []
    for k in range(N):
        s = S[k]
        for i in range(2):
            if d[s[i]] == 1:
                R.append(s[1-i])
                d[s[i]] = 0
                d[s[1-i]] = 1
                break
        else:
            print("No")
            break
    else:
        print("Yes")
        for i in range(N):
            print(R[i])
else:
    R = []
    if d[S[0][0]] == 0 and d[S[0][1]] == 0:
        print("No")
        sys.exit()
    for i in range(N-1):
        s = S[i]
        if d[s[0]] == 1 and d[s[1]] == 1 and d[h[s]] == 0:
            if S[i+1] == s:
                d[s[0]] += 1
                d[s[1]] -= 1
                R.append(s[0])
            else:
                for j in range(2):
                    if s[j] in S[i+1]:
                        d[s[j]] += 1
                        d[s[1-j]] -= 1
                        R.append(s[j])
                        break
        elif d[s[0]] >= d[s[1]]:
            d[s[0]] -= 1
            d[s[1]] += 1
            R.append(s[1])
        else:
            d[s[0]] += 1
            d[s[1]] -= 1
            R.append(s[0])
    for i in range(2):
        if d[S[N-1][i]] > 0:
            R.append(S[N-1][1-i])
            break
    print("Yes")
    for i in range(N):
        print(R[i])

Submission status: Accepted

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

N, A, B, C = list(map(int, input().split()))
s = [0] * (N+1)

for i in range(N):
    s[i] = input()
s[N] = 'AB'

sum = A + B + C
if sum != 2:
    ans = [0] * N
    for i in range(N):
        if s[i] == 'AB':
            if A > B:
                A -= 1
                B += 1
                ans[i] = 'B'
            else:
                A += 1
                B -= 1
                ans[i] = 'A'
        elif s[i] == 'AC':
            if A > C:
                A -= 1
                C += 1
                ans[i] = 'C'
            else:
                A += 1
                C -= 1
                ans[i] = 'A'
        elif s[i] == 'BC':
            if B > C:
                B -= 1
                C += 1
                ans[i] = 'C'
            else:
                B += 1
                C -= 1
                ans[i] = 'B'
        if A < 0 or B < 0 or C < 0:
            print('No')
            sys.exit()
    print('Yes')
    for i in range(N):
        print(ans[i])
    sys.exit()


ans = [0] * N
for i in range(N):
    if s[i] == 'AB':
        if A > B:
            A -= 1
            B += 1
            ans[i] = 'B'
        elif A < B:
            A += 1
            B -= 1
            ans[i] = 'A'
        else:
            if s[i+1] == 'BC':
                A -= 1
                B += 1
                ans[i] = 'B'
            else:
                A += 1
                B -= 1
                ans[i] = 'A'
    elif s[i] == 'AC':
        if A > C:
            A -= 1
            C += 1
            ans[i] = 'C'
        elif A < C:
            A += 1
            C -= 1
            ans[i] = 'A'
        else:
            if s[i+1] == 'AB':
                A += 1
                C -= 1
                ans[i] = 'A'
            else:
                A -= 1
                C += 1
                ans[i] = 'C'
    elif s[i] == 'BC':
        if B > C:
            B -= 1
            C += 1
            ans[i] = 'C'
        elif B < C:
            B += 1
            C -= 1
            ans[i] = 'B'
        else:
            if s[i+1] == 'AB':
                B += 1
                C -= 1
                ans[i] = 'B'
            else:
                B -= 1
                C += 1
                ans[i] = 'C'

    if A < 0 or B < 0 or C < 0:
        print('No')
        sys.exit()
print('Yes')
for i in range(N):
    print(ans[i])

Submission status: Accepted

Solution code:
n,a,b,c=map(int,input().split())
arr=[input() for _ in range(n)]
if (a==0 and b==0) or (a==0 and c==0) or (b==0 and c==0):
  ans=[]
  for i in range(n):
    if arr[i]=='AB':
      if a==0 and b==0:
        print('No')
        break
      else:
        if a>b:
          a-=1
          b+=1
          ans.append('B')
        elif a<b:
          a+=1
          b-=1
          ans.append('A')
        elif a==b:
          if i==n-1:
            a+=1
            b-=1
            ans.append('A')
          else:
            if arr[i+1]=='AB':
              a+=1
              b-=1
              ans.append('A')
            elif arr[i+1]=='AC':
              a+=1
              b-=1
              ans.append('A')
            elif arr[i+1]=='BC':
              a-=1
              b+=1
              ans.append('B')
    elif arr[i]=='AC':
      if a==0 and c==0:
        print('No')
        break
      else:
        if a>c:
          a-=1
          c+=1
          ans.append('C')
        elif a<c:
          a+=1
          c-=1
          ans.append('A')
        elif a==c:
          if i==n-1:
            a+=1
            c-=1
            ans.append('A')
          else:
            if arr[i+1]=='AC':
              a+=1
              c-=1
              ans.append('A')
            elif arr[i+1]=='AB':
              a+=1
              c-=1
              ans.append('A')
            elif arr[i+1]=='BC':
              a-=1
              c+=1
              ans.append('C')
    elif arr[i]=='BC':
      if b==0 and c==0:
        print('No')
        break
      else:
        if b>c:
          b-=1
          c+=1
          ans.append('C')
        elif b<c:
          b+=1
          c-=1
          ans.append('B')
        elif b==c:
          if i==n-1:
            b+=1
            c-=1
            ans.append('B')
          else:
            if arr[i+1]=='BC':
              b+=1
              c-=1
              ans.append('B')
            elif arr[i+1]=='AB':
              b+=1
              c-=1
              ans.append('B')
            elif arr[i+1]=='AC':
              b-=1
              c+=1
              ans.append('C')
  else:
    print('Yes')
    for i in range(n):
      print(ans[i])
else:
  print('Yes')
  ans=[]
  for i in range(n):
    if arr[i]=='AB':
      if a>b:
        a-=1
        b+=1
        ans.append('B')
      elif a<b:
        a+=1
        b-=1
        ans.append('A')
      elif a==b:
        if i==n-1:
          a+=1
          b-=1
          ans.append('A')
        else:
          if arr[i+1]=='AB':
            a+=1
            b-=1
            ans.append('A')
          elif arr[i+1]=='AC':
            a+=1
            b-=1
            ans.append('A')
          elif arr[i+1]=='BC':
            a-=1
            b+=1
            ans.append('B')
    elif arr[i]=='AC':
      if a>c:
        a-=1
        c+=1
        ans.append('C')
      elif a<c:
        a+=1
        c-=1
        ans.append('A')
      elif a==c:
        if i==n-1:
          a+=1
          c-=1
          ans.append('A')
        else:
          if arr[i+1]=='AC':
            a+=1
            c-=1
            ans.append('A')
          elif arr[i+1]=='AB':
            a+=1
            c-=1
            ans.append('A')
          elif arr[i+1]=='BC':
            a-=1
            c+=1
            ans.append('C')
    elif arr[i]=='BC':
      if b>c:
        b-=1
        c+=1
        ans.append('C')
      elif b<c:
        b+=1
        c-=1
        ans.append('B')
      elif b==c:
        if i==n-1:
          b+=1
          c-=1
          ans.append('B')
        else:
          if arr[i+1]=='BC':
            b+=1
            c-=1
            ans.append('B')
          elif arr[i+1]=='AB':
            b+=1
            c-=1
            ans.append('B')
          elif arr[i+1]=='AC':
            b-=1
            c+=1
            ans.append('C')
  for i in range(n):
    print(ans[i])

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
move = []
for i in range(N):
    new = input()
    move.append(new)
    
flag = 1
path = []
for i in range(N):
    if move[i] == "AB":
        if B==0:
            if A==0:
                flag=0
                break
            elif i < N-1:
                if move[i+1] == "AC" and A==1 and C==0:
                    flag=0
                    break
                else:
                    B += 1
                    A -= 1
                    path.append("B")
            else:
                B += 1
                A -= 1
                path.append("B")
        elif A==0:
            if B==0:
                flag=0
                break
            elif i < N-1:
                if move[i+1] == "BC" and B==1 and C==0:
                    flag=0
                    break
                else:
                    A += 1
                    B -= 1
                    path.append("A")
            else:
                A += 1
                B -= 1
                path.append("A")
        else:
            if i < N-1:
                if move[i+1] == "BC" and B==1 and C==0:
                    B += 1
                    A -= 1
                    path.append("B")
                else:
                    A += 1
                    B -= 1
                    path.append("A")
            else:
                A += 1
                B -= 1
                path.append("A")
    elif move[i] == "AC":
        if C==0:
            if A==0:
                flag=0
                break
            elif i < N-1:
                if move[i+1] == "AB" and A==1 and B==0:
                    flag=0
                    break
                else:
                    C += 1
                    A -= 1
                    path.append("C")
            else:
                C += 1
                A -= 1
                path.append("C")
        elif A==0:
            if C==0:
                flag=0
                break
            elif i < N-1:
                if move[i+1] == "BC" and B==0 and C==1:
                    flag=0
                    break
                else:
                    A += 1
                    C -= 1
                    path.append("A")
            else:
                A += 1
                C -= 1
                path.append("A")
        else:
            if i < N-1:
                if move[i+1] == "BC" and B==0 and C==1:
                    C += 1
                    A -= 1
                    path.append("C")
                else:
                    A += 1
                    C -= 1
                    path.append("A")
            else:
                A += 1
                C -= 1
                path.append("A")
    else:
        if B==0:
            if C==0:
                flag=0
                break
            elif i < N-1:
                if move[i+1] == "AC" and A==0 and C==1:
                    flag=0
                    break
                else:
                    B += 1
                    C -= 1
                    path.append("B")
            else:
                B += 1
                C -= 1
                path.append("B")
        elif C==0:
            if B==0:
                flag=0
                break
            elif i < N-1:
                if move[i+1] == "AB" and B==1 and A==0:
                    flag=0
                    break
                else:
                    C += 1
                    B -= 1
                    path.append("C")
            else:
                C += 1
                B -= 1
                path.append("C")
        else:
            if i < N-1:
                if move[i+1] == "AB" and A==0 and B==1:
                    B += 1
                    C -= 1
                    path.append("B")
                else:
                    C += 1
                    B -= 1
                    path.append("C")
            else:
                C += 1
                B -= 1
                path.append("C")  
    if flag==0:
        break
    #print(A,B,C)
                
if flag == 0:
    print("No")
else:
    print("Yes")
    for p in path:
        print(p)

Submission status: Accepted

Solution code:
N,A,B,C = map(int,input().split())
q = [input() for i in range(N)]
d = {'A':A,'B':B,'C':C}
ans = []
possible = True
for i in range(N):
    x = q[i][0]
    y = q[i][1]
    z = [s for s in 'ABC' if s!=x and s!=y][0]
    if d[x]>d[y]:
        ans.append(y)
        d[x] -= 1
        d[y] += 1
    elif d[x]<d[y]:
        ans.append(x)
        d[x] += 1
        d[y] -= 1
    else:
        if d[x]==1 and d[y]==1 and d[z]==0:
            if i==N-1 or q[i+1]==q[i]:
                ans.append(x)
                d[x] += 1
                d[y] -= 1
            elif x in q[i+1]:
                ans.append(x)
                d[x] += 1
                d[y] -= 1
            else:
                ans.append(y)
                d[y] += 1
                d[x] -= 1
        elif d[x]==0 and d[y]==0:
            possible = False
        else:
            ans.append(x)
            d[x] += 1
            d[y] -= 1

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

Submission status: Accepted

Solution code:
n, a, b, c = map(int, input().split())
s = [input() for _ in range(n)]
ans = []
flg = True
def adding(x, y):
    x += 1
    y -= 1

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

    if s[i] == 'AC':
        if a == c:
            if i < n - 1:
                if s[i + 1] == 'AB':
                    a += 1
                    c -= 1
                    ans.append('A')
                elif s[i + 1] == 'BC':
                    c += 1
                    a -= 1
                    ans.append('C')
                else:
                    a += 1
                    c -= 1
                    ans.append('A')
            else:
                a += 1
                c -= 1
                ans.append('A')
        elif a < c:
            a += 1
            c -= 1
            ans.append('A')
        else:
            c += 1
            a -= 1
            ans.append('C')

    if s[i] == 'BC':
        if b == c:
            if i < n - 1:
                if s[i + 1] == 'AB':
                    b += 1
                    c -= 1
                    ans.append('B')
                elif s[i + 1] == 'AC':
                    c += 1
                    b -= 1
                    ans.append('C')
                else:
                    b += 1
                    c -= 1
                    ans.append('B')
            else:
                b += 1
                c -= 1
                ans.append('B')
        elif b < c:
            b += 1
            c -= 1
            ans.append('B')
        else:
            c += 1
            b -= 1
            ans.append('C')

    #print(a, b, c)
    if a < 0 or b < 0 or c < 0:
        flg = False
        break

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

else:
    print('No')

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))

from collections import defaultdict, Counter
from sys import exit
import math
# def seni(dp, a)


def main():
    n, a, b, c = getList()
    a = min(a,2)
    b = min(b, 2)
    c = min(c, 2)
    dp = [[0 for i in range(28)] for j in range(n+1)]
    dp[0][a*9 + b * 3 + c] = 1
    for i in range(n):
        q = input().strip()
        if q == "AB":
            # A->B
            for na in range(1, 3):
                for nb in range(3):
                    for nc in range(3):
                        if dp[i][na * 9 + nb * 3 + nc] != 0:
                            dp[i+1][(na-1) * 9 + min(nb+1, 2) * 3 + nc] = ["AB", na * 9 + nb * 3 + nc]

            for nb in range(1, 3):
                for na in range(3):
                    for nc in range(3):
                        if dp[i][na * 9 + nb * 3 + nc] != 0:
                            dp[i+1][min(na+1, 2) * 9 + (nb-1) * 3 + nc] = ["BA", na * 9 + nb * 3 + nc]

        if q == "BC":
            # B C
            for nb in range(1, 3):
                for na in range(3):
                    for nc in range(3):
                        if dp[i][na * 9 + nb * 3 + nc] != 0:
                            dp[i+1][na * 9 + (nb-1) * 3 + min(nc+1, 2)] = ["BC", na * 9 + nb * 3 + nc]

            for nc in range(1, 3):
                for na in range(3):
                    for nb in range(3):
                        if dp[i][na * 9 + nb * 3 + nc] != 0:
                            dp[i+1][na * 9 + min(nb+1, 2) * 3 + nc-1] = ["CB", na * 9 + nb * 3 + nc]

        if q == "AC":
            # A C
            for na in range(1, 3):
                for nb in range(3):
                    for nc in range(3):
                        if dp[i][na * 9 + nb * 3 + nc] != 0:
                            dp[i+1][(na-1) * 9 + nb * 3 + min(nc + 1, 2)] = ["AC", na * 9 + nb * 3 + nc]

            for nc in range(1, 3):
                for na in range(3):
                    for nb in range(3):
                        if dp[i][na * 9 + nb * 3 + nc] != 0:
                            dp[i+1][min(na+1, 2) * 9 + nb * 3 + nc - 1] = ["CA", na * 9 + nb * 3 + nc]


    # for d in dp:
    #     print(d)

    for i, tgt in enumerate(dp[-1]):
        if tgt != 0:
            ans = hukugen(dp, i, n)
            print("Yes")
            for an in ans:
                print(an)

            return


    print("No")

def hukugen(dp, cur, n):
    ans = []
    taiou = {"A": 9, "B": 3, "C":1}
    for i in range(n):
        moji = dp[n-i][cur][0]
        # print(moji, dp[n-i], cur)
        ans.append(moji[1])
        cur =dp[n-i][cur][1]
        # cur -= taiou[moji[1]]

    return (list(reversed(ans)))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n,a,b,c  = map(int, input().split())
Ss = [input() for i in range(n)]
ans = []
flg = True
for i,S in enumerate(Ss):
    if S == "AB":
        if a == b == 0:
            flg = False
        elif a == b == 1 and i < len(Ss)-1:
            if Ss[i+1] == "AC":
                a += 1
                b -= 1
                ans.append("A")
            else:
                a -= 1
                b += 1
                ans.append("B")
        elif a < b:
            a += 1
            b -= 1
            ans.append("A")
        else:
            a -= 1
            b += 1
            ans.append("B")
    elif S == "AC":
        if a == c == 0:
            flg = False
        elif a == c == 1 and i<len(Ss)-1:
            if Ss[i+1] == "BC":
                a -= 1
                c += 1
                ans.append("C")
            else:
                a += 1
                c -= 1
                ans.append("A")

        elif a < c:
            a += 1
            c -= 1
            ans.append("A")
        else:
            a -= 1
            c += 1
            ans.append("C")
    elif S == "BC":
        if b == c == 0:
            flg = False
        elif b == c == 1 and i < len(Ss)-1:
            if Ss[i+1] == "AC":
                c += 1
                b -= 1
                ans.append("C")
            else:
                c -= 1
                b += 1
                ans.append("B")
        elif b < c:
            b += 1
            c -= 1
            ans.append("B")
        else:
            b -= 1
            c += 1
            ans.append("C")
            
if flg: 
    print("Yes")
    print(*ans)
else:
    print("No")

Submission status: Accepted

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

res_list = []
flag = 1

for i in range(n):
    if flag == 0:
        break
    if ss[i] == 'AB':
        if a == 0 and b == 0:
            flag = 0
        elif a == 0:
            a, b = 1, b-1
            res_list.append('A')
        elif b == 0:
            a, b = a-1, 1
            res_list.append('B')
        elif a == 1 and b == 1:
            if i == n-1:
                res_list.append('A')
            elif ss[i+1] == 'BC':
                res_list.append('B')
                a, b = 0, 2
            elif ss[i+1] == 'AC':
                res_list.append('A')
                a, b = 2, 0
            else:
                res_list.append('A')
                a, b = 2, 0
        elif a == 1:
            res_list.append('A')
            a, b = 2, b-1
        elif b == 1:
            res_list.append('B')
            a, b = a-1, 2
        else:
            res_list.append('A')
            a, b = a+1, b-1
    elif ss[i] == 'BC':
        if b == 0 and c == 0:
            flag = 0
        elif b == 0:
            b, c = 1, c-1
            res_list.append('B')
        elif c == 0:
            b, c = b-1, 1
            res_list.append('C')
        elif b == 1 and c == 1:
            if i == n-1:
                res_list.append('B')
            elif ss[i+1] == 'AB':
                res_list.append('B')
                b, c = 2, 0
            elif ss[i+1] == 'AC':
                res_list.append('C')
                b, c = 0, 2
            else:
                res_list.append('B')
                b, c = 2, 0
        elif b == 1:
            res_list.append('B')
            b, c = 2, c-1
        elif c == 1:
            res_list.append('C')
            b, c = b-1, 2
        else:
            res_list.append('B')
            b, c = b+1, c-1
    else:
        if a == 0 and c == 0:
            flag = 0
        elif a == 0:
            a, c = 1, c-1
            res_list.append('A')
        elif c == 0:
            a, c = a-1, 1
            res_list.append('C')
        elif a == 1 and c == 1:
            if i == n-1:
                res_list.append('A')
            elif ss[i+1] == 'AB':
                res_list.append('A')
                a, c = 2, 0
            elif ss[i+1] == 'BC':
                res_list.append('C')
                a, c = 0, 2
            else:
                res_list.append('A')
                a, c = 2, 0
        elif a == 1:
            res_list.append('A')
            a, c = 2, c-1
        elif c == 1:
            res_list.append('C')
            a, c = a-1, 2
        else:
            res_list.append('A')
            a, c = a+1, c-1

if flag == 0:
    print('No')
else:
    print('Yes')
    for res in res_list:
        print(res)

Submission status: Accepted

Solution code:
n,a,b,c = map(int,input().split())
ans = []
s = [input() for i in range(n)]
for i in range(n):
    
    if s[i] == "AB":
        if a > b:
            a -= 1
            b += 1
            ans.append("B")
        elif a==b:
            if i < n-1:
                if s[i+1] == "AB":
                    a -= 1
                    b += 1
                    ans.append("B")
                elif s[i+1] == "AC":
                    a += 1
                    b -= 1
                    ans.append("A")
                else:
                    a -= 1
                    b += 1
                    ans.append("B")
            else:
                a -= 1
                b += 1
                ans.append("B")

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

Submission status: Accepted

Solution code:
N,A,B,C = map(int,input().split())
def sum3(dict_):
    ans = []
    for _ in range(N):
        xy = input()
        x = xy[0] 
        y = xy[1]
        if dict_[x]==0 and dict_[y]==0:
            return False, []
        elif dict_[x]>=dict_[y]:
            dict_[x] -= 1
            dict_[y] += 1
            ans.append(y)
        else:
            dict_[y] -= 1
            dict_[x] += 1
            ans.append(x)
    return True, ans
def sum2(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 (dict_[x]>=1 and dict_[y]==0):
            dict_[x]-=1
            dict_[y]+=1
            ans.append(y)
        elif (dict_[y]>=1 and dict_[x]==0):
            dict_[y]-=1
            dict_[x]+=1
            ans.append(x)
        elif  (dict_[x]==1 and dict_[y]==1):
            if i==N-1:
                ans.append(x)
            else:
                next_xy = q_list[i+1]
                if xy==next_xy: 
                    dict_[x]+=1
                    dict_[y]-=1
                    ans.append(x)
                elif (x in next_xy):
                    dict_[x]+=1
                    dict_[y]-=1
                    ans.append(x)
                else:
                    dict_[y]+=1
                    dict_[x]-=1
                    ans.append(y)
        else:
            return False, []
    return True,ans
def sum1(dict_):
    ans = []
    flag = True
    for _ in range(N):
        xy = input()
        x = xy[0] 
        y = xy[1]
        if dict_[x]==1:
            dict_[x]-=1
            dict_[y]+=1
            ans.append(y)
        elif dict_[y]==1:
            dict_[y]-=1
            dict_[x]+=1
            ans.append(x)
        else:
            flag = False
            return flag,[]
    return flag, ans
def s0(dict_):
    return False, []
dict_ = {"A":A,"B":B,"C":C}
sum_ = A+B+C
if sum_>=3:
    flag, ans = sum3(dict_)
elif sum_==2:
    flag, ans = sum2(dict_)
elif sum_==1:
    flag, ans = sum1(dict_)
elif sum_==0:
    flag, ans = False, []
if flag:
    print('Yes')
    for i in range(N):
        print(ans[i])
else:
    print("No")

Submission status: Accepted

Solution code:
n,a,b,c=map(int,input().split())
# from collections import Counter
s=[input() for _ in range(n)]
AB,BC,AC="AB","BC","AC"

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