Problem p02692 (python)

Fixed submission:
s = [0,0,0]
n,s[0],s[1],s[2] = map(int,input().split())
T = [0]*n
ABC = 'ABC'
for i in range(n):
    t = input()
    if t == 'AB':T[i] = 2
    if t == 'BC':T[i] = 0
    if t == 'AC':T[i] = 1
ans = ['' for _ in range(n)]
# if s.count(0) <= 1 and sum(s) >= 3:
#     for i,t in enumerate(T):
#         nxt = [0,1,2]
#         nxt.remove(t)
#         if s[nxt[0]] < s[nxt[1]]:
#             ans[i] = ABC[nxt[0]]
#             s[nxt[0]] += 1
#             s[nxt[1]] -= 1
#         else:
#             ans[i] = ABC[nxt[1]]
#             s[nxt[0]] -= 1
#             s[nxt[1]] += 1
#     print('Yes')
#     print('\n'.join(ans))
if sum(s) == 2:
    if s.count(0) == 1:
        now = s.index(0)
        i = 0
    else:
        if T[0] != s.index(2):
            now = T[0]
            i = 1
            nxt = [0,1,2]
            nxt.remove(T[0])
            nxt.remove(s.index(2))
            ans[0] = ABC[nxt[0]]
        else:
            print('No')
            exit(0)
    while i < n-1:
        if T[i] == T[i+1]:
            if T[i] == now:
                ans[i] = ABC[(now-1)%3]
                ans[i+1] = ABC[(now-2)%3]
            else:
                for j in range(3):
                    if j not in [T[i],now]:
                        ans[i+1] = ABC[j]
                ans[i] = ABC[now]
            i += 2
        elif T[i] == now:
            for j in range(3):
                if j not in [T[i+1],now]:ans[i] = ABC[j]
            ans[i+1] = ABC[T[i]]
            now = T[i+1]
            i += 2
        else:
            ans[i] = ABC[now]
            for j in range(3):
                if j not in [T[i],now]:
                    now = j
                    break
            i += 1
    if i == n-1:
        if T[i] == now:
            for j in range(3):
                if j not in [now,T[i]]:ans[i] = ABC[j]
        else:
            ans[i] = ABC[now]
    print('Yes')
    print('\n'.join(ans))
else:
    for i,t in enumerate(T):
        nxt = [0,1,2]
        nxt.remove(t)
        if s[nxt[0]] < s[nxt[1]]:
            ans[i] = ABC[nxt[0]]
            s[nxt[0]] += 1
            s[nxt[1]] -= 1
        else:
            ans[i] = ABC[nxt[1]]
            s[nxt[0]] -= 1
            s[nxt[1]] += 1
        if -1 in s:
            print('No')
            break
    else:
        print('Yes')
        print('\n'.join(ans))

Buggy submission (verdict: Runtime Error):
s = [0,0,0]
n,s[0],s[1],s[2] = map(int,input().split())
T = [0]*n
ABC = 'ABC'
for i in range(n):
    t = input()
    if t == 'AB':T[i] = 2
    if t == 'BC':T[i] = 0
    if t == 'AC':T[i] = 1
ans = ['' for _ in range(n)]
# if s.count(0) <= 1 and sum(s) >= 3:
#     for i,t in enumerate(T):
#         nxt = [0,1,2]
#         nxt.remove(t)
#         if s[nxt[0]] < s[nxt[1]]:
#             ans[i] = ABC[nxt[0]]
#             s[nxt[0]] += 1
#             s[nxt[1]] -= 1
#         else:
#             ans[i] = ABC[nxt[1]]
#             s[nxt[0]] -= 1
#             s[nxt[1]] += 1
#     print('Yes')
#     print('\n'.join(ans))
if sum(s) == 2:
    if s.count(0) == 1:
        now = s.index(0)
        i = 0
    else:
        if T[0] != s.index(2):
            now = T[0]
            i = 1
            nxt = [0,1,2]
            nxt.remove(T[0])
            nxt.remove(s.index(2))
            ans[0] = ABC[nxt]
        else:
            print('No')
            exit(0)
    while i < n-1:
        if T[i] == T[i+1]:
            if T[i] == now:
                ans[i] = ABC[(now-1)%3]
                ans[i+1] = ABC[(now-2)%3]
            else:
                for j in range(3):
                    if j not in [T[i],now]:
                        ans[i+1] = ABC[j]
                ans[i] = ABC[now]
            i += 2
        elif T[i] == now:
            for j in range(3):
                if j not in [T[i+1],now]:ans[i] = ABC[j]
            ans[i+1] = ABC[T[i]]
            now = T[i+1]
            i += 2
        else:
            ans[i] = ABC[now]
            for j in range(3):
                if j not in [T[i],now]:
                    now = j
                    break
            i += 1
    if i == n-1:
        if T[i] == now:
            for j in range(3):
                if j not in [now,T[i]]:ans[i] = ABC[j]
        else:
            ans[i] = ABC[now]
    print('Yes')
    print('\n'.join(ans))
else:
    for i,t in enumerate(T):
        nxt = [0,1,2]
        nxt.remove(t)
        if s[nxt[0]] < s[nxt[1]]:
            ans[i] = ABC[nxt[0]]
            s[nxt[0]] += 1
            s[nxt[1]] -= 1
        else:
            ans[i] = ABC[nxt[1]]
            s[nxt[0]] -= 1
            s[nxt[1]] += 1
        if -1 in s:
            print('No')
            break
    else:
        print('Yes')
        print('\n'.join(ans))

Buggy submission (verdict: Runtime Error):
s = [0,0,0]
n,s[0],s[1],s[2] = map(int,input().split())
T = [0]*n
ABC = 'ABC'
for i in range(n):
    t = input()
    if t == 'AB':T[i] = 2
    if t == 'BC':T[i] = 0
    if t == 'AC':T[i] = 1
ans = ['' for _ in range(n)]
# if s.count(0) <= 1 and sum(s) >= 3:
#     for i,t in enumerate(T):
#         nxt = [0,1,2]
#         nxt.remove(t)
#         if s[nxt[0]] < s[nxt[1]]:
#             ans[i] = ABC[nxt[0]]
#             s[nxt[0]] += 1
#             s[nxt[1]] -= 1
#         else:
#             ans[i] = ABC[nxt[1]]
#             s[nxt[0]] -= 1
#             s[nxt[1]] += 1
#     print('Yes')
#     print('\n'.join(ans))
if s.count(0) == 1 and s.count(1) == 2:
    now = s.index(0)
    i = 0
    while i < n-1:
        if T[i] == T[i+1]:
            if T[i] == now:
                ans[i] = ABC[(now-1)%3]
                ans[i+1] = ABC[(now-2)%3]
            else:
                for j in range(3):
                    if j not in [T[i],now]:
                        ans[i+1] = ABC[j]
                ans[i] = ABC[now]
            i += 2
        elif T[i] == now:
            for j in range(3):
                if j not in [T[i+1],now]:ans[i] = ABC[j]
            ans[i+1] = ABC[T[i]]
            now = T[i+1]
            i += 2
        else:
            ans[i] = ABC[now]
            for j in range(3):
                if j not in [T[i],now]:
                    now = j
                    break
            i += 1
    if i == n-1:
        s[3] = 0
        if T[i] == now:
            for j in range(3):
                if j not in [now,T[i]]:ans[i] = ABC[j]
        else:
            ans[i] = ABC[now]
    print('Yes')
    print('\n'.join(ans))
else:
    for i,t in enumerate(T):
        nxt = [0,1,2]
        nxt.remove(t)
        if s[nxt[0]] < s[nxt[1]]:
            ans[i] = ABC[nxt[0]]
            s[nxt[0]] += 1
            s[nxt[1]] -= 1
        else:
            ans[i] = ABC[nxt[1]]
            s[nxt[0]] -= 1
            s[nxt[1]] += 1
        if -1 in s:
            print('No')
            break
    else:
        print('Yes')
        print('\n'.join(ans))

Buggy submission (verdict: Runtime Error):
s = [0,0,0]
n,s[0],s[1],s[2] = map(int,input().split())
T = [0]*n
ABC = 'ABC'
for i in range(n):
    t = input()
    if t == 'AB':T[i] = 2
    if t == 'BC':T[i] = 0
    if t == 'AC':T[i] = 1
ans = ['' for _ in range(n)]
# if s.count(0) <= 1 and sum(s) >= 3:
#     for i,t in enumerate(T):
#         nxt = [0,1,2]
#         nxt.remove(t)
#         if s[nxt[0]] < s[nxt[1]]:
#             ans[i] = ABC[nxt[0]]
#             s[nxt[0]] += 1
#             s[nxt[1]] -= 1
#         else:
#             ans[i] = ABC[nxt[1]]
#             s[nxt[0]] -= 1
#             s[nxt[1]] += 1
#     print('Yes')
#     print('\n'.join(ans))
if s.count(0) == 1 and s.count(1) == 2:
    s[3] = 0
    now = s.index(0)
    i = 0
    while i < n-1:
        if T[i] == T[i+1]:
            if T[i] == now:
                ans[i] = ABC[(now-1)%3]
                ans[i+1] = ABC[(now-2)%3]
            else:
                for j in range(3):
                    if j not in [T[i],now]:
                        ans[i+1] = ABC[j]
                ans[i] = ABC[now]
            i += 2
        elif T[i] == now:
            for j in range(3):
                if j not in [T[i+1],now]:ans[i] = ABC[j]
            ans[i+1] = ABC[T[i]]
            now = T[i+1]
            i += 2
        else:
            ans[i] = ABC[now]
            for j in range(3):
                if j not in [T[i],now]:
                    now = j
                    break
            i += 1
    if i == n-1:
        if T[i] == now:
            for j in range(3):
                if j not in [now,T[i]]:ans[i] = ABC[j]
        else:
            ans[i] = ABC[now]
    print('Yes')
    print('\n'.join(ans))
else:
    for i,t in enumerate(T):
        nxt = [0,1,2]
        nxt.remove(t)
        if s[nxt[0]] < s[nxt[1]]:
            ans[i] = ABC[nxt[0]]
            s[nxt[0]] += 1
            s[nxt[1]] -= 1
        else:
            ans[i] = ABC[nxt[1]]
            s[nxt[0]] -= 1
            s[nxt[1]] += 1
        if -1 in s:
            print('No')
            break
    else:
        print('Yes')
        print('\n'.join(ans))

Buggy submission (verdict: Runtime Error):
s = [0,0,0]
n,s[0],s[1],s[2] = map(int,input().split())
T = [0]*n
ABC = 'ABC'
for i in range(n):
    t = input()
    if t == 'AB':T[i] = 2
    if t == 'BC':T[i] = 0
    if t == 'AC':T[i] = 1
ans = ['' for _ in range(n)]
if s.count(0) <= 1 and sum(s) >= 3:
    for i,t in enumerate(T):
        nxt = [0,1,2]
        nxt.remove(t)
        if s[nxt[0]] < s[nxt[1]]:
            ans[i] = ABC[nxt[0]]
            s[nxt[0]] += 1
            s[nxt[1]] -= 1
        else:
            ans[i] = ABC[nxt[1]]
            s[nxt[0]] -= 1
            s[nxt[1]] += 1
    print('Yes')
    print('\n'.join(ans))
elif s.count(0) == 1:
    now = s.index(0)
    i = 0
    while i < n-1:
        if T[i] == T[i+1]:
            if T[i] == now:
                ans[i] = ABC[(now-1)%3]
                ans[i+1] = ABC[(now-2)%3]
            else:
                for j in range(3):
                    if j not in [T[i],now]:
                        ans[i+1] = ABC[j]
                ans[i] = now
            i += 2
        elif T[i] == now:
            for j in range(3):
                if j not in [T[i+1],now]:ans[i] = ABC[j]
            ans[i+1] = ABC[T[i]]
            now = T[i+1]
            i += 2
        else:
            ans[i] = ABC[now]
            for j in range(3):
                if j not in [T[i],now]:
                    now = j
                    break
            i += 1
    if i == n-1:
        ans[i] = ABC[(T[i]-1)%3]
    print('Yes')
    print('\n'.join(ans))
else:
    for i,t in enumerate(T):
        nxt = [0,1,2]
        nxt.remove(t)
        if s[nxt[0]] < s[nxt[1]]:
            ans[i] = ABC[nxt[0]]
            s[nxt[0]] += 1
            s[nxt[1]] -= 1
        else:
            ans[i] = ABC[nxt[1]]
            s[nxt[0]] -= 1
            s[nxt[1]] += 1
        if -1 in s:
            print('No')
            break
    else:
        print('Yes')
        print('\n'.join(ans))

Buggy submission (verdict: Runtime Error):
s = [0,0,0]
n,s[0],s[1],s[2] = map(int,input().split())
T = [0]*n
ABC = 'ABC'
for i in range(n):
    t = input()
    if t == 'AB':T[i] = 2
    if t == 'BC':T[i] = 0
    if t == 'AC':T[i] = 1
ans = ['' for _ in range(n)]
if s.count(0) <= 1 and sum(s) >= 3:
    for i,t in enumerate(T):
        nxt = [0,1,2]
        nxt.remove(t)
        if s[nxt[0]] < s[nxt[1]]:
            ans[i] = ABC[nxt[0]]
            s[nxt[0]] += 1
            s[nxt[1]] -= 1
        else:
            ans[i] = ABC[nxt[1]]
            s[nxt[0]] -= 1
            s[nxt[1]] += 1
    print('Yes')
    print('\n'.join(ans))
elif s.count(0) == 1:
    now = s.index(0)
    i = 0
    while i < n-1:
        if T[i] == T[i+1]:
            if T[i] == now:
                ans[i] = ABC[(now-1)%3]
                ans[i+1] = ABC[(now-2)%3]
            else:
                for j in range(3):
                    if j not in [T[i],now]:
                        ans[i+1] = ABC[j]
                ans[i] = now
            i += 2
        elif T[i] == now:
            for j in range(3):
                if j not in [T[i+1],now]:ans[i] = ABC[j]
            ans[i+1] = T[i]
            now = T[i+1]
            i += 2
        else:
            ans[i] = ABC[now]
            for j in range(3):
                if j not in [T[i],now]:
                    now = j
                    break
            i += 1
    if i == n-1:
        ans[i] = ABC[(T[i]-1)%3]
    print('Yes')
    print('\n'.join(ans))
else:
    for i,t in enumerate(T):
        nxt = [0,1,2]
        nxt.remove(t)
        if s[nxt[0]] < s[nxt[1]]:
            ans[i] = ABC[nxt[0]]
            s[nxt[0]] += 1
            s[nxt[1]] -= 1
        else:
            ans[i] = ABC[nxt[1]]
            s[nxt[0]] -= 1
            s[nxt[1]] += 1
        if -1 in s:
            print('No')
            break
    else:
        print('Yes')
        print('\n'.join(ans))