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: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N, *ABC = map(int, rl().split())
    s = [rl().rstrip() for _ in range(N)]
    
    if sum(ABC) == 0:
        print('No')
    elif sum(ABC) == 1:
        print('No')
    else:
        if ABC[ord(s[0][0]) - ord('A')] == 0 and ABC[ord(s[0][1]) - ord('A')] == 0:
            print('No')
        else:
            print('Yes')
            ans = []
            for i, si in enumerate(s):
                idx0 = ord(si[0]) - ord('A')
                idx1 = ord(si[1]) - ord('A')
                if ABC[idx0] == 0:
                    ABC[idx0] += 1
                    ABC[idx1] -= 1
                    ans.append(si[0])
                elif ABC[idx1] == 0:
                    ABC[idx0] -= 1
                    ABC[idx1] += 1
                    ans.append(si[1])
                elif sum(ABC) == 2 and ABC[idx0] == 1 and ABC[idx1] == 1 and i != N - 1 and si != s[i + 1]:
                    if si[0] in s[i + 1]:
                        ABC[idx0] += 1
                        ABC[idx1] -= 1
                        ans.append(si[0])
                    else:
                        ABC[idx0] -= 1
                        ABC[idx1] += 1
                        ans.append(si[1])
                else:
                    ABC[idx0] += 1
                    ABC[idx1] -= 1
                    ans.append(si[0])
            print(*ans, sep='\n')


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import numpy as np
def resolve():
    N,A,B,C = map(int,input().split())
    Nlist = []
    YesOrNo = True
    ABC = str(input())
    for i in range(N):
        ABC_old = ABC

        if ABC_old == "AB":
            if ABC == "AC":
                add1 = np.sign(B - A + 0.1)
            else:
                add1 = np.sign(B - A - 0.1)

            if add1 < 0:
                Nlist.append("B")
            else:
                Nlist.append("A")
            A = A + add1
            B = B - add1
            if A < 0 or B < 0:
                YesOrNo = False
                print('No')
                break
        
        if ABC_old == "AC":
            if ABC == "AB":
                add1 = np.sign(C - A + 0.1)
            else:
                add1 = np.sign(C - A - 0.1)

            if add1 < 0:
                Nlist.append("C")
            else:
                Nlist.append("A")
            A = A + add1
            C = C - add1
            if A < 0 or C < 0:
                YesOrNo = False
                print('No')
                break
        
        if ABC_old == "BC":
            if ABC == "AB":
                add1 = np.sign(C - B + 0.1)
            else:
                add1 = np.sign(C - B - 0.1)

            if add1 < 0:
                Nlist.append("C")
            else:
                Nlist.append("B")
            B = B + add1
            C = C - add1
            if B < 0 or C < 0:
                YesOrNo = False
                print('No')
                break

        if i == N-1:
            break
        ABC = str(input())

    if YesOrNo:
        print("Yes")
        s_Nlist = '\n'.join(Nlist)
        print(s_Nlist)
resolve()

Submission status: Wrong Answer

Solution code:
import numpy as np
def resolve():
    N,A,B,C = map(int,input().split())
    Nlist = []
    YesOrNo = True
    for _ in range(N):
        ABC = str(input())
        if ABC == "AB":
            add1 = np.sign(B - A + 0.1)
            if add1 < 0:
                Nlist.append("B")
            else:
                Nlist.append("A")
            A = A + add1
            B = B - add1
            if A < 0 or B < 0:
                YesOrNo = False
                print('No')
                break
        
        if ABC == "AC":
            add1 = np.sign(C - A + 0.1)
            if add1 < 0:
                Nlist.append("C")
            else:
                Nlist.append("A")
            A = A + add1
            C = C - add1
            if A < 0 or C < 0:
                YesOrNo = False
                print('No')
                break
        
        if ABC == "BC":
            add1 = np.sign(C - B + 0.1)
            if add1 < 0:
                Nlist.append("C")
            else:
                Nlist.append("B")
            B = B + add1
            C = C - add1
            if B < 0 or C < 0:
                YesOrNo = False
                print('No')
                break
    if YesOrNo:
        print("Yes")
        s_Nlist = '\n'.join(Nlist)
        print(s_Nlist)
resolve()

Submission status: Wrong Answer

Solution code:
import sys

n,a,b,c=map(int,input().split())

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

Submission status: Wrong Answer

Solution code:
import sys

n,a,b,c=map(int,input().split())

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

Submission status: Wrong Answer

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

N, A, B, C = map(int, input().split())
cmd = [input()[:-1] for _ in range(N)]
ans = [""]*N

def ope(i, cmd):
    global A, B, C
    if cmd == "AC":
        ans[i] = "A"
        A += 1
        C -= 1
    elif cmd == "BC":
        ans[i]  = "B"
        B += 1
        C -= 1
    else:
        ans[i] = "A"
        A += 1
        B -= 1

if A+B+C == 0:
    print("No")
elif A+B+C == 1:
    flg = True
    for i in range(N):
        if cmd[i] == "AB" and A+B == 1:
            if B == 1:
                ans[i] = "A"
            else:
                ans[i] = "B"
            A, B = B, A
        elif cmd[i] == "AC" and A+C == 1:
            if C == 1:
                ans[i] = "A"
            else:
                ans[i] = "C"
            A, C = C, A
        elif cmd[i] == "BC" and B+C == 1:
            if C == 1:
                ans[i] = "B"
            else:
                ans[i] = "C"
            B, C = C, B
        else:
            flg = False
            break 
    if flg:
        print("Yes")
        print("\n".join(ans))
    else:
        print("No")

elif A+B+C >= 2:
    if (cmd[0] == "AB" and A == 0 and B == 0) or \
        (cmd[0] == "BC" and B == 0 and C == 0) or \
        (cmd[0] == "AC" and A == 0 and C == 0):
        print("No")
    else:
        print("Yes")
        for i in range(N):
            if cmd[i] == "AB" and (A == 0 or B == 0):
                if A == 0:
                    ans[i] = "A"
                    A += 1
                    B -= 1
                else:
                    ans[i] = "B"
                    B += 1
                    A -= 1
            elif cmd[i] == "AC" and (A == 0 or C == 0):
                if A == 0:
                    ans[i] = "A"
                    A += 1
                    C -= 1
                else:
                    ans[i] = "C"
                    C += 1
                    A -= 1
            elif cmd[i] == "BC" and (B == 0 or C == 0):
                if B == 0:
                    ans[i] = "B"
                    B += 1
                    C -= 1
                else:
                    ans[i] = "C"
                    C += 1
                    B -= 1
            elif A+B+C == 2 and i != N-1:
                if cmd[i] != cmd[i+1]:
                    if cmd[i] == "AB" and A == 1 and B == 1:
                        if cmd[i+1] == "BC":
                            ans[i] = "B"
                            B += 1
                            A -= 1
                        else:
                            ans[i] = "A"
                            A += 1
                            B -= 1
                    elif cmd[i] == "BC" and B == 1 and C == 1:
                        if cmd[i+1] == "AC":
                            ans[i] = "C"
                            C += 1
                            B -= 1
                        else:
                            ans[i] = "B"
                            B += 1
                            C -= 1
                    elif cmd[i] == "AC" and A == 1 and C == 1:
                        if cmd[i+1] == "BC":
                            ans[i] = "C"
                            C += 1
                            A -= 1
                        else:
                            ans[i] = "A"
                            A += 1
                            C -= 1
                    else:
                        ope(i, cmd[i])
                else:
                    ope(i, cmd[i])
            else:
                ope(i, cmd[i])
        print("\n".join(ans))

Submission status: Wrong Answer

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

N, A, B, C = map(int, input().split())
cmd = [input()[:-1] for _ in range(N)]
ans = [""]*N

def ope(i, cmd):
    global A, B, C
    if cmd == "AC":
        ans[i] = "A"
        A += 1
        C -= 1
    elif cmd == "BC":
        ans[i]  = "B"
        B += 1
        C -= 1
    else:
        ans[i] = "A"
        A += 1
        B -= 1

if A+B+C == 0:
    print("No")
elif A+B+C == 1:
    flg = True
    for i in range(N):
        if cmd[i] == "AB" and A+B == 1:
            if B == 1:
                ans[i] = "A"
            else:
                ans[i] = "B"
            A, B = B, A
        elif cmd[i] == "AC" and A+C == 1:
            if C == 1:
                ans[i] = "A"
            else:
                ans[i] = "C"
            A, C = C, A
        elif cmd[i] == "BC" and B+C == 1:
            if C == 1:
                ans[i] = "B"
            else:
                ans[i] = "C"
            B, C = C, B
        else:
            flg = False
            break 
    if flg:
        print("Yes")
        print("\n".join(ans))
    else:
        print("No")

elif A+B+C >= 2:
    if (cmd[0] == "AB" and A == 0 and B == 0) or \
        (cmd[0] == "BC" and B == 0 and C == 0) or \
        (cmd[0] == "AC" and A == 0 and C == 0):
        print("No")
    else:
        print("Yes")
        for i in range(N):
            if cmd[i] == "AB" and (A == 0 or B == 0):
                if A == 0:
                    ans[i] = "A"
                    A += 1
                    B -= 1
                else:
                    ans[i] = "B"
                    B += 1
                    A -= 1
            elif cmd[i] == "AC" and (A == 0 or C == 0):
                if A == 0:
                    ans[i] = "A"
                    A += 1
                    C -= 1
                else:
                    ans[i] = "C"
                    C += 1
                    A -= 1
            elif cmd[i] == "BC" and (B == 0 or C == 0):
                if B == 0:
                    ans[i] = "B"
                    B += 1
                    C -= 1
                else:
                    ans[i] = "C"
                    C += 1
                    B -= 1
            elif A+B+C == 2 and i != N-1:
                if cmd[i] != cmd[i+1]:
                    if cmd[i] == "AB" and A == 1 and B == 1:
                        if cmd[i+1] == "BC":
                            ans[i] = "B"
                            B += 1
                            A -= 1
                        else:
                            ans[i] = "A"
                            A += 1
                            B -= 1
                    elif cmd[i] == "BC" and B == 1 and C == 1:
                        if cmd[i+1] == "AC":
                            ans[i] = "C"
                            C += 1
                            B -= 1
                        else:
                            ans[i] = "B"
                            B += 1
                            C -= 1
                    elif cmd[i] == "AC" and A == 1 and C == 1:
                        if cmd[i+1] == "BC":
                            ans[i] = "C"
                            C += 1
                            B -= 1
                        else:
                            ans[i] = "B"
                            B += 1
                            C -= 1
                    else:
                        ope(i, cmd[i])
                else:
                    ope(i, cmd[i])
            else:
                ope(i, cmd[i])
        print("\n".join(ans))

Submission status: Wrong Answer

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

N, A, B, C = map(int, input().split())
cmd = [input()[:-1] for _ in range(N)]
ans = [""]*N

def ope(i, cmd):
    global A, B, C
    if cmd == "AC":
        ans[i] = "A"
        A += 1
        C -= 1
    elif cmd == "BC":
        ans[i]  = "B"
        B += 1
        C -= 1
    else:
        ans[i] = "A"
        A += 1
        B -= 1

if A+B+C == 0:
    print("No")
elif A+B+C == 1:
    flg = True
    for i in range(N):
        if cmd[i] == "AB" and A+B == 1:
            if A == 1:
                ans[i] = "A"
            else:
                ans[i] = "B"
            A, B = B, A
        elif cmd[i] == "AC" and A+C == 1:
            if A == 1:
                ans[i] = "A"
            else:
                ans[i] = "C"
            A, C = C, A
        elif cmd[i] == "BC" and B+C == 1:
            if B == 1:
                ans[i] = "B"
            else:
                ans[i] = "C"
            B, C = C, B
        else:
            flg = False
            break 
    if flg:
        print("Yes")
        print("\n".join(ans))
    else:
        print("No")

elif A+B+C >= 2:
    if (cmd[0] == "AB" and A == 0 and B == 0) or \
        (cmd[0] == "BC" and B == 0 and C == 0) or \
        (cmd[0] == "AC" and A == 0 and C == 0):
        print("No")
    else:
        print("Yes")
        for i in range(N):
            if cmd[i] == "AB" and (A == 0 or B == 0):
                if A == 0:
                    ans[i] = "A"
                    A += 1
                    B -= 1
                else:
                    ans[i] = "B"
                    B += 1
                    A -= 1
            elif cmd[i] == "AC" and (A == 0 or C == 0):
                if A == 0:
                    ans[i] = "A"
                    A += 1
                    C -= 1
                else:
                    ans[i] = "C"
                    C += 1
                    A -= 1
            elif cmd[i] == "BC" and (B == 0 or C == 0):
                if B == 0:
                    ans[i] = "B"
                    B += 1
                    C -= 1
                else:
                    ans[i] = "C"
                    C += 1
                    B -= 1
            elif A+B+C == 2 and i != N-1:
                if cmd[i] != cmd[i+1]:
                    if cmd[i] == "AB" and A == 1 and B == 1:
                        if cmd[i+1] == "BC":
                            ans[i] = "B"
                            B += 1
                            A -= 1
                        else:
                            ans[i] = "A"
                            A += 1
                            B -= 1
                    elif cmd[i] == "BC" and B == 1 and C == 1:
                        if cmd[i+1] == "AC":
                            ans[i] = "C"
                            C += 1
                            B -= 1
                        else:
                            ans[i] = "B"
                            B += 1
                            C -= 1
                    elif cmd[i] == "AC" and A == 1 and C == 1:
                        if cmd[i+1] == "BC":
                            ans[i] = "C"
                            C += 1
                            B -= 1
                        else:
                            ans[i] = "B"
                            B += 1
                            C -= 1
                    else:
                        ope(i, cmd[i])
                else:
                    ope(i, cmd[i])
            else:
                ope(i, cmd[i])
        print("\n".join(ans))

Submission status: Wrong Answer

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

N, A, B, C = map(int, input().split())
cmd = [input()[:-1] for _ in range(N)]
ans = [""]*N

def ope(i, cmd):
    global A, B, C
    if cmd == "AC":
        ans[i] = "A"
        A += 1
        C -= 1
    elif cmd == "BC":
        ans[i]  = "B"
        B += 1
        C -= 1
    else:
        ans[i] = "A"
        A += 1
        C -= 1

if A+B+C == 0:
    print("No")
elif A+B+C == 1:
    flg = True
    for i in range(N):
        if cmd[i] == "AB" and A+B == 1:
            if A == 1:
                ans[i] = "A"
            else:
                ans[i] = "B"
            A, B = B, A
        elif cmd[i] == "AC" and A+C == 1:
            if A == 1:
                ans[i] = "A"
            else:
                ans[i] = "C"
            A, C = C, A
        elif cmd[i] == "BC" and B+C == 1:
            if B == 1:
                ans[i] = "B"
            else:
                ans[i] = "C"
            B, C = C, B
        else:
            flg = False
            break 
    if flg:
        print("Yes")
        print("\n".join(ans))
    else:
        print("No")

elif A+B+C >= 2:
    if (cmd[0] == "AB" and A == 0 and B == 0) or \
        (cmd[0] == "BC" and B == 0 and C == 0) or \
        (cmd[0] == "AC" and A == 0 and C == 0):
        print("No")
    else:
        print("Yes")
        for i in range(N):
            if cmd[i] == "AB" and (A == 0 or B == 0):
                if A == 0:
                    ans[i] = "A"
                    A += 1
                    B -= 1
                else:
                    ans[i] = "B"
                    B += 1
                    A -= 1
            elif cmd[i] == "AC" and (A == 0 or C == 0):
                if A == 0:
                    ans[i] = "A"
                    A += 1
                    C -= 1
                else:
                    ans[i] = "C"
                    C += 1
                    A -= 1
            elif cmd[i] == "BC" and (B == 0 or C == 0):
                if B == 0:
                    ans[i] = "B"
                    B += 1
                    C -= 1
                else:
                    ans[i] = "C"
                    C += 1
                    B -= 1
            elif A+B+C == 2 and i != N-1:
                if cmd[i] != cmd[i+1]:
                    if cmd[i] == "AB" and A == 1 and B == 1:
                        if cmd[i+1] == "BC":
                            ans[i] = "B"
                            B += 1
                            A -= 1
                        else:
                            ans[i] = "A"
                            A += 1
                            B -= 1
                    elif cmd[i] == "BC" and B == 1 and C == 1:
                        if cmd[i+1] == "AC":
                            ans[i] = "C"
                            C += 1
                            B -= 1
                        else:
                            ans[i] = "B"
                            B += 1
                            C -= 1
                    elif cmd[i] == "AC" and A == 1 and C == 1:
                        if cmd[i+1] == "BC":
                            ans[i] = "C"
                            C += 1
                            B -= 1
                        else:
                            ans[i] = "B"
                            B += 1
                            C -= 1
                    else:
                        ope(i, cmd[i])
                else:
                    ope(i, cmd[i])
            else:
                ope(i, cmd[i])
        print("\n".join(ans))

Submission status: Wrong Answer

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

N, A, B, C = map(int, input().split())
cmd = [input()[:-1] for _ in range(N)]
ans = [""]*N

def ope(i, cmd):
    global A, B, C
    if cmd == "AC":
        ans[i] = "A"
        A += 1
        C -= 1
    elif cmd == "BC":
        ans[i]  = "B"
        B += 1
        C -= 1
    else:
        ans[i] = "A"
        A += 1
        C -= 1

if A+B+C == 0:
    print("No")
elif A+B+C == 1:
    flg = True
    for i in range(N):
        if cmd[i] == "AB" and A+B == 1:
            if A == 1:
                ans[i] = "A"
            else:
                ans[i] = "B"
            A, B = B, A
        elif cmd[i] == "AC" and A+C == 1:
            if A == 1:
                ans[i] = "A"
            else:
                ans[i] = "C"
            A, C = C, A
        elif cmd[i] == "BC" and B+C == 1:
            if B == 1:
                ans[i] = "B"
            else:
                ans[i] = "C"
            B, C = C, B
        else:
            flg = False
            break 
    if flg:
        print("Yes")
        print("\n".join(ans))
    else:
        print("No")

elif A+B+C >= 2:
    if (cmd[0] == "AB" and A == 0 and B == 0) or \
        (cmd[0] == "BC" and B == 0 and C == 0) or \
        (cmd[0] == "AC" and A == 0 and C == 0):
        print("No")
    else:
        print("Yes")
        for i in range(N):
            if cmd[i] == "AB" and A+B == 1:
                if A == 1:
                    ans[i] = "B"
                else:
                    ans[i] = "A"
                A, B = B, A
            elif cmd[i] == "AC" and A+C == 1:
                if A == 1:
                    ans[i] = "C"
                else:
                    ans[i] = "A"
                A, C = C, A
            elif cmd[i] == "BC" and B+C == 1:
                if B == 1:
                    ans[i] = "C"
                else:
                    ans[i] = "B"
                B, C = C, B
            elif A+B+C == 2 and i != N-1:
                if cmd[i] != cmd[i+1]:
                    if cmd[i] == "AB" and A == 1 and B == 1:
                        if cmd[i+1] == "BC":
                            ans[i] = "B"
                            B += 1
                            A -= 1
                        else:
                            ans[i] = "A"
                            A += 1
                            B -= 1
                    elif cmd[i] == "BC" and B == 1 and C == 1:
                        if cmd[i+1] == "AC":
                            ans[i] = "C"
                            C += 1
                            B -= 1
                        else:
                            ans[i] = "B"
                            B += 1
                            C -= 1
                    elif cmd[i] == "AC" and A == 1 and C == 1:
                        if cmd[i+1] == "BC":
                            ans[i] = "C"
                            C += 1
                            B -= 1
                        else:
                            ans[i] = "B"
                            B += 1
                            C -= 1
                    else:
                        ope(i, cmd[i])
                else:
                    ope(i, cmd[i])
            else:
                ope(i, cmd[i])
        print("\n".join(ans))