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

strs=[]
for _ in range(N):
    s=input()
    if s=='AB':
        strs.append((0,1))
    elif s=='AC':
        strs.append((0,2))
    else: #BC
        strs.append((1,2))

l=[A,B,C]
ans=[]
ds={0:'A',1:'B',2:'C'}

if sum(l)==0:
    print('No')
    exit()

if sum(l)==1:
    for n in range(N):
        i1,i2=strs[n]
        if l[i1]==0 and l[i2]==0:
            break
        else:
            if l[i1]==0:
                l[i1]+=1
                l[i2]-=1
                ans.append(ds[i1])
            else: # l[i2]==0
                l[i1]-=1
                l[i2]+=1
                ans.append(ds[i2])
    else:
        print('Yes')
        print(*ans,sep='\n')
        exit()
    print('No')
    exit()

i1,i2=strs[0]
if l[i1]==0 and l[i2]==0:
    print('No')
else:
    print('Yes')
    for n in range(N):
        i1,i2=strs[n]

        if n < N-1:
            i1_n,i2_n=strs[n+1]
        else:
            i1_n,i2_n=i1,i2
        
        if l[i1]*l[i2]==0:
            if l[i1]==0:
                l[i1]+=1
                l[i2]-=1
                ans.append(ds[i1])
            else:
                l[i1]-=1
                l[i2]+=1
                ans.append(ds[i2])
        elif (l[i1]==1 or l[i2]==1) and n<N-1 and [i1,i2]!=[i1_n,i2_n]:
            if i1==i1_n or i1==i2_n:
                l[i1]+=1
                l[i2]-=1
                ans.append(ds[i1])
            else:
                l[i1]-=1
                l[i2]+=1
                ans.append(ds[i2])
        else:
            l[i1]+=1
            l[i2]-=1
            ans.append(ds[i1])
    print(*ans,sep='\n')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys

YES = "Yes"  # type: str
NO = "No"  # type: str


def solve(N: int, A: int, B: int, C: int, s: "List[str]"):
    L = ["A" for _ in range(N)]
    for i in range(N):
        if s[i] == "AB":
            if A == 0 and B == 0:
                print(NO)
                return
            else:
                if A > B:
                    A -= 1
                    B += 1
                    L[i] = "B"
                elif A < B:
                    A += 1
                    B -= 1
                    L[i] = "A"
                else:
                    if i == N - 1:
                        A -= 1
                        B += 1
                        L[i] = "B"
                    else:
                        if s[i+1] == "AB":
                            A -= 1
                            B += 1
                            L[i] = "B"
                        elif s[i+1] == "BC":
                            A -= 1
                            B += 1
                            L[i] = "B"
                        else:   # AC
                            A += 1
                            B -= 1
                            L[i] = "A"
        elif s[i] == "BC":
            if B == 0 and C == 0:
                print(NO)
                return
            else:
                if B > C:
                    B -= 1
                    C += 1
                    L[i] = "C"
                elif B < C:
                    B += 1
                    C -= 1
                    L[i] = "B"
                else:
                    if i == N - 1:
                        B -= 1
                        C += 1
                        L[i] = "C"
                    else:
                        if s[i+1] == "AB":
                            B += 1
                            C -= 1
                            L[i] = "B"
                        elif s[i+1] == "BC":
                            B -= 1
                            C += 1
                            L[i] = "C"
                        else:   # AC
                            B -= 1
                            C += 1
                            L[i] = "C"
        else:   # AC
            if A == 0 and C == 0:
                print(NO)
                return
            else:
                if A > C:
                    A -= 1
                    C += 1
                    L[i] = "C"
                elif A < C:
                    A += 1
                    C -= 1
                    L[i] = "A"
                else:
                    if i == N - 1:
                        A -= 1
                        C += 1
                        L[i] = "C"
                    else:
                        if s[i+1] == "AB":
                            A += 1
                            C -= 1
                            L[i] = "A"
                        elif s[i+1] == "BC":
                            A -= 1
                            C += 1
                            L[i] = "C"
                        else:   # AC
                            A -= 1
                            C += 1
                            L[i] = "C"
    print(YES)
    for l in L:
        print(l)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(N)]  # type: "List[str]"
    solve(N, A, B, C, s)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, a, b, c = map(int, input().split())
ans = []
prev = ''
for _ in range(n):
    s = input()
    if 'C' not in s:
        if a == b == 0 and ans:
            # キングクリムゾン発動！
            prev_x = ans.pop()
            if prev_x == prev[0]:
                prev_y = prev[1]
            else:
                prev_y = prev[0]
            ans.append(prev_y)
            if prev_x == 'A':
                a -= 2
            elif prev_x == 'B':
                b -= 2
            else:
                c -= 2
            if prev_y == 'A':
                a += 2
            elif prev_y == 'B':
                b += 2
            else:
                c += 2
        if a < b:
            a += 1
            b -= 1
            ans.append('A')
        else:
            a -= 1
            b += 1
            ans.append('B')

    elif 'B' not in s:
        if a == c == 0 and ans:
            # キングクリムゾン発動！
            prev_x = ans.pop()
            if prev_x == prev[0]:
                prev_y = prev[1]
            else:
                prev_y = prev[0]
            ans.append(prev_y)

            if prev_x == 'A':
                a -= 2
            elif prev_x == 'B':
                b -= 2
            else:
                c -= 2
            if prev_y == 'A':
                a += 2
            elif prev_y == 'B':
                b += 2
            else:
                c += 2
        if a < c:
            a += 1
            c -= 1
            ans.append('A')
        else:
            a -= 1
            c += 1
            ans.append('C')
    elif 'A' not in s:
        if b == c == 0 and ans:
            # キングクリムゾン発動！
            prev_x = ans.pop()
            if prev_x == prev[0]:
                prev_y = prev[1]
            else:
                prev_y = prev[0]
            ans.append(prev_y)
            if prev_x == 'A':
                a -= 2
            elif prev_x == 'B':
                b -= 2
            else:
                c -= 2
            if prev_y == 'A':
                a += 2
            elif prev_y == 'B':
                b += 2
            else:
                c += 2
        if b < c:
            b += 1
            c -= 1
            ans.append('B')
        else:
            b -= 1
            c += 1
            ans.append('C')
    prev = s
    if -1 in (a, b, c):
        print('No')
        break
else:
    print('Yes')
    for x in ans:
        print(x)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline().strip()
n, a, b, c = map(int, input().split())
s = []
for _ in range(n):
    s.append(input())

# 0 <= i < n-1
ans = []
for i in range(n-1):
    if s[i] == 'BC':
        if b > c:
            ans.append('C')
            b -= 1
            c += 1
        elif b < c:
            ans.append('B')
            b += 1
            c -= 1
        elif s[i+1] == 'AB':
            ans.append('B')
            b += 1
            c -= 1
        else:
            ans.append('C')
            b -= 1
            c += 1

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

    else:
        if a > b:
            ans.append('B')
            a -= 1
            b += 1
        elif a < b:
            ans.append('A')
            a += 1
            b -= 1
        elif s[i+1] == 'AC':
            ans.append('A')
            a += 1
            b -= 1
        else:
            ans.append('B')
            a -= 1
            b += 1

    if a < 0 or b < 0 or c < 0:
        print('No')
        exit()

# i = n-1
if s[n-1] == 'BC':
    if b > c:
        ans.append('C')
        b -= 1
        c += 1
    else:
        ans.append('B')
        b += 1
        c -= 1

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

else:
    if a > b:
        ans.append('B')
        a -= 1
        b += 1
    else:
        ans.append('A')
        a += 1
        b -= 1

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())
s_=[input() for i in range(n)]
ans=""
for i in range(n):
    s1,s2=s_[i]
    if s2=="B":
        if i!=n-1:
            if s_[i+1]=="AC" and c==0 and a==1:
                a,b,ans=(a+1,b-1,ans+"A")
            elif s_[i+1]=="BC" and c==0 and b==1:
                a,b,ans=(a-1,b+1,ans+"B")
            else:
                a,b,ans=(a-1,b+1,ans+"B") if a>b else (a+1,b-1,ans+"A")
        else:
            a,b,ans=(a-1,b+1,ans+"B") if a>b else (a+1,b-1,ans+"A")
        if min(a,b)==-1:
            print("No")
            break
    elif s1=="A":
        if i!=n-1:
            if s_[i+1]=="AB" and b==0 and a==1:
                a,c,ans=(a+1,c-1,ans+"A")
            elif s_[i+1]=="BC" and b==0 and c==1:
                a,c,ans=(a-1,c+1,ans+"C")
            else:
                a,c,ans=(a-1,c+1,ans+"C") if a>c else (a+1,c-1,ans+"A")
        else:
            a,c,ans=(a-1,c+1,ans+"C") if a>c else (a+1,c-1,ans+"A")
        if min(a,c)==-1:
            print("No")
            break
    else:
        if i!=n-1:
            if s_[i+1]=="AB" and a==0 and b==1:
                b,c,ans=(b+1,c-1,ans+"B")
            elif s_[i+1]=="AC" and a==0 and c==1:
                b,c,ans=(b-1,c+1,ans+"C")
            else:
                b,c,ans=(b-1,c+1,ans+"C") if b>c else (b+1,c-1,ans+"B")
        else:
            b,c,ans=(b-1,c+1,ans+"C") if b>c else (b+1,c-1,ans+"B")
        if min(b,c)==-1:
            print("No")
            break
else:
    print("Yes")
    for j in range(n):
        print(ans[j])

Submission status: Accepted

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

abc = {'A': a, 'B': b, 'C': c}

def answer():
    solution = []
    if a + b + c == 0:
        return False, None
    elif a + b + c == 1:
        for s in ls:
            s0, s1 = s[0], s[1]
            if abc[s0] == 1:
                abc[s0] -= 1
                abc[s1] += 1
                solution.append(s1)
            elif abc[s1] == 1:
                abc[s0] += 1
                abc[s1] -= 1
                solution.append(s0)
            else:
                return False, None
        return True, solution
    else:
        for i, s in enumerate(ls):
            s0, s1 = s[0], s[1]
            if abc[s0] == 0 and abc[s1] == 0:
                return False, None
            elif i == n - 1:
                if abc[s0] == 0:
                    abc[s0] += 1
                    abc[s1] -= 1
                    solution.append(s0)
                else:
                    abc[s0] -= 1
                    abc[s1] += 1
                    solution.append(s1)
            elif abc[s0] == 0:
                abc[s0] += 1
                abc[s1] -= 1
                solution.append(s0)
            elif abc[s1] == 0:
                abc[s0] -= 1
                abc[s1] += 1
                solution.append(s1)
            elif abc[s0] == 1 and (s0 in ls[i + 1]):
                abc[s0] += 1
                abc[s1] -= 1
                solution.append(s0)
            else:
                abc[s0] -= 1
                abc[s1] += 1
                solution.append(s1)
        return True, solution


yn, solution = answer()
if yn:
    print('Yes')
    for s in solution:
        print(s)
else:
    print('No')

Submission status: Accepted

Solution code:
N, A, B, C = list(map(int, input().split()))
d = {'A': A, 'B': B, 'C': C}
res = []
SN = [input() for i in range(N)]
for i in range(N):
    s = SN[i]
    s0 = s[0]
    s1 = s[1]

    if d[s0] == d[s1] == 0:
        print('No')
        exit()
    elif d[s0] == 0:
        d[s0] += 1
        d[s1] -= 1
        res.append(s0)
    elif d[s1] == 0:
        d[s0] -= 1
        d[s1] += 1
        res.append(s1)
    elif i == N-1:
        res.append(s0)
    elif s0 in SN[i+1]:
        d[s0] += 1
        d[s1] -= 1
        res.append(s0)
    else:
        d[s0] -= 1
        d[s1] += 1
        res.append(s1)

print('Yes')
for r in res:
    print(r)

Submission status: Accepted

Solution code:
n,a,b,c=map(int,input().split())
s_=[input() for i in range(n)]
ans=""
for i in range(n):
    s1,s2=s_[i]
    if s2=="B":
        if i!=n-1:
            if s_[i+1]=="AC" and c==0 and a<=1:
                a,b,ans=(a+1,b-1,ans+"A")
            elif s_[i+1]=="BC" and c==0 and b<=1:
                a,b,ans=(a-1,b+1,ans+"B")
            else:
                a,b,ans=(a-1,b+1,ans+"B") if a>b else (a+1,b-1,ans+"A")
        else:
            a,b,ans=(a-1,b+1,ans+"B") if a>b else (a+1,b-1,ans+"A")
        if min(a,b)==-1:
            print("No")
            break
    elif s1=="A":
        if i!=n-1:
            if s_[i+1]=="AB" and b==0 and a<=1:
                a,c,ans=(a+1,c-1,ans+"A")
            elif s_[i+1]=="BC" and b==0 and c<=1:
                a,c,ans=(a-1,c+1,ans+"C")
            else:
                a,c,ans=(a-1,c+1,ans+"C") if a>c else (a+1,c-1,ans+"A")
        else:
            a,c,ans=(a-1,c+1,ans+"C") if a>c else (a+1,c-1,ans+"A")
        if min(a,c)==-1:
            print("No")
            break
    else:
        if i!=n-1:
            if s_[i+1]=="AB" and a==0 and b<=1:
                b,c,ans=(b+1,c-1,ans+"B")
            elif s_[i+1]=="AC" and a==0 and c<=1:
                b,c,ans=(b-1,c+1,ans+"C")
            else:
                b,c,ans=(b-1,c+1,ans+"C") if b>c else (b+1,c-1,ans+"B")
        else:
            b,c,ans=(b-1,c+1,ans+"C") if b>c else (b+1,c-1,ans+"B")
        if min(b,c)==-1:
            print("No")
            break
else:
    print("Yes")
    for j in range(n):
        print(ans[j])

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 # 998244353
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n, a, b, c = map(int,input().split())
    D = {"AB" : (0, 1), "AC" : (0, 2), "BC" : (1, 2)}
    E = {0 : 'A', 1 : 'B', 2 : 'C'}
    queries = [D[input()] for _ in range(n)]
    s = a + b + c

    if s != 2:
        ans = []
        state = [a, b, c]
        for i, j in queries:
            if state[i] == 0 and state[j] == 0:
                print("No")
                return
            if state[i] < state[j]:
                state[i] += 1
                state[j] -= 1
                ans.append(E[i])
            else:
                state[i] -= 1
                state[j] += 1
                ans.append(E[j])
        print("Yes")
        print(*ans, sep = '\n')
        return

    # s = 2
    dp = set()
    dp.add((a, b, c))
    prev = [{} for _ in range(n)]
    for k in range(n):
        ndp = set()
        i, j = queries[k]
        for state in dp:
            if state[i] > 0:
                next = list(state)
                next[i] -= 1
                next[j] += 1
                next = tuple(next)
                ndp.add(next)
                prev[k][next] = state
            if state[j] > 0:
                next = list(state)
                next[j] -= 1
                next[i] += 1
                next = tuple(next)
                ndp.add(next)
                prev[k][next] = state
        dp = ndp

    if not dp:
        print("No")
        return

    for now in dp:
        break

    ans = []
    for k in range(n - 1, -1, -1):
        pa, pb, pc = prev[k][now]
        a, b, c = now
        if a > pa:
            ans.append('A')
        elif b > pb:
            ans.append('B')
        elif c > pc:
            ans.append('C')
        now = prev[k][now]
    ans.reverse()


    print("Yes")
    print(*ans, sep = '\n')
resolve()

Submission status: Accepted

Solution code:
n,a,b,c=map(int,input().split())
s_=[input() for i in range(n)]
ans=""
for i in range(n):
    s1,s2=s_[i]
    if s2=="B":
        if a==1 and b==1 and i!=n-1:
            if s_[i+1][0]=="A":
                a,b,ans=(a+1,b-1,ans+"A")
            else:
                a,b,ans=(a-1,b+1,ans+"B")
            continue
        a,b,ans=(a-1,b+1,ans+"B") if a>b else (a+1,b-1,ans+"A")
        if min(a,b)==-1:
            print("No")
            break
    elif s1=="A":
        if a==1 and c==1 and i!=n-1:
            if s_[i+1][1]=="C":
                a,c,ans=(a-1,c+1,ans+"C")
            else:
                a,c,ans=(a+1,c-1,ans+"A")
            continue
        a,c,ans=(a-1,c+1,ans+"C") if a>c else (a+1,c-1,ans+"A")
        if min(a,c)==-1:
            print("No")
            break
    else:
        if c==1 and b==1 and i!=n-1:
            if s_[i+1][1]=="C":
                b,c,ans=(b-1,c+1,ans+"C")
            else:
                b,c,ans=(b+1,c-1,ans+"B")
            continue
        b,c,ans=(b-1,c+1,ans+"C") if b>c else (b+1,c-1,ans+"B")
        if min(b,c)==-1:
            print("No")
            break
else:
    print("Yes")
    for j in range(n):
        print(ans[j])

Submission status: Accepted

Solution code:
n, a, b, c = map(int, input().split())
d = {"A": a, "B": b, "C": c}
res = []
s_l = [input() for _ in range(n)]

for i, s in enumerate(s_l): 
    s0 = s[0]
    s1 = s[1]
    if d[s0] == d[s1] == 0:
        print("No")
        exit()
    elif d[s0] == 0:
        d[s0] += 1
        d[s1] -= 1
        res.append(s0)
    elif d[s1] == 0:
        d[s0] -= 1
        d[s1] += 1
        res.append(s1)
    elif i == n-1:
        res.append(s0)
    elif s0 in s_l[i+1]:
        d[s0] += 1
        d[s1] -= 1
        res.append(s0)
    else:
        d[s0] -= 1
        d[s1] += 1
        res.append(s1)
print("Yes")
for r in res:
    print(r)

Submission status: Accepted

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

Submission status: Accepted

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


def solve(x):
    d = {"A": 0, "B": 1, "C": 2}
    ans = []
    for i in range(N):
        u, v = X[i]
        if x[d[u]] == x[d[v]] == 0:
            return

        if x[d[u]] < x[d[v]]:
            x[d[u]] += 1
            x[d[v]] -= 1
            ans.append(u)
        elif x[d[u]] > x[d[v]]:
            x[d[u]] -= 1
            x[d[v]] += 1
            ans.append(v)
        elif i < N - 1 and X[i] != X[i + 1]:
            # x[d[u]] == x[d[v]] >= 1
            if u in X[i + 1]:
                x[d[u]] += 1
                x[d[v]] -= 1
                ans.append(u)
            else:
                x[d[u]] -= 1
                x[d[v]] += 1
                ans.append(v)
        else:
            x[d[u]] += 1
            x[d[v]] -= 1
            ans.append(u)

        if x[d[u]] < 0 or x[d[v]] < 0:
            return

    return ans

ans = solve([A, B, C])
if ans:
    print("Yes")
    print(*ans, sep="\n")
else:
    print("No")

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
sys.setrecursionlimit(10**7)
from pprint import pprint as pp
from pprint import pformat as pf

import math
#from sortedcontainers import SortedList, SortedDict, SortedSet # no in atcoder
import bisect


class Player():

    A = 0
    B = 1
    C = 2

    def __init__(self, data, op_list):
        self.data = data
        self.op_list = op_list
        self.move_history = ["X"] * len(self.op_list)
        self.key = 0
        self.ans = "Yes" # "Yes" or "No"
        self.flg_out = False
        self.flg_foresight = False
        s = sum(data)
        if s == 2:
            self.flg_foresight = True
        elif s == 0:
            self.ans = "No"
            self.flg_out = True

    def has_next_op(self):
        if self.key + 1 < len(self.op_list):
            return True
        else:
            return False

    def play(self):
        for key, op in enumerate(op_list):
            #print('self.data') # debug
            #print(self.data) # debug
            if self.flg_out:
                break
            self.key = key
            self.read_op(op)

    def read_op(self, op):
        if op == "AB":
            self.do(self.A, self.B)
        if op == "AC":
            self.do(self.A, self.C)
        if op == "BC":
            self.do(self.B, self.C)

    def do(self, x, y):
        if self.data[x] < self.data[y]:
            self.move(y, x)
        elif self.data[x] > self.data[y]:
            self.move(x, y)
        else: # same
            if self.flg_foresight:
                if self.is_next_used(x):
                    self.move(y, x)
                else:
                    self.move(x, y)
            else:
                self.move(x, y) # which is ok

    def move(self, frm, to):
        self.move_history[self.key] = self.num_to_char(to)
        self.data[frm] -= 1
        self.data[to] += 1
        if self.data[frm] < 0:
            self.ans = "No"
            self.flg_out = True

    @staticmethod
    def num_to_char(z):
        chars = ['A', 'B', 'C']
        return chars[z]

    def is_next_used(self, z):
        if not self.has_next_op():
            return False
        next_op = self.op_list[self.key + 1]
        if self.num_to_char(z) in next_op:
            return True
        else:
            return False

    def print_ans(self):
        print(self.ans)
        if self.ans == "No":
            return
        for h in self.move_history:
            print(h)




if __name__ == '__main__':
    n, a, b, c = list(map(int, input().split()))
    data = [a, b, c]
    #print('data') # debug
    #print(data) # debug
    op_list = [""] * n
    for i in range(n):
        op_list[i] = input()
    #print('op_list') # debug
    #print(op_list) # debug

    p = Player(data, op_list)
    p.play()
    p.print_ans()


    #print('\33[32m' + 'end' + '\033[0m') # debug

Submission status: Accepted

Solution code:

n,a,b,c=map(int,input().split())
s=[""]*n
abc=dict()
abc["A"]=a
abc["B"]=b
abc["C"]=c

for i in range(n):
    s[i]=input()

t=[""]*n
yn=""
for i in range(n):
    if abc[s[i][0]]==abc[s[i][1]] and i<n-1:
#        print("1:　s:",s[i],"a:",abc["A"],"b:",abc["B"],"c:",abc["C"])
        if s[i][1] in s[i+1]:
#            print("2:　s:",s[i],"s[i+1]:",s[i+1],"i+1:",i+1)
            t[i]=s[i][1]
            abc[s[i][0]]-=1
            abc[s[i][1]]+=1
            if abc[s[i][0]]<0:
                yn="No"
                break            
        else:
#            print("3:　s:",s[i],"s[i+1]:",s[i+1],"i+1:",i+1)
            t[i]=s[i][0]
            abc[s[i][0]]+=1
            abc[s[i][1]]-=1
            if abc[s[i][1]]<0:
                yn="No"
                break
    elif abc[s[i][0]]>=abc[s[i][1]]:
        t[i]=s[i][1]
        abc[s[i][0]]-=1
        abc[s[i][1]]+=1
        if abc[s[i][0]]<0:
            yn="No"
            break
    else:
        t[i]=s[i][0]
        abc[s[i][0]]+=1
        abc[s[i][1]]-=1
        if abc[s[i][1]]<0:
            yn="No"
            break

#    print(t[i],"a:",abc["A"],"b:",abc["B"],"c:",abc["C"])
#print(t[i],"a:",abc["A"],"b:",abc["B"],"c:",abc["C"])
            
if yn=="No":
    print("No")
else:
    print("Yes")
    for i in range(n):
        print(t[i])

Submission status: Accepted

Solution code:
import sys
import bisect
inf = 10**15
mod = 10**9+7
ans = []
n,a,b,c = map(int, input().split())
abc = []


for i in range(n):
    s = input()
    abc.append(s)

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