Buggy python submission (verdict: Runtime Error):


n, a, b, c = map(int, input().split())
ss = [None] * n
for i in range(n):
    ss[i] = input()

abc = {"A": a, "B": b, "C": c}
ans = [None] * n
# 3つある→ aをなくしにかかる
# 2つある→ bを1にしにかかる
# 0, 1, c→ c>=2のとき、
No = False
for i,s in enumerate(ss):
    zero = [i for i, item in abc.items() if item==0]
    one = [i for i, item in abc.items() if item==1]
    two = [i for i, item in abc.items() if item>=2]
#     print(s)
#     print(zero, one, two)
    if len(zero)==0:
        if s in ["AB", "AC"]:
            ans[i] = "A"
            abc["A"] += 1
            abc[s[1]] -= 1
        else:
            ans[i] = "C"
            abc["C"] += 1
            abc["B"] -= 1
    elif len(zero)==1 and len(two)>=1:
        z = zero[0]
        t = two[0]
        if z in s:
            # zに加える
            ans[i] = z
            abc[z] += 1
            abc[s.replace(z, "")] -= 1
        else:
            # twoから引く
            tmp = s.replace(t, "")
            ans[i] = tmp
            abc[tmp] += 1
            abc[t] -= 1
    elif len(zero)==1 and len(one)==2:
        z = zero[0]
        if s[0] in one and s[1] in one:
            # １手先を見て、
            ns = ss[i+1] if i+1<=n-1 else None
            if (z not in ns) or ns is None:
                # 次も同じなのでどちらでもよい
                ans[i] = one[0]
                abc[one[0]] += 1
                abc[one[1]] -= 1
            else:
                # nsのzでないほうに加える
                tmp = ns.replace(z, "")
                ans[i] = tmp
                abc[tmp] += 1
                abc[s.replace(tmp, "")] -= 1
        else:
            # 0に加える
            z = zero[0]
            ans[i] = z
            abc[z] += 1
            abc[s.replace(z, "")] -= 1
    else:
        # 0が２つ以上
        assert len(zero) >= 2
        if len(zero)==3:
            No = True
            break
        elif s[0] in zero and s[1] in zero:
            No = True
            break
        else:
            # zに加える
            z = (set(s) & set(zero)).pop()
            ans[i] = z
            abc[z] += 1
            abc[s.replace(z, "")] -= 1
            
if No:
    print("No")
else:
    print("Yes")
    print("\n".join(ans))

Fixed submission:


n, a, b, c = map(int, input().split())
ss = [None] * n
for i in range(n):
    ss[i] = input()

abc = {"A": a, "B": b, "C": c}
ans = [None] * n
# 3つある→ aをなくしにかかる
# 2つある→ bを1にしにかかる
# 0, 1, c→ c>=2のとき、
No = False
for i,s in enumerate(ss):
    zero = [i for i, item in abc.items() if item==0]
    one = [i for i, item in abc.items() if item==1]
    two = [i for i, item in abc.items() if item>=2]
#     print(s)
#     print(zero, one, two)
    if len(zero)==0:
        if s in ["AB", "AC"]:
            ans[i] = "A"
            abc["A"] += 1
            abc[s[1]] -= 1
        else:
            ans[i] = "C"
            abc["C"] += 1
            abc["B"] -= 1
    elif len(zero)==1 and len(two)>=1:
        z = zero[0]
        t = two[0]
        if z in s:
            # zに加える
            ans[i] = z
            abc[z] += 1
            abc[s.replace(z, "")] -= 1
        else:
            # twoから引く
            tmp = s.replace(t, "")
            ans[i] = tmp
            abc[tmp] += 1
            abc[t] -= 1
    elif len(zero)==1 and len(one)==2:
        z = zero[0]
        if s[0] in one and s[1] in one:
            # １手先を見て、
            ns = ss[i+1] if i+1<=n-1 else None
            if (ns is None) or (z not in ns):
                # 次も同じなのでどちらでもよい
                ans[i] = one[0]
                abc[one[0]] += 1
                abc[one[1]] -= 1
            else:
                # nsのzでないほうに加える
                tmp = ns.replace(z, "")
                ans[i] = tmp
                abc[tmp] += 1
                abc[s.replace(tmp, "")] -= 1
        else:
            # 0に加える
            z = zero[0]
            ans[i] = z
            abc[z] += 1
            abc[s.replace(z, "")] -= 1
    else:
        # 0が２つ以上
        assert len(zero) >= 2
        if len(zero)==3:
            No = True
            break
        elif s[0] in zero and s[1] in zero:
            No = True
            break
        else:
            # zに加える
            z = (set(s) & set(zero)).pop()
            ans[i] = z
            abc[z] += 1
            abc[s.replace(z, "")] -= 1
            
if No:
    print("No")
else:
    print("Yes")
    print("\n".join(ans))

Buggy python submission (verdict: Runtime Error):


n, a, b, c = map(int, input().split())
ss = [None] * n
for i in range(n):
    ss[i] = input()

abc = {"A": a, "B": b, "C": c}
ans = [None] * n
# 3つある→ aをなくしにかかる
# 2つある→ bを1にしにかかる
# 0, 1, c→ c>=2のとき、
No = False
for i,s in enumerate(ss):
    zero = [i for i, item in abc.items() if item==0]
    one = [i for i, item in abc.items() if item==1]
    two = [i for i, item in abc.items() if item>=2]
#     print(s)
#     print(zero, one, two)
    if len(zero)==0:
        if s in ["AB", "AC"]:
            ans[i] = "A"
            abc["A"] += 1
            abc[s[1]] -= 1
        else:
            ans[i] = "C"
            abc["C"] += 1
            abc["B"] -= 1
    elif len(zero)==1 and len(two)>=1:
        z = zero[0]
        t = two[0]
        if z in s:
            # zに加える
            ans[i] = z
            abc[z] += 1
            abc[s.replace(z, "")] -= 1
        else:
            # twoから引く
            tmp = s.replace(t, "")
            ans[i] = tmp
            abc[tmp] += 1
            abc[t] -= 1
    elif len(zero)==1 and len(one)==2:
        z = zero[0]
        if s[0] in one and s[1] in one:
            # １手先を見て、
            ns = ss[i+1]
            if z not in ns:
                # 次も同じなのでどちらでもよい
                ans[i] = one[0]
                abc[one[0]] += 1
                abc[one[1]] -= 1
            else:
                # nsのzでないほうに加える
                tmp = ns.replace(z, "")
                ans[i] = tmp
                abc[tmp] += 1
                abc[s.replace(tmp, "")] -= 1
        else:
            # 0に加える
            z = zero[0]
            ans[i] = z
            abc[z] += 1
            abc[s.replace(z, "")] -= 1
    else:
        # 0が２つ以上
        assert len(zero) >= 2
        if len(zero)==3:
            No = True
            break
        elif s[0] in zero and s[1] in zero:
            No = True
            break
        else:
            # zに加える
            z = (set(s) & set(zero)).pop()
            ans[i] = z
            abc[z] += 1
            abc[s.replace(z, "")] -= 1
            
if No:
    print("No")
else:
    print("Yes")
    print("\n".join(ans))

Fixed submission:


n, a, b, c = map(int, input().split())
ss = [None] * n
for i in range(n):
    ss[i] = input()

abc = {"A": a, "B": b, "C": c}
ans = [None] * n
# 3つある→ aをなくしにかかる
# 2つある→ bを1にしにかかる
# 0, 1, c→ c>=2のとき、
No = False
for i,s in enumerate(ss):
    zero = [i for i, item in abc.items() if item==0]
    one = [i for i, item in abc.items() if item==1]
    two = [i for i, item in abc.items() if item>=2]
#     print(s)
#     print(zero, one, two)
    if len(zero)==0:
        if s in ["AB", "AC"]:
            ans[i] = "A"
            abc["A"] += 1
            abc[s[1]] -= 1
        else:
            ans[i] = "C"
            abc["C"] += 1
            abc["B"] -= 1
    elif len(zero)==1 and len(two)>=1:
        z = zero[0]
        t = two[0]
        if z in s:
            # zに加える
            ans[i] = z
            abc[z] += 1
            abc[s.replace(z, "")] -= 1
        else:
            # twoから引く
            tmp = s.replace(t, "")
            ans[i] = tmp
            abc[tmp] += 1
            abc[t] -= 1
    elif len(zero)==1 and len(one)==2:
        z = zero[0]
        if s[0] in one and s[1] in one:
            # １手先を見て、
            ns = ss[i+1] if i+1<=n-1 else None
            if (ns is None) or (z not in ns):
                # 次も同じなのでどちらでもよい
                ans[i] = one[0]
                abc[one[0]] += 1
                abc[one[1]] -= 1
            else:
                # nsのzでないほうに加える
                tmp = ns.replace(z, "")
                ans[i] = tmp
                abc[tmp] += 1
                abc[s.replace(tmp, "")] -= 1
        else:
            # 0に加える
            z = zero[0]
            ans[i] = z
            abc[z] += 1
            abc[s.replace(z, "")] -= 1
    else:
        # 0が２つ以上
        assert len(zero) >= 2
        if len(zero)==3:
            No = True
            break
        elif s[0] in zero and s[1] in zero:
            No = True
            break
        else:
            # zに加える
            z = (set(s) & set(zero)).pop()
            ans[i] = z
            abc[z] += 1
            abc[s.replace(z, "")] -= 1
            
if No:
    print("No")
else:
    print("Yes")
    print("\n".join(ans))