Problem p02692 (python)

Fixed submission:
n, a, b, c = list(map(int, input().split()))
ss = []
for i in range(n):
    ss.append(input())
ans = []
num_dic = {"A": a,"B": b, "C": c}

for i in range(n):
    s = ss[i]
    if num_dic[s[0]] == 0 and num_dic[s[1]] == 0:
        print("No")
        exit()
    elif num_dic[s[0]] > num_dic[s[1]]:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[1])
    elif num_dic[s[0]] < num_dic[s[1]]:
        num_dic[s[0]] += 1
        num_dic[s[1]] -= 1
        ans.append(s[0])
    elif i == n -1:
        ans.append(s[0])
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
    elif s[0] in ss[i+1]:
        num_dic[s[0]] += 1
        num_dic[s[1]] -= 1
        ans.append(s[0])
    else:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[1])
print("Yes")
for a in ans:
    print(a)

Buggy submission (verdict: Runtime Error):
n, a, b, c = list(map(int, input().split()))
ss = []
for i in range(n):
    ss.append(input())
ans = []
num_dic = {"A": a,"B": b, "C": c}

for i in range(n):
    s = ss[i]
    if num_dic[s[0]] == 0 and num_dic[s[1]] == 0:
        print("No")
        exit()
    elif num_dic[s[0]] > num_dic[s[1]]:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[1])
    elif num_dic[s[0]] < num_dic[s[1]]:
        num_dic[s[0]] += 1
        num_dic[s[1]] -= 1
        ans.append(s[0])
    elif i == n -1:
        ans.append(s[0])
    elif s[0] in s[i+1]:
        num_dic[s[0]] += 1
        num_dic[s[1]] -= 1
        ans.append(s[0])
    else:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[1])
print("Yes")
for a in ans:
    print(a)

Buggy submission (verdict: Runtime Error):
n, a, b, c = list(map(int, input().split()))
ss = []
for i in range(n):
    ss.append(input())
ans = []
num_dic = {"A": a,"B": b, "C": c}

for i in range(n):
    s = ss[i]
    if num_dic[s[0]] == 0 and num_dic[s[1]] == 0:
        print("No")
        import IPython;IPython.embed()
        exit()
    elif num_dic[s[0]] > num_dic[s[1]]:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[0])
    elif num_dic[s[0]] < num_dic[s[1]]:
        num_dic[s[0]] += 1
        num_dic[s[1]] -= 1
        ans.append(s[1])
    elif i == n -1:
        ans.append(s[0])
    elif s[0] in s[i+1]:
        num_dic[s[0]] += 1
        num_dic[s[1]] -= 1
        ans.append(s[0])
    else:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[1])
print("Yes")
for a in ans:
    print(a)

Buggy submission (verdict: Runtime Error):
n, a, b, c = list(map(int, input().split()))
ss = []
for i in range(n):
    ss.append(input())
ans = []
num_dic = {"A": a,"B": b, "C": c}

for i in range(n):
    s = ss[i]
    if num_dic[s[0]] == 0 and num_dic[s[1]] == 0:
        print("No")
        exit()
    elif num_dic[s[0]] > num_dic[s[1]]:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[0])
    elif num_dic[s[0]] > num_dic[s[1]]:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[1])
    elif i == n -1:
        ans.append(s[0])
    elif s[0] in s[i+1]:
        num_dic[s[0]] += 1
        num_dic[s[1]] -= 1
        ans.append(s[0])
    else:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[1])
for a in ans:
    print(a)

Buggy submission (verdict: Runtime Error):
n, a, b, c = list(map(int, input().split()))
ss = []
for i in range(n):
    ss.append(input())
ans = []
num_dic = {"A": a,"B":b, "C": c}

for i in range(n):
    s = ss[i]
    if num_dic[s[0]] == 0 and num_dic[s[1]] == 0:
        print("No")
        exit()
    elif num_dic[s[0]] > num_dic[s[1]]:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[0])
    elif num_dic[s[0]] > num_dic[s[1]]:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[1])
    elif i == n -1:
        ans.append(s[0])
    elif s[0] in S[i+1]:
        num_dic[s[0]] += 1
        num_dic[s[1]] -= 1
        ans.append(s[0])
    else:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[1])
for a in ans:
    print(a)

Buggy submission (verdict: Runtime Error):
n, a, b, c = list(map(int, input().split()))
ss = []
for i in range(n):
    ss.append(input())
ans = []
num_dic = {"A": a,"B":b, "C": c}
if num_dic["A"] + num_dic["B"] + num_dic["C"] == 0:
    print("No")
    exit()
if num_dic["A"] + num_dic["B"] + num_dic["C"] == 1:
    for i in range(n):
        s = ss[i]
        if num_dic[s[0]] + num_dic[s[1]] == 0:
            print("No")
            exit()
        else:
            if num_dic[s[0]] == 0:
                ans.append(s[0])
            else:
                ans.append(s[1])
    print("Yes")
    for a in ans:
        print(a)
if num_dic["A"] + num_dic["B"] + num_dic["C"] == 2:
    for i in range(n-1):
        s = ss[i]
        if num_dic[s[0]] + num_dic[s[1]] == 2:
            if num_dic[s[0]] == 1:
                if ss[i+1].find(s[0]) > -1:
                    num_dic[s[0]] = 2
                    num_dic[s[1]] = 0
                    ans.append(s[0])
                else:
                    num_dic[s[0]] = 0
                    num_dic[s[1]] = 2
                    ans.append(s[1])
            else:
                if num_dic[s[0]] == 0:
                    num_dic[s[0]] = 1
                    num_dic[s[1]] = 1
                    ans.append(s[0])
                else:
                    num_dic[s[0]] = 1
                    num_dic[s[1]] = 1
                    ans.append(s[1])
        elif num_dic[s[0]] + num_dic[s[1]] == 1:
            if num_dic[s[0]] == 0:
                ans.append(s[0])
                num_dic[s[0]] = 1
                num_dic[s[1]] = 0
            else:
                ans.append(s[1])
                num_dic[s[0]] = 0
                num_dic[s[1]] = 1
        elif num_dic[s[0]] + num_dic[s[1]] == 0:
            print("No")
            exit()
        else:
            if num_dic[s[0]] == 0:
                ans.append(s[0])
                num_dic[s[0]] = 1
                num_dic[s[1]] = num_dic[s[1]] - 1
            else:
                ans.append(s[1])
                num_dic[s[0]] = num_dic[s[0]] - 1
                num_dic[s[1]] = num_dic[s[1]] + 1
    if num_dic[s[i+1][0]] + num_dic[s[i+1][1]] == 0:
        print("No")
        exit()
    if num_dic[s[i+1][0]] != 0:
        ans.append(s[i+1][0])
    else:
        ans.append(s[i+1][1])
    print("Yes")
    for a in ans:
        print(a)
else:
    for i in range(n):
        s = ss[i]
        if num_dic[s[0]] == 0 and num_dic[s[1]] == 0:
            print("No")
            exit()
        if num_dic[s[0]] == 0:
            ans.append(s[0])
            
            num_dic[s[0]] = 1
            num_dic[s[1]] = num_dic[s[1]] - 1
        elif num_dic[s[0]] == 0:
            ans.append(s[1])
            num_dic[s[0]] = num_dic[s[0]] - 1
            num_dic[s[1]] = 1
        else:
            if num_dic[s[0]] >= num_dic[s[1]]:
                ans.append(s[1])
                num_dic[s[0]] = num_dic[s[0]] - 1
                num_dic[s[1]] = num_dic[s[1]] + 1
            else:
                ans.append(s[0])
                num_dic[s[0]] = num_dic[s[0]] + 1
                num_dic[s[1]] = num_dic[s[1]] - 1
    print("Yes")
    for a in ans:
        print(a)