Problem p02692 (python)

Fixed submission:
n,a,b,c = map(int,input().split())
s = []
for _ in range(n):
    s.append(input())
if a + b + c == 0:
    print('No')
elif a + b + c == 1:
    ans = ''
    for i in range(n):
        if s[i] == 'AB':
            if a == 1 and b == 0:
                a = 0
                b = 1
                ans += 'B'
            elif a == 0 and b == 1:
                a = 1
                b = 0
                ans += 'A'
            else:
                print('No')
                exit()
        elif s[i] == 'BC':
            if b == 1 and c == 0:
                b = 0
                c = 1
                ans += 'C'
            elif b == 0 and c == 1:
                b = 1
                c = 0
                ans += 'B'
            else:
                print('No')
                exit()
        else:
            if a == 1 and c == 0:
                a = 0
                c = 1
                ans += 'C'
            elif a == 0 and c == 1:
                a = 1
                c = 0
                ans += 'A'
            else:
                print('No')
                exit()
    print('Yes')
    for i in range(n):
        print(ans[i])
else:
    ans = ''
    s2 = 'AC'
    for i in range(n):
        s1 = s[i]
        if i != n-1:
            s2 = s[i+1]
        if s1 == 'AB':
            if a >= 1 and b == 0:
                a -= 1
                b = 1
                ans += 'B'
            elif a == 0 and b >= 1:
                a = 1
                b -= 1
                ans += 'A'
            elif a >= 1 and b >= 1:
                if s2 == 'BC':
                    b += 1
                    a -= 1
                    ans += 'B'
                else:
                    a += 1
                    b -= 1
                    ans += 'A'
            else:
                print('No')
                exit()
        elif s1 == 'BC':
            if b >= 1 and c == 0:
                b -= 1
                c = 1
                ans += 'C'
            elif b == 0 and c >= 1:
                b = 1
                c -= 1
                ans += 'B'
            elif b >= 1 and c >= 1:
                if s2 == 'AC':
                    c += 1
                    b -= 1
                    ans += 'C'
                else:
                    b += 1
                    c -= 1
                    ans += 'B'
            else:
                print('No')
                exit()
        else:
            if a >= 1 and c == 0:
                a -= 1
                c = 1
                ans += 'C'
            elif a == 0 and c >= 1:
                a = 1
                c -= 1
                ans += 'A'
            elif a >= 1 and c >= 1:
                if s2 == 'AB':
                    a += 1
                    c -= 1
                    ans += 'A'
                else:
                    a -= 1
                    c += 1
                    ans += 'C'
            else:
                print('No')
                exit()
        s1 = s2
    print('Yes')
    for i in range(n):
        print(ans[i])

Buggy submission (verdict: Runtime Error):
"""
import copy

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


s = s[::-1]
ans = []

tmp = [[0,0,0]]

for s1 in s:
    for i in range(len(tmp)):
        t = tmp.pop(0)
        if ans:
            ans1 = ans.pop(0)
        else:
            ans1 = ''
        if s1 == 'AB':
            if t[0] == 0 and t[1] == 0:
                tmp.append([t[0]+1,t[1],t[2]])
                tmp.append([t[0],t[1]+1,t[2]])
                ans.append(ans1+'B')
                ans.append(ans1+'A')
            elif t[0] == 0:
                tmp.append([t[0]+1,t[1]-1,t[2]])
                ans.append(ans1+'B')
            elif t[1] == 0:
                tmp.append([t[0]-1,t[1]+1,t[2]])
                ans.append(ans1+'A')
            else:
                tmp.append([t[0]+1,t[1]-1,t[2]])
                tmp.append([t[0]-1,t[1]+1,t[2]])
                ans.append(ans1+'B')
                ans.append(ans1+'A')
        elif s1 == 'BC':
            if t[1] == 0 and t[2] == 0:
                tmp.append([t[0],t[1],t[2]+1])
                tmp.append([t[0],t[1]+1,t[2]])
                ans.append(ans1+'B')
                ans.append(ans1+'C')
            elif t[1] == 0:
                tmp.append([t[0],t[1]+1,t[2]-1])
                ans.append(ans1+'C')
            elif t[2] == 0:
                tmp.append([t[0],t[1]-1,t[2]+1])
                ans.append(ans1+'B')
            else:
                tmp.append([t[0],t[1]-1,t[2]+1])
                tmp.append([t[0],t[1]+1,t[2]-1])
                ans.append(ans1+'B')
                ans.append(ans1+'C')
        else:
            if t[2] == 0 and t[0] == 0:
                tmp.append([t[0]+1,t[1],t[2]])
                tmp.append([t[0],t[1],t[2]+1])
                ans.append(ans1+'C')
                ans.append(ans1+'A')

            elif t[2] == 0:
                tmp.append([t[0]-1,t[1],t[2]+1])
                ans.append(ans1+'A')

            elif t[0] == 0:
                tmp.append([t[0]+1,t[1],t[2]-1])
                ans.append(ans1+'C')

            else:
                tmp.append([t[0]+1,t[1],t[2]-1])
                tmp.append([t[0]-1,t[1],t[2]+1])
                ans.append(ans1+'C')
                ans.append(ans1+'A')

    li = [True]*len(tmp)
    for i in range(len(tmp)):
        for j in range(i+1, len(tmp)):
            for k in range(3):
                if tmp[i][k] <= tmp[j][k]:
                    li[j] = False
    tmp2 = []
    ans2 = []
    for i in range(len(tmp)):
        if li[i]:
            tmp2.append(tmp[i])
            ans2.append(ans[i])
    tmp = copy.copy(tmp2)
    ans = copy.copy(ans2)

for i in range(len(tmp)):
    if a >= tmp[i][0] and b >= tmp[i][1] and c >= tmp[i][2]:
        print('Yes')
        for j in range(n):
            print(ans[i][-j-1])
        exit()

print('No')
"""

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

Buggy submission (verdict: Runtime Error):
n,a,b,c = map(int,input().split())
s = []
for _ in range(n):
    s.append(input())
if a + b + c == 0:
    print('No')
elif a + b + c == 1:
    ans = ''
    for i in range(n):
        if s[i] == 'AB':
            if a == 1 and b == 0:
                a = 0
                b = 1
                ans += 'B'
            elif a == 0 and b == 1:
                a = 1
                b = 0
                ans += 'A'
            else:
                print('No')
                exit()
        elif s[i] == 'BC':
            if b == 1 and c == 0:
                b = 0
                c = 1
                ans += 'C'
            elif b == 0 and c == 1:
                b = 1
                c = 0
                ans += 'B'
            else:
                print('No')
                exit()
        else:
            if a == 1 and c == 0:
                a = 0
                c = 1
                ans += 'C'
            elif a == 0 and c == 1:
                a = 1
                c = 0
                ans += 'A'
            else:
                print('No')
                exit()
    print('Yes')
    for i in range(n):
        print(ans[i])
else:
    ans = ''
    for i in range(n):
        s1 = s[i]
        if i != n-1:
            s2 = s[i+1]
        if s1 == 'AB':
            if a >= 1 and b == 0:
                a -= 1
                b = 1
                ans += 'B'
            elif a == 0 and b >= 1:
                a = 1
                b -= 1
                ans += 'A'
            elif a >= 1 and b >= 1:
                if s2 == 'BC':
                    b += 1
                    a -= 1
                    ans += 'B'
                else:
                    a += 1
                    b -= 1
                    ans += 'A'
            else:
                print('No')
                exit()
        elif s1 == 'BC':
            if b >= 1 and c == 0:
                b -= 1
                c = 1
                ans += 'C'
            elif b == 0 and c >= 1:
                b = 1
                c -= 1
                ans += 'B'
            elif b >= 1 and c >= 1:
                if s2 == 'AC':
                    c += 1
                    b -= 1
                    ans += 'C'
                else:
                    b += 1
                    c -= 1
                    ans += 'B'
            else:
                print('No')
                exit()
        else:
            if a >= 1 and c == 0:
                a -= 1
                c = 1
                ans += 'C'
            elif a == 0 and c >= 1:
                a = 1
                c -= 1
                ans += 'A'
            elif a >= 1 and c >= 1:
                if s2 == 'AB':
                    a += 1
                    c -= 1
                    ans += 'A'
                else:
                    a -= 1
                    c += 1
                    ans += 'C'
            else:
                print('No')
                exit()
        s1 = s2
    print('Yes')
    for i in range(n):
        print(ans[i])

Buggy submission (verdict: Runtime Error):
n,a,b,c = map(int,input().split())
s = []
for _ in range(n):
    s.append(input())
if a + b + c == 0:
    print('No')
elif a + b + c == 1:
    ans = ''
    for i in range(n):
        if s[i] == 'AB':
            if a == 1 and b == 0:
                a = 0
                b = 1
                ans += 'B'
            elif a == 0 and b == 1:
                a = 1
                b = 0
                ans += 'A'
            else:
                print('No')
                exit()
        if s[i] == 'BC':
            if b == 1 and c == 0:
                b = 0
                c = 1
                ans += 'C'
            elif b == 0 and c == 1:
                b = 1
                c = 0
                ans += 'B'
            else:
                print('No')
                exit()
        else:
            if a == 1 and c == 0:
                a = 0
                c = 1
                ans += 'C'
            elif a == 0 and c == 1:
                a = 1
                c = 0
                ans += 'A'
            else:
                print('No')
                exit()
    print('Yes')
    for i in range(n):
        print(ans[i])
else:
    ans = ''
    for i in range(n):
        s1 = s[i]
        if i != n-1:
            s2 = s[i+1]
        if s1 == 'AB':
            if a >= 1 and b == 0:
                a -= 1
                b = 1
                ans += 'B'
            elif a == 0 and b >= 1:
                a = 1
                b -= 1
                ans += 'A'
            elif a >= 1 and b >= 1:
                if s2 == 'BC':
                    b += 1
                    a -= 1
                    ans += 'B'
                else:
                    a += 1
                    b -= 1
                    ans += 'A'
            else:
                print('No')
                exit()
        elif s1 == 'BC':
            if b >= 1 and c == 0:
                b -= 1
                c = 1
                ans += 'C'
            elif b == 0 and c >= 1:
                b = 1
                c -= 1
                ans += 'B'
            elif b >= 1 and c >= 1:
                if s2 == 'AC':
                    c += 1
                    b -= 1
                    ans += 'C'
                else:
                    b += 1
                    c -= 1
                    ans += 'B'
            else:
                print('No')
                exit()
        else:
            if a >= 1 and c == 0:
                a -= 1
                c = 1
                ans += 'C'
            elif a == 0 and c >= 1:
                a = 1
                c -= 1
                ans += 'A'
            elif a >= 1 and c >= 1:
                if s2 == 'AB':
                    a += 1
                    c -= 1
                    ans += 'A'
                else:
                    a -= 1
                    c += 1
                    ans += 'C'
            else:
                print('No')
                exit()
        s1 = s2
    print('Yes')
    for i in range(n):
        print(ans[i])

Buggy submission (verdict: Runtime Error):
n,a,b,c = map(int,input().split())
s = []
for _ in range(n):
    s.append(input())
if a + b + c == 0:
    print('No')
elif a + b + c == 1:
    ans = ''
    for i in range(n):
        if s[i] == 'AB':
            if a == 1 and b == 0:
                a = 0
                b = 1
                ans += 'B'
            elif a == 0 and b == 1:
                a = 1
                b = 0
                ans += 'A'
            else:
                print('No')
                exit()
        if s[i] == 'BC':
            if b == 1 and c == 0:
                b = 0
                c = 1
                ans += 'C'
            elif b == 0 and c == 1:
                b = 1
                c = 0
                ans += 'B'
            else:
                print('No')
                exit()
        else:
            if a == 1 and c == 0:
                a = 0
                c = 1
                ans += 'C'
            elif a == 0 and c == 1:
                a = 1
                c = 0
                ans += 'A'
            else:
                print('No')
                exit()
    print('Yes')
    for i in range(n):
        print(ans[i])
else:
    ans = ''
    for i in range(n):
        s1 = s[i]
        if i != n-1:
            s2 = s[i+1]
        if s1 == 'AB':
            if a >= 1 and b == 0:
                a = 0
                b = 1
                ans += 'B'
            elif a == 0 and b >= 1:
                a = 1
                b = 0
                ans += 'A'
            elif a >= 1 and b >= 1:
                if s2 == 'BC':
                    b += 1
                    a -= 1
                    ans += 'B'
                else:
                    a += 1
                    b -= 1
                    ans += 'A'
            else:
                print('No')
                exit()
        elif s1 == 'BC':
            if b >= 1 and c == 0:
                b = 0
                c = 1
                ans += 'C'
            elif b == 0 and c >= 1:
                b = 1
                c = 0
                ans += 'B'
            elif b >= 1 and c >= 1:
                if s2 == 'AC':
                    c += 1
                    b -= 1
                    ans += 'C'
                else:
                    b += 1
                    c -= 1
                    ans += 'B'
            else:
                print('No')
                exit()
        else:
            if a >= 1 and c == 0:
                a = 0
                c = 1
                ans += 'C'
            elif a == 0 and c >= 1:
                a = 1
                c = 0
                ans += 'A'
            elif a >= 1 and c >= 1:
                if s2 == 'AB':
                    a += 1
                    c -= 1
                    ans += 'A'
                else:
                    a -= 1
                    c += 1
                    ans += 'C'
            else:
                print('No')
                exit()
        s1 = s2
    print('Yes')
    for i in range(n):
        print(ans[i])

Buggy submission (verdict: Runtime Error):
"""
import copy

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


s = s[::-1]
ans = []

tmp = [[0,0,0]]

for s1 in s:
    for i in range(len(tmp)):
        t = tmp.pop(0)
        if ans:
            ans1 = ans.pop(0)
        else:
            ans1 = ''
        if s1 == 'AB':
            if t[0] == 0 and t[1] == 0:
                tmp.append([t[0]+1,t[1],t[2]])
                tmp.append([t[0],t[1]+1,t[2]])
                ans.append(ans1+'B')
                ans.append(ans1+'A')
            elif t[0] == 0:
                tmp.append([t[0]+1,t[1]-1,t[2]])
                ans.append(ans1+'B')
            elif t[1] == 0:
                tmp.append([t[0]-1,t[1]+1,t[2]])
                ans.append(ans1+'A')
            else:
                tmp.append([t[0]+1,t[1]-1,t[2]])
                tmp.append([t[0]-1,t[1]+1,t[2]])
                ans.append(ans1+'B')
                ans.append(ans1+'A')
        elif s1 == 'BC':
            if t[1] == 0 and t[2] == 0:
                tmp.append([t[0],t[1],t[2]+1])
                tmp.append([t[0],t[1]+1,t[2]])
                ans.append(ans1+'B')
                ans.append(ans1+'C')
            elif t[1] == 0:
                tmp.append([t[0],t[1]+1,t[2]-1])
                ans.append(ans1+'C')
            elif t[2] == 0:
                tmp.append([t[0],t[1]-1,t[2]+1])
                ans.append(ans1+'B')
            else:
                tmp.append([t[0],t[1]-1,t[2]+1])
                tmp.append([t[0],t[1]+1,t[2]-1])
                ans.append(ans1+'B')
                ans.append(ans1+'C')
        else:
            if t[2] == 0 and t[0] == 0:
                tmp.append([t[0]+1,t[1],t[2]])
                tmp.append([t[0],t[1],t[2]+1])
                ans.append(ans1+'C')
                ans.append(ans1+'A')

            elif t[2] == 0:
                tmp.append([t[0]-1,t[1],t[2]+1])
                ans.append(ans1+'A')

            elif t[0] == 0:
                tmp.append([t[0]+1,t[1],t[2]-1])
                ans.append(ans1+'C')

            else:
                tmp.append([t[0]+1,t[1],t[2]-1])
                tmp.append([t[0]-1,t[1],t[2]+1])
                ans.append(ans1+'C')
                ans.append(ans1+'A')

    li = [True]*len(tmp)
    for i in range(len(tmp)):
        for j in range(i+1, len(tmp)):
            for k in range(3):
                if tmp[i][k] <= tmp[j][k]:
                    li[j] = False
    tmp2 = []
    ans2 = []
    for i in range(len(tmp)):
        if li[i]:
            tmp2.append(tmp[i])
            ans2.append(ans[i])
    tmp = copy.copy(tmp2)
    ans = copy.copy(ans2)

for i in range(len(tmp)):
    if a >= tmp[i][0] and b >= tmp[i][1] and c >= tmp[i][2]:
        print('Yes')
        for j in range(n):
            print(ans[i][-j-1])
        exit()

print('No')
"""

n,a,b,c = map(int,input().split())
if a + b + c == 0:
    print('No')
elif a + b + c == 1:
    ans = ''
    for _ in range(n):
        s = input()
        if s == 'AB':
            if a == 1 and b == 0:
                a = 0
                b = 1
                ans += 'B'
            elif a == 0 and b == 1:
                a = 1
                b = 0
                ans += 'A'
            else:
                print('No')
                exit()
        if s == 'BC':
            if b == 1 and c == 0:
                b = 0
                c = 1
                ans += 'C'
            elif b == 0 and c == 1:
                b = 1
                c = 0
                ans += 'B'
            else:
                print('No')
                exit()
        else:
            if a == 1 and c == 0:
                a = 0
                c = 1
                ans += 'C'
            elif a == 0 and c == 1:
                a = 1
                c = 0
                ans += 'A'
            else:
                print('No')
                exit()
    print('Yes')
    for i in range(n):
        print(ans[i])
else:
    ans = ''
    for i in range(n):
        if i == 0:
            s1 = input()
        if i != n-1:
            s2 = input()

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