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
input=lambda: sys.stdin.readline().rstrip()
n,a,b,c=map(int,input().split())
S=[input() for _ in range(n)]
if a==0 and b==0 and S[0]=="AB":
  print("No")
elif b==0 and c==0 and S[0]=="BC":
  print("No")
elif a==0 and c==0 and S[0]=="AC":
  print("No")
else:
  if a+b+c==1:
    chk=True
    Ans=[]
    for s in S:
      if s=="AB":
        if c==1:
          chk=False
        elif a==1:
          Ans.append("B")
          a,b=0,1
        else:
          Ans.append("A")
          a,b=1,0
      if s=="BC":
        if a==1:
          chk=False
        elif b==1:
          Ans.append("C")
          b,c=0,1
        else:
          Ans.append("B")
          b,c=1,0
      if s=="AC":
        if b==1:
          chk=False
        elif a==1:
          Ans.append("C")
          a,c=0,1
        else:
          Ans.append("A")
          a,c=1,0
    if not chk:
      print("No")
    else:
      print("Yes")
      for ans in Ans:
        print(ans)
  else:
    print("Yes")
    Ans=[]
    for i in range(n):
      if i==n-1:
        if S[i]=="AB":
          if a<=b:
            Ans.append("A")
          else:
            Ans.append("B")
        if S[i]=="BC":
          if b<=c:
            Ans.append("B")
          else:
            Ans.append("C")
        if S[i]=="AC":
          if a<=c:
            Ans.append("A")
          else:
            Ans.append("C")

      else:
        if S[i]=="AB":
          if a==1 and b==1 and c==0:
            if S[i+1]=="BC":
              Ans.append("B")
              a,b=0,2
            else:
              Ans.append("A")
              a,b=2,0
          elif a<=b:
            Ans.append("A")
            a+=1
            b-=1
          else:
            Ans.append("B")
            a-=1
            b+=1
        if S[i]=="BC":
          if a==0 and b==1 and c==1:
            if S[i+1]=="AB":
              Ans.append("B")
              b,c=2,0
            else:
              Ans.append("C")
              b,c=0,2
          elif b<=c:
            Ans.append("B")
            b+=1
            c-=1
          else:
            Ans.append("C")
            b-=1
            c+=1
        if S[i]=="AC":
          if a==1 and b==0 and c==1:
            if S[i+1]=="AB":
              Ans.append("A")
              a,c=2,0
            else:
              Ans.append("C")
              a,c=0,2
          elif a<=c:
            Ans.append("A")
            a+=1
            c-=1
          else:
            Ans.append("C")
            a-=1
            c+=1
    for ans in Ans:
      print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n,a,b,c = map(int,input().split())
s = [input().rstrip() for i in range(n)]
def mthd(a,b,c,s):
  print("Yes")
  for i in range(n):
    if s[i] == "AB":
      if a >= b:
        print("B")
        a -= 1
        b += 1
      else:
        print("A")
        a += 1
        b -= 1
    if s[i] == "AC":
      if a >= c:
        print("C")
        a -= 1
        c += 1
      else:
        print("A")
        a += 1
        c -= 1
    if s[i] == "BC":
      if c >= b:
        print("B")
        c -= 1
        b += 1
      else:
        print("C")
        c += 1
        b -= 1
if a+b+c >= 3 and [a,b,c].count(0) <= 1:
  mthd(a,b,c,s)
else:
  if s[0] == "AB" and a+b == 0:
    print("No")
  elif s[0] == "BC" and b+c == 0:
    print("No")
  elif s[0] == "AC" and a+c == 0:
    print("No")
  elif a+b+c >= 3:
    mthd(a,b,c,s)
  elif a+b+c == 1:
    ans = []
    for i in range(n):
      if s[i] == "AB":
        if a == 1:
          ans.append("B")
        elif b == 1:
          ans.append("A")
        else:
          print("No")
          exit()
        a,b = b,a
      if s[i] == "BC":
        if c == 1:
          ans.append("B")
        elif b == 1:
          ans.append("C")
        else:
          print("No")
          exit()
        c,b = b,c
      if s[i] == "AC":
        if c == 1:
          ans.append("A")
        elif a == 1:
          ans.append("C")
        else:
          print("No")
          exit()
        c,a = a,c
    print("Yes",*ans,sep="\n")
  elif a+b+c == 2:
    print("Yes")
    for i in range(n-1):
      if s[i] == "AB":
        if a>b or (a == b and s[i+1] != "AC"):
          print("B")
          a -= 1
          b += 1
        elif a<b or s[i+1] == "AC":
          print("A")
          a += 1
          b -= 1
      elif s[i] == "AC":
        if a>c or (a == c and s[i+1] != "AB"):
          print("C")
          a -= 1
          c += 1
        elif a<c or s[i+1] == "AB":
          print("A")
          a += 1
          c -= 1
      elif s[i] == "BC":
        if c>b or (b == c and s[i+1] != "AC"):
          print("B")
          c -= 1
          b += 1
        elif c<b or s[i+1] == "AC":
          print("C")
          c += 1
          b -= 1
    if s[-1] == "AB":
      if b > 0:
        print("A")
      else:
        print("B")
    if s[-1] == "BC":
      if b > 0:
        print("C")
      else:
        print("B")
    if s[-1] == "AC":
      if c > 0:
        print("A")
      else:
        print("C")

Submission status: Accepted

Solution code:
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    N, A, B, C = map(int, input().split())
    ans = []
    S = []
    for _ in range(N):
        S.append(input().rstrip('\n'))

    j_AB = 0
    j_BC = 0
    j_AC = 0
    for i in range(N):
        s = S[i]
        if s == "AB":
            if A == B == 0:
                print("No")
                exit()
            if A > B:
                A -= 1
                B += 1
                ans.append("B")
            elif A < B:
                A += 1
                B -= 1
                ans.append("A")
            else:
                if A > 1 or C > 0:
                    A += 1
                    B -= 1
                    ans.append("A")
                else:
                    if j_AB > i:
                        if j_AB == N:
                            A -= 1
                            B += 1
                            ans.append("B")
                        elif S[j_AB] == "BC":
                            A -= 1
                            B += 1
                            ans.append("B")
                        else:
                            A += 1
                            B -= 1
                            ans.append("A")
                    else:
                        j_AB = i+1
                        if j_AB == N:
                            A -= 1
                            B += 1
                            ans.append("B")
                            continue
                        while True:
                            if S[j_AB] != "AB":
                                break
                            j_AB += 1
                            if j_AB == N:
                                break
                        if j_AB > i:
                            if j_AB == N:
                                A -= 1
                                B += 1
                                ans.append("B")
                            elif S[j_AB] == "BC":
                                A -= 1
                                B += 1
                                ans.append("B")
                            else:
                                A += 1
                                B -= 1
                                ans.append("A")
        elif s == "BC":
            if B == C == 0:
                print("No")
                exit()
            if B > C:
                B -= 1
                C += 1
                ans.append("C")
            elif B < C:
                B += 1
                C -= 1
                ans.append("B")
            else:
                if B > 1 or A > 0:
                    B += 1
                    C -= 1
                    ans.append("B")
                else:
                    if j_BC > i:
                        if j_BC == N:
                            B -= 1
                            C += 1
                            ans.append("C")
                        elif S[j_BC] == "AC":
                            B -= 1
                            C += 1
                            ans.append("C")
                        else:
                            B += 1
                            C -= 1
                            ans.append("B")
                    else:
                        j_BC = i+1
                        if j_BC == N:
                            B -= 1
                            C += 1
                            ans.append("C")
                            continue
                        while True:
                            if S[j_BC] != "BC":
                                break
                            j_BC += 1
                            if j_BC == N:
                                break
                        if j_BC > i:
                            if j_BC == N:
                                B -= 1
                                C += 1
                                ans.append("C")
                            elif S[j_BC] == "AC":
                                B -= 1
                                C += 1
                                ans.append("C")
                            else:
                                B += 1
                                C -= 1
                                ans.append("B")
        elif s == "AC":
            if A == C == 0:
                print("No")
                exit()
            if A > C:
                A -= 1
                C += 1
                ans.append("C")
            elif A < C:
                A += 1
                C -= 1
                ans.append("A")
            else:
                if A > 1 or B > 0:
                    A += 1
                    C -= 1
                    ans.append("A")
                else:
                    if j_AC > i:
                        if j_AC == N:
                            A -= 1
                            C += 1
                            ans.append("C")
                        elif S[j_AC] == "BC":
                            A -= 1
                            C += 1
                            ans.append("C")
                        else:
                            A += 1
                            C -= 1
                            ans.append("A")
                    else:
                        j_AC = i+1
                        if j_AC == N:
                            A -= 1
                            C += 1
                            ans.append("C")
                            continue
                        while True:
                            if S[j_AC] != "AC":
                                break
                            j_AC += 1
                            if j_AC == N:
                                break
                        if j_AC > i:
                            if j_AC == N:
                                A -= 1
                                C += 1
                                ans.append("C")
                            elif S[j_AC] == "BC":
                                A -= 1
                                C += 1
                                ans.append("C")
                            else:
                                A += 1
                                C -= 1
                                ans.append("A")
    print("Yes")
    for a in ans:
        print(a)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def Int():
    return int(input())
def Ints():
    return map(int,input().split())
def IntList():
    return list(Ints())
def IntMat(N):
    return [IntList() for i in range(N)]

import sys

N,A,B,C = Ints()
            
QList = []
for i in range(N-1):
    QList.append(input())
QList.append(input())
#print(QList)
#print(QList[0]=='AB')
    
Ans = []
Wh = 1
now = 0

while now < N:
    q = QList[now]
    #print(A,B,C)
    if not (A>=0 and B>= 0 and C>=0):
        Wh = 0
        break
    if q=='AB':
        if A==0 and B==0:
            print('No')
            sys.exit()
        elif A==1 and B==1:
            if now==N-1:
                Ans.append('A')
                A += 1
                B -= 1
            else:
                nexq = QList[now+1]
                if nexq==q:
                    Ans.append('A')
                    A += 1
                    B -= 1
                elif nexq == 'AC':
                    Ans.append('A')
                    A += 1
                    B -= 1
                elif nexq == 'BC':
                    Ans.append('B')
                    B += 1
                    A -= 1
        else:
            if A<B:
                Ans.append('A')
                A += 1
                B -= 1
            else:
                Ans.append('B')
                B += 1
                A -= 1
    if q=='AC':
        if A==0 and C==0:
            print('No')
            sys.exit()
        elif A==1 and C==1:
            if now==N-1:
                Ans.append('A')
                A += 1
                C -= 1
            else:
                nexq = QList[now+1]
                if nexq==q:
                    Ans.append('A')
                    A += 1
                    C -= 1
                elif nexq == 'BC':
                    Ans.append('C')
                    C += 1
                    A -= 1
                elif nexq == 'AB':
                    Ans.append('A')
                    A += 1
                    C -= 1
        else:
            if A<C:
                Ans.append('A')
                A += 1
                C -= 1
            else:
                Ans.append('C')
                C += 1
                A -= 1
    if q=='BC':
        if B==0 and C==0:
            print('No')
            sys.exit()
        elif B==1 and C==1:
            if now==N-1:
                Ans.append('B')
                B += 1
                C -= 1
            else:
                nexq = QList[now+1]
                if nexq==q:
                    Ans.append('B')
                    B += 1
                    C -= 1
                elif nexq == 'AC':
                    Ans.append('C')
                    C += 1
                    B -= 1
                elif nexq == 'AB':
                    Ans.append('B')
                    B += 1
                    C -= 1
        else:
            if B<C:
                Ans.append('B')
                B += 1
                C -= 1
            else:
                Ans.append('C')
                C += 1
                B -= 1
    now += 1
                
if Wh==0:
    print('No')
else:
    print('Yes')
    for i in Ans:
        print(i)

Submission status: Accepted

Solution code:


n,a,b,c = list(map(int, input().split()))
q = []
for i in range(n):
    s = input()
    if(s == 'AB'):
        q.append(0)
    elif(s == 'BC'):
        q.append(1)
    else:
        q.append(2)

act = []
for i in range(n):
    if(q[i]==0): #case AB
        if(a==0 and b==0):
            print("No")
            exit()
        elif(a==1 and b==1):
            if(i!=n-1 and q[i+1]==1):
                act.append('B')
                b+=1
                a-=1
            else:
                act.append('A')
                b-=1
                a+=1
        elif(a>=b):
            act.append('B')
            b+=1
            a-=1
        else:
            act.append('A')
            b-=1
            a+=1
    elif(q[i]==1):
        if(b==0 and c==0):
            print("No")
            exit()
        elif(b==1 and c==1):
            if(i!=n-1 and q[i+1]==2):
                act.append('C')
                c+=1
                b-=1
            else:
                act.append('B')
                c-=1
                b+=1
        elif(b>=c):
            act.append('C')
            c+=1
            b-=1
        else:
            act.append('B')
            c-=1
            b+=1
    else:
        if(c==0 and a==0):
            print("No")
            exit()
        elif(c==1 and a==1):
            if(i!=n-1 and q[i+1]==0):
                act.append('A')
                a+=1
                c-=1
            else:
                act.append('C')
                a-=1
                c+=1
        elif(c>=a):
            act.append('A')
            a+=1
            c-=1
        else:
            act.append('C')
            a-=1
            c+=1

print("Yes")
for i in range(n):
    print(act[i])

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

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


def check():
    abc = [min(2, i) for i in (A, B, C)]
    Ans = [None]*N
    for i in range(N):
        a, b, c = abc
        if S[i] == 'AB':
            if a == 0 and b == 0:
                return False
            if a == 1 and b == 1:
                if i < N-1 and S[i+1] == 'BC':
                    Ans[i] = 1
                else:
                    Ans[i] = 0
            else:
                if a < b:
                    Ans[i] = 0
                else:
                    Ans[i] = 1
            if Ans[i] == 0:
                abc = (a+1, b-1, c)
            else:
                abc = (a-1, b+1, c)
        if S[i] == 'AC':
            if a == 0 and c == 0:
                return False
            if a == 1 and c == 1:
                if i < N-1 and S[i+1] == 'BC':
                    Ans[i] = 2
                else:
                    Ans[i] = 0
            else:
                if a < c:
                    Ans[i] = 0
                else:
                    Ans[i] = 2
            if Ans[i] == 0:
                abc = (a+1, b, c-1)
            else:
                abc = (a-1, b, c+1)
        if S[i] == 'BC':
            if b == 0 and c == 0:
                return False
            if b == 1 and c == 1:
                if i < N-1 and S[i+1] == 'AB':
                    Ans[i] = 1
                else:
                    Ans[i] = 2
            else:
                if b > c:
                    Ans[i] = 2
                else:
                    Ans[i] = 1
            if Ans[i] == 1:
                abc = (a, b+1, c-1)
            else:
                abc = (a, b-1, c+1)
    return Ans
       
ans = check()     
if ans:
    print('Yes')
    R = ['A', 'B', 'C']
    print('\n'.join(R[a] for a in ans))
else:
    print('No')

Submission status: Accepted

Solution code:
FAST_IO = 1
if FAST_IO:
    import io, sys, atexit
    rr = iter(sys.stdin.read().splitlines()).next
    sys.stdout = _OUTPUT_BUFFER = io.BytesIO()
    @atexit.register
    def write():
        sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
else:
    rr = raw_input
rri = lambda: int(rr())
rrm = lambda: map(int, rr().split())

####
from collections import Counter

def solve3(N, A, B, C, SA):
    return solve(N, A, B, C, SA, True)
    S = A+B+C
    # max(A, B, C) > 2
    # min(A, B, C) = 0
    std = sorted([A, B, C])
    if std[1] == 0:  # 0, 0, >2
        return solve(N, A, B, C, SA, True)
    # 0, >0, >2
    
def solve2(N, A, B, C, SA):
    def canonical(a, b, c):
        return a, b, c

    if A > 2 or B > 2 or C > 2:
        return solve3(N, A, B, C, SA)
    seen = [None] * (N+1)
    seen[0] = {canonical(A, B, C)}
    for i, x in enumerate(SA, 1):
        seen[i] = set()
        for a, b, c in seen[i-1]:
            if x == 0:  # BC
                na = a; nb = b + 1; nc = c - 1
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add(canonical(na, nb, nc))
                na = a; nb = b - 1; nc = c + 1
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add(canonical(na, nb, nc))
            if x == 1:  # AC
                na = a + 1; nb = b; nc = c - 1
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add(canonical(na, nb, nc))
                na = a - 1; nb = b; nc = c + 1
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add(canonical(na, nb, nc))
            if x == 2:  # AB
                na = a + 1; nb = b - 1; nc = c
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add(canonical(na, nb, nc))
                na = a - 1; nb = b + 1; nc = c
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add(canonical(na, nb, nc))
        if not seen[i]: return
    ans = []
    a,b,c = seen[-1].pop()
    for i in xrange(N-1, -1, -1):
        x = SA[i]
        if x == 0:  # BC
            na = a; nb = b + 1; nc = c - 1
            nei = canonical(na, nb, nc)
            if nei in seen[i]:
                ans.append('C')
                a,b,c = nei
                continue
            na = a; nb = b - 1; nc = c + 1
            nei = canonical(na, nb, nc)
            if nei in seen[i]:
                ans.append('B')
                a,b,c = nei
                continue
            return
        if x == 1:  # AC
            na = a + 1; nb = b; nc = c - 1
            nei = canonical(na, nb, nc)
            if nei in seen[i]:
                ans.append('C')
                a,b,c = nei
                continue
            na = a - 1; nb = b; nc = c + 1
            nei = canonical(na, nb, nc)
            if nei in seen[i]:
                ans.append('A')
                a,b,c = nei
                continue
            return
        if x == 2:  # AB
            na = a + 1; nb = b - 1; nc = c
            nei = canonical(na, nb, nc)
            if nei in seen[i]:
                ans.append('B')
                a,b,c = nei
                continue
            na = a - 1; nb = b + 1; nc = c
            nei = canonical(na, nb, nc)
            if nei in seen[i]:
                ans.append('A')
                a,b,c = nei
                continue
            return 
    return ans[::-1]
    
def solve(N, A, B, C, SA, flag=False):
    if flag is False and min(A, B, C) == 0:
        return solve2(N, A, B, C, SA)
    ans = []
    for x in SA:
        if x == 0:  # BC
            if B >= C:
                B -= 1
                C += 1
                ans.append('C')
            else:
                B += 1
                C -= 1
                ans.append('B')
        elif x == 1: #AC
            if A >= C:
                A -= 1
                C += 1
                ans.append('C')
            else:
                A += 1
                C -= 1
                ans.append('A')
        else:  # AB
            if A >= B:
                A -= 1
                B += 1
                ans.append('B')
            else:
                A += 1
                B -= 1
                ans.append('A')
        if A<0 or B<0 or C<0: return
    return ans
    
N, A, B, C = rrm()
SA = []
for _ in xrange(N):
    s = rr()
    if s == 'AB':
        SA.append(2)
    elif s == 'BC':
        SA.append(0)
    else:
        SA.append(1)
ans = solve(N, A, B, C, SA)
if ans:
    print "Yes"
    for x in ans: print x
else:
    print "No"

Submission status: Accepted

Solution code:
n, a, b, c = map(int, input().split())
q = [input() for i in range(n)]
if [a, b, c].count(0) <= 1:
    print("Yes")
    for i in range(n-1):
        if q[i] == "AB":
            if a == 0:
                print("A")
                a += 1
                b -= 1

            elif b == 0:
                print("B")
                b += 1
                a -= 1

            else:
                if q[i+1] == "AB":
                    print("A")
                    a += 1
                    b -= 1
                elif q[i+1] == "BC":
                    print("B")
                    b += 1
                    a -= 1
                else:
                    print("A")
                    a += 1
                    b -= 1

        elif q[i] == "BC":
            if b == 0:
                print("B")
                b += 1
                c -= 1

            elif c == 0:
                print("C")
                c += 1
                b -= 1

            else:
                if q[i+1] == "BC" or q[i+1] == "AB":
                    print("B")
                    b += 1
                    c -= 1
                elif q[i+1] == "AC":
                    print("C")
                    c += 1
                    b -= 1

        else:
            if a == 0:
                print("A")
                a += 1
                c -= 1

            elif c == 0:
                print("C")
                c += 1
                a -= 1

            else:
                if q[i+1] == "AC" or q[i+1] == "AB":
                    print("A")
                    a += 1
                    c -= 1
                elif q[i+1] == "BC":
                    print("C")
                    c += 1
                    a -= 1
    if q[-1] == "AC":
        if a == 0:
            print("A")
        else:
            print("C")
    elif q[-1] == "AB":
        if a == 0:
            print("A")
        else:
            print("B")
    else:
        if b == 0:
            print("B")
        else:
            print("C")


else:
    flag = 1
    A, B, C = a, b, c
    for i in range(n):
        if q[i] == "AB":
            if a == 0 and b == 0:
                flag = 0
                break
            elif a == 0:
                a += 1
                b -= 1
            elif b == 0:
                b += 1
                a -= 1
            else:
                break

        elif q[i] == "BC":
            if b == 0 and c == 0:
                flag = 0
                break
            elif b == 0:
                b += 1
                c -= 1
            elif c == 0:
                c += 1
                b -= 1
            else:
                break
        else:
            if a == 0 and c == 0:
                flag = 0
                break
            elif a == 0:
                a += 1
                c -= 1
            elif c == 0:
                c += 1
                a -= 1
            else:
                break
        if [a, b, c].count(0) <= 1:
            break
    if flag == 0:
        print("No")
    else:
        print("Yes")
        a, b, c = A, B, C
        for i in range(n-1):
            if q[i] == "AB":
                if a == 0:
                    print("A")
                    a += 1
                    b -= 1

                elif b == 0:
                    print("B")
                    b += 1
                    a -= 1

                else:
                    if q[i+1] == "AB":
                        print("A")
                        a += 1
                        b -= 1
                    elif q[i+1] == "BC":
                        print("B")
                        b += 1
                        a -= 1
                    else:
                        print("A")
                        a += 1
                        b -= 1

            elif q[i] == "BC":
                if b == 0:
                    print("B")
                    b += 1
                    c -= 1

                elif c == 0:
                    print("C")
                    c += 1
                    b -= 1

                else:
                    if q[i+1] == "BC" or q[i+1] == "AB":
                        print("B")
                        b += 1
                        c -= 1
                    elif q[i+1] == "AC":
                        print("C")
                        c += 1
                        b -= 1

            else:
                if a == 0:
                    print("A")
                    a += 1
                    c -= 1

                elif c == 0:
                    print("C")
                    c += 1
                    a -= 1

                else:
                    if q[i+1] == "AC" or q[i+1] == "AB":
                        print("A")
                        a += 1
                        c -= 1
                    elif q[i+1] == "BC":
                        print("C")
                        c += 1
                        a -= 1
        if q[-1] == "AC":
            if a == 0:
                print("A")
            else:
                print("C")
        elif q[-1] == "AB":
            if a == 0:
                print("A")
            else:
                print("B")
        else:
            if b == 0:
                print("B")
            else:
                print("C")

Submission status: Accepted

Solution code:
N,A,B,C = map(int,input().split())
S = [input() for _ in [0]*N]

ans = [0]*N
for i,s in enumerate(S):
    if s=="AB":
        if A==B==0:
            print("No")
            break
        elif A==B==1:
            if S[(i+1)%N]=="BC":
                ans[i] = "B"
                B += 1
                A -= 1
            else:
                ans[i] = "A"
                A += 1
                B -= 1
        elif A>=B:
            ans[i] = "B"
            A -= 1
            B += 1
        else:
            ans[i] = "A"
            A += 1
            B -= 1
    elif s=="BC":
        if B==C==0:
            print("No")
            break
        elif B==C==1:
            if S[(i+1)%N]=="AC":
                ans[i] = "C"
                C += 1
                B -= 1
            else:
                ans[i] = "B"
                B += 1
                C -= 1
        elif B>=C:
            ans[i] = "C"
            B -= 1
            C += 1
        else:
            ans[i] = "B"
            B += 1
            C -= 1
    elif s=="AC":
        if A==C==0:
            print("No")
            break
        elif A==C==1:
            if S[(i+1)%N]=="BC":
                ans[i] = "C"
                C += 1
                A -= 1
            else:
                ans[i] = "A"
                A += 1
                C -= 1
        elif A>=C:
            ans[i] = "C"
            A -= 1
            C += 1
        else:
            ans[i] = "A"
            A += 1
            C -= 1
else:
    print("Yes")
    for out in ans:
        print(out)