Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import sys
M = 1001; p = [1]*M;
p[0],p[1] = 0,0
for i in xrange(31):
    if p[i]:
        for j in xrange(i*i,M,i):
            p[j] = 0
for s in sys.stdin.readlines():
    n = int(s); ans = 0;
    for i in xrange(1,n+1):
        if p[i] and p[n+1-i]:
            ans += 1
    print ans

Fixed submission:
M = 10001; p = [1]*M;
p[0],p[1] = 0,0
for i in xrange(2,101):
    if p[i]:
        for j in xrange(i*i,M,i):
            p[j] = 0
while 1:
    try:
        n = int(raw_input()); ans = 0;
        for i in xrange(1,n+1):
            if p[i] and p[n+1-i]:
                ans += 1
        print ans
    except EOFError:
        break

Buggy python submission (verdict: Runtime Error):
import sys
M = 1001; p = [1]*M;
p[0],p[1] = 0,0
for i in xrange(31):
    if p[i]:
        for j in xrange(i*i,M,i):
            p[j] = 0
for s in sys.stdin.readlines():
    n = int(s.strip()); ans = 0;
    for i in xrange(1,n+1):
        if p[i] and p[n+1-i]:
            ans += 1
    print ans

Fixed submission:
M = 10001; p = [1]*M;
p[0],p[1] = 0,0
for i in xrange(2,101):
    if p[i]:
        for j in xrange(i*i,M,i):
            p[j] = 0
while 1:
    try:
        n = int(raw_input()); ans = 0;
        for i in xrange(1,n+1):
            if p[i] and p[n+1-i]:
                ans += 1
        print ans
    except EOFError:
        break

Buggy python submission (verdict: Runtime Error):
M = 1001; p = [1]*M;
p[0],p[1] = 0,0
for i in xrange(32):
    if p[i]:
        for j in xrange(i*i,M,i):
            p[j] = 0
while 1:
    try:
        n = int(raw_input()); ans = 0;
        for i in xrange(1,n+1):
            if p[i] and p[n+1-i]:
                ans += 1
        print ans
    except EOFError:
        break

Fixed submission:
M = 10001; p = [1]*M;
p[0],p[1] = 0,0
for i in xrange(2,101):
    if p[i]:
        for j in xrange(i*i,M,i):
            p[j] = 0
while 1:
    try:
        n = int(raw_input()); ans = 0;
        for i in xrange(1,n+1):
            if p[i] and p[n+1-i]:
                ans += 1
        print ans
    except EOFError:
        break
Buggy python submission (verdict: Time Limit Exceeded):
while 1:
    n = input()
    if n==0:
        break
    w, h = map(int, raw_input().split())
    p = [map(int, raw_input().split()) for i in xrange(n)]
    s, t = map(int, raw_input().split())
    ans = 0
    for b in xrange(1, h-t+2):
        for a in xrange(w-s+2):
            cnt = 0
            for c, d in p:
                if 0 <= c-a < s and 0 <= d-b < t:
                    cnt += 1
            ans = max(ans, cnt)
    print ans

Fixed submission:
while 1:
    n = input()
    if n==0:
        break
    w, h = map(int, raw_input().split())
    m = [[0]*w for i in range(h)]
    l = [[0]*(w+1) for i in range(h+1)]
    for i in xrange(n):
        x, y = map(int, raw_input().split())
        m[y-1][x-1] = 1
    for i in xrange(h):
        temp = 0
        for j in xrange(w):
            temp += m[i][j]
            l[i+1][j+1] = l[i][j+1] + temp

    s, t = map(int, raw_input().split())
    ans = 0
    for i in xrange(h-t+1):
        for j in xrange(w-s+1):
            ans = max(ans, l[i+t][j+s] - l[i][j+s] - l[i+t][j] + l[i][j])
    print ans
Buggy python submission (verdict: Runtime Error):
from math import sqrt
from collections import deque
M = 10**6
prime = [1]*(M+1)
prime[0] = prime[1] = 0
for i in xrange(2, int(sqrt(M))+1):
    if prime[i]:
        for j in xrange(i*i, M+1, i):
            prime[j] = 0
P = {(0, 0): 1}; R = [None, (0, 0)]
c = 1; i = 1; p = 1
x = y = 0
while c < M:
    for j in xrange(i):
        x += p; c += 1
        P[x, y] = c; R.append((x, y))
    for j in xrange(i):
        y -= p; c += 1
        P[x, y] = c; R.append((x, y))
    p = -p
    i += 1
deq = deque()
R = [-1, 0, 1]
while 1:
    m, n = map(int, raw_input().split())
    if m == n == 0:
        break
    deq.append(n)
    cnts = {n: prime[n]}
    used = set()
    while deq:
        v = deq.popleft()
        x, y = R[v]
        for dx in R:
            t = P.get((x+dx, y+1), M+1)
            if t <= m:
                cnts[t] = max(cnts[v] + prime[t], cnts.get(t, 0))
                if t not in used:
                    deq.append(t)
                    used.add(t)
    vals = sorted((v, k) for k, v in cnts.items() if prime[k])
    print "%d %d" % vals[-1] if vals else "0 0"

Fixed submission:
from math import sqrt
from collections import deque
M = 10**6
prime = [1]*(M+1)
prime[0] = prime[1] = 0
for i in xrange(2, int(sqrt(M))+1):
    if prime[i]:
        for j in xrange(i*i, M+1, i):
            prime[j] = 0
P = {(0, 0): 1}; R = [None, (0, 0)]
c = 1; i = 1; p = 1
x = y = 0
while c < M:
    for j in xrange(i):
        x += p; c += 1
        P[x, y] = c; R.append((x, y))
    for j in xrange(i):
        y -= p; c += 1
        P[x, y] = c; R.append((x, y))
    p = -p
    i += 1
deq = deque()
dd = [-1, 0, 1]
while 1:
    m, n = map(int, raw_input().split())
    if m == n == 0:
        break
    deq.append(n)
    cnts = {n: prime[n]}
    used = set()
    while deq:
        v = deq.popleft()
        x, y = R[v]
        for dx in dd:
            t = P.get((x+dx, y+1), M+1)
            if t <= m:
                cnts[t] = max(cnts[v] + prime[t], cnts.get(t, 0))
                if t not in used:
                    deq.append(t)
                    used.add(t)
    vals = sorted((v, k) for k, v in cnts.items() if prime[k])
    print "%d %d" % vals[-1] if vals else "0 0"
Buggy python submission (verdict: Runtime Error):
def dfs(dg, gs, ms, v):
    me = ms[v]
    for i in xrange(len(me)):
        if me[i] in dg:
            ret = dfs(dg, gs, ms, gs.index(me[i]))
            for e in ret:
                if e not in dg[gs[v]]:
                    dg[gs[v]].append(e)
        else:
            if me[i] not in dg[gs[v]]:
                dg[gs[v]].append(me[i])
    return dg[gs[v]]

while 1:
    n = input()
    if not n:
        break
    gs = []; ms = []
    dg = {}
    for i in xrange(n):
        g, m = raw_input().split(":")
        gs.append(g); ms.append(m[:-1].split(","))
        dg[g] = []

    dfs(dg, gs, ms, 0)
    print len(dg[gs[0]])

Fixed submission:
def dfs(dg, gs, ms, used, v):
    if used[v]:
        return dg[gs[v]]
    used[v] = 1
    me = ms[v]
    for i in xrange(len(me)):
        if me[i] in dg:
            ret = dfs(dg, gs, ms, used, gs.index(me[i]))
            for e in ret:
                if e not in dg[gs[v]]:
                    dg[gs[v]].append(e)
        else:
            if me[i] not in dg[gs[v]]:
                dg[gs[v]].append(me[i])
    return dg[gs[v]]

while 1:
    n = input()
    if not n:
        break
    gs = []; ms = []
    dg = {}
    for i in xrange(n):
        g, m = raw_input().split(":")
        gs.append(g); ms.append(m[:-1].split(","))
        dg[g] = []

    dfs(dg, gs, ms, [0]*n, 0)
    print len(dg[gs[0]])
Buggy python submission (verdict: Runtime Error):
while 1:
    n = input()
    if n == 0:
        break
    status = {}
    god_st = 0
    res = {}
    for i in xrange(n):
        s = raw_input().split()
        #M, D = map(int, s[0].split("/"))
        h, m = map(int, s[1].split(":"))
        mi = 60*h + m
        st = s[2] == 'I'
        ID = s[3]

        if ID == '000':
            # goddness
            if st == 1:
                god_st = mi
            else:
                for k in status:
                    if k != ID and status[k] != -1:
                        res[k] = res.get(k, 0) + (mi - max(god_st, status[k]))
        else:
            if st == 1:
                status[ID] = mi
            else:
                status[ID] = -1
    print max(res.values())

Fixed submission:
while 1:
    n = input()
    if n == 0:
        break
    status = {}
    god_st = -1
    res = {}
    for i in xrange(n):
        s = raw_input().split()
        #M, D = map(int, s[0].split("/"))
        h, m = map(int, s[1].split(":"))
        mi = 60*h + m
        st = s[2] == 'I'
        ID = s[3]

        if ID == '000':
            # goddness
            if st == 1:
                god_st = mi
            else:
                for k in status:
                    if k != ID and status[k] != -1:
                        res[k] = res.get(k, 0) + (mi - max(god_st, status[k]))
                god_st = -1
        else:
            if st == 1:
                status[ID] = mi
            else:
                if god_st != -1:
                    res[ID] = res.get(ID, 0) + (mi - max(god_st, status[ID]))
                status[ID] = -1
    print max(res.values()) if res else 0
Buggy python submission (verdict: Runtime Error):
def root(x):
    if x == parent[x]:
        return x
    y = root(parent[x])
    relative[x] += relative[parent[x]]
    parent[x] = y
    return y
def unite(a, b, w):
    pa = root(a); pb = root(b)
    if pa < pb:
        parent[pb] = pa
        relative[pb] = relative[a] + w
    else:
        parent[pa] = pb
        relative[pa] = relative[b] - w
while 1:
    N, M = map(int, input().split())
    if N == M == 0:
        break
    *parent, = range(N+1)
    relative = [0]*(N+1)
    for i in range(M):
        cmd = input().split()
        if cmd[0] == '!':
            a, b, w = map(int, cmd[1:])
            unite(a-1, b-1, w)
        else:
            a, b = map(int, cmd[1:])
            if root(a-1) != root(b-1):
                print("UNKNOWN")
            else:
                print(relative[b-1] - relative[a-1])

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
def root(x):
    if x == parent[x]:
        return x
    y = root(parent[x])
    relative[x] += relative[parent[x]]
    parent[x] = y
    return y
def unite(a, b, w):
    pa = root(a); pb = root(b)
    pw = relative[a] + w - relative[b]
    if pa < pb:
        parent[pb] = pa
        relative[pb] = pw
    else:
        parent[pa] = pb
        relative[pa] = -pw
while 1:
    N, M = map(int, input().split())
    if N == M == 0:
        break
    *parent, = range(N)
    relative = [0]*N
    for i in range(M):
        cmd = input().split()
        if cmd[0] == '!':
            a, b, w = map(int, cmd[1:])
            unite(a-1, b-1, w)
        else:
            a, b = map(int, cmd[1:])
            if root(a-1) != root(b-1):
                print("UNKNOWN")
            else:
                print(relative[b-1] - relative[a-1])
Buggy python submission (verdict: Runtime Error):
from bisect import bisect
n, a, b, q = map(int, input().split())

W = [input().split() for i in range(a)]
X = [int(x) for x, c in W]
C = [c for x, c in W]

P = [list(map(int, input().split())) for i in range(b)]
Y = [y for y, h in P] + [n+1]
D = [0]*b

for i in range(b):
    y0, h = P[i]
    y1 = Y[i+1]
    l = y1 - y0
    if y0 < h + l:
        D[i] = y0 - h
    else:
        D[i] = l

Z = [int(input()) for i in range(q)]

idx = 0
S = {}
for i in range(a):
    x = X[i]; c = C[i]
    S[x] = c
    while Y[idx+1] < x: idx += 1
    i = idx
    while Y[0] <= x:
        while x < Y[i]: i -= 1
        y0, h = P[i]; y1 = Y[i+1]
        if h == 0: break
        x = h + ((x - y0) % D[i])
        S[x] = c
ans = []
for z in Z:
    if z not in S:
        i = bisect(Y, z)
        while Y[0] <= z:
            while z < Y[i]: i -= 1
            y0, h = P[i]; y1 = Y[i+1]
            if h == 0: break
            z = h + ((z - y0) % D[i])
            if z in S:
                break
    if z in S:
        ans.append(S[z])
    else:
        ans.append('?')
print(*ans, sep='')

Fixed submission:
from bisect import bisect
n, a, b, q = map(int, input().split())

W = [input().split() for i in range(a)]
X = [int(x) for x, c in W]
C = [c for x, c in W]

P = [list(map(int, input().split())) for i in range(b)]
Y = [y for y, h in P] + [n+1]
D = [0]*b

for i in range(b):
    y0, h = P[i]; y1 = Y[i+1]
    D[i] = min(y0 - h, y1 - y0)

idx = 0
S = {}
for i in range(a):
    x = X[i]; c = C[i]
    S[x] = c
    if x < Y[0]:
        continue
    while Y[idx+1] <= x: idx += 1
    i = idx
    while Y[0] <= x:
        while x < Y[i]: i -= 1
        y0, h = P[i]
        if h == 0: break
        x = h + ((x - y0) % D[i])
        S[x] = c
def check(z):
    if z in S:
        return S[z]
    i = bisect(Y, z)-1
    while Y[0] <= z:
        while z < Y[i]: i -= 1
        y0, h = P[i]
        if h == 0: break
        z = h + ((z - y0) % D[i])
        if z in S:
            return S[z]
    return '?'

Z = (int(input()) for i in range(q))
print(*map(check, Z), sep='')
Buggy python submission (verdict: Runtime Error):
while 1:
    s1 = input().split('"')
    if s1 is '.':
        break
    s2 = input().split('"')
    if len(s1) != len(s2):
        print("DIFFERENT")
        continue
    r = [0]*2
    for i in range(len(s1)):
        if s1[i] != s2[i]:
            r[i%2] += 1
    if [0,1]<r:
        print("DIFFERENT")
    else:
        print(["IDENTICAL","CLOSE"][r[1]])

Fixed submission:
while 1:
    s1 = input().split('"')
    if s1 == ['.']:
        break
    s2 = input().split('"')
    if len(s1) != len(s2):
        print("DIFFERENT")
        continue
    r = [0]*2
    for i in range(len(s1)):
        if s1[i] != s2[i]:
            r[i%2] += 1
    if [0,1]<r:
        print("DIFFERENT")
    else:
        print(["IDENTICAL","CLOSE"][r[1]])
Buggy python submission (verdict: Runtime Error):
from itertools import permutations
for t in xrange(input()):
    h, w = map(int, raw_input().split())
    A = [list(raw_input()) for i in xrange(h)]
    xmin = {}; xmax = {}
    ymin = {}; ymax = {}
    obj = set()
    for i in xrange(h):
        for j in xrange(w):
            c = A[i][j]
            if c == '.': continue
            xmin[c] = min(xmin.get(c, 99), j)
            xmax[c] = max(xmax.get(c, -1), j)
            ymin[c] = min(ymin.get(c, 99), i)
            ymax[c] = max(ymax.get(c, -1), i)
            obj.add(c)
    if not obj:
        print "SAFE"
        continue
    ok = 1
    for q in obj:
        for i in xrange(ymin[q], ymax[q]+1):
            for j in xrange(xmin[q], xmax[q]+1):
                if A[i][j] == '.':
                    ok = 0
                    break
            if not ok: break
        if not ok: break
    if not ok:
        print "SUSPICIOUS"
        continue
    for p in permutations(obj):
        valid = [[1]*w for i in xrange(h)]
        ok = 1
        for q in p:
            for i in xrange(ymin[q], ymax[q]+1):
                for j in xrange(xmin[q], xmax[q]+1):
                    if A[i][j] != q and not valid[i][j]:
                        ok = 0
                        break
                    elif A[i][j] == q:
                        valid[i][j] = 0
                if not ok: break
            if not ok: break
        if ok:
            print "SAFE"
            break
    else:
        print "SUSPICIOUS"

Fixed submission:
for t in xrange(input()):
    h, w = map(int, raw_input().split())
    A = [list(raw_input()) for i in xrange(h)]
    xmin = {}; xmax = {}
    ymin = {}; ymax = {}
    obj = set()
    for i in xrange(h):
        for j in xrange(w):
            c = A[i][j]
            if c == '.': continue
            xmin[c] = min(xmin.get(c, 99), j)
            xmax[c] = max(xmax.get(c, -1), j)
            ymin[c] = min(ymin.get(c, 99), i)
            ymax[c] = max(ymax.get(c, -1), i)
            obj.add(c)
    if not obj:
        print "SAFE"
        continue
    ok = 1
    g = {e: set() for e in obj}
    deg = {e: 0 for e in obj}
    for q in obj:
        for i in xrange(ymin[q], ymax[q]+1):
            for j in xrange(xmin[q], xmax[q]+1):
                c = A[i][j]
                if c == '.':
                    ok = 0
                    break
                if c != q:
                    g[q].add(c)
            if not ok: break
        if not ok: break
    if not ok:
        print "SUSPICIOUS"
        continue
    for e in obj:
        for t in g[e]:
            deg[t] += 1
    cnt = len(obj)
    used = set()
    for e in obj:
        for e in obj:
            if not deg[e] and e not in used:
                for t in g[e]:
                    deg[t] -= 1
                used.add(e)
                cnt -= 1
    if cnt:
        print "SUSPICIOUS"
    else:
        print "SAFE"
Buggy python submission (verdict: Runtime Error):
from heapq import heappush, heappop
while 1:
    N, M, L, K, A, H = map(int, raw_input().split())
    if N == 0:
        break
    F = [0]*N
    if L>0:
        for e in map(int, raw_input().split()):
            F[e] = 1
    G = [[] for i in xrange(N)]
    for i in xrange(K):
        x, y, t = map(int, raw_input().split())
        G[x].append((y, t))
        G[y].append((x, t))
    dist = [[10**18]*(M+1) for i in xrange(N)]
    dist[A][M] = 0
    que = [(0, A, M)]
    while que:
        cost, v, rest = heappop(que)
        if dist[v][rest] < cost:
            continue
        if F[v] and rest < M and cost + 1 < dist[v][rest+1]:
            dist[v][rest+1] = cost+1
            heappush(que, (cost+1, v, rest+1))
        for t, co in G[v]:
            if rest>=co and cost + co < dist[t][rest-co]:
                dist[t][rest-co] = cost + co
                heappush(que, (cost+co, t, rest-co))
    ans = min(dist[H])
    print "Help!" if ans > 10**17 else ans

Fixed submission:
from heapq import heappush, heappop
while 1:
    N, M, L, K, A, H = map(int, raw_input().split())
    if N == 0:
        break
    F = [0]*N
    for e in map(int, raw_input().split()):
        F[e] = 1
    G = [[] for i in xrange(N)]
    for i in xrange(K):
        x, y, t = map(int, raw_input().split())
        G[x].append((y, t))
        G[y].append((x, t))
    dist = [[10**18]*(M+1) for i in xrange(N)]
    dist[A][M] = 0
    que = [(0, A, M)]
    while que:
        cost, v, rest = heappop(que)
        if dist[v][rest] < cost:
            continue
        if F[v] and rest < M and cost + 1 < dist[v][rest+1]:
            dist[v][rest+1] = cost+1
            heappush(que, (cost+1, v, rest+1))
        for t, co in G[v]:
            if rest>=co and cost + co < dist[t][rest-co]:
                dist[t][rest-co] = cost + co
                heappush(que, (cost+co, t, rest-co))
    ans = min(dist[H])
    print "Help!" if ans > 10**17 else ans
Buggy python submission (verdict: Runtime Error):
ef suber(c, k):
    if ord('a')<=ord(c):
        w = ord(c) - ord('a')
        if w < k:
            return chr(ord('Z') - (k - w - 1)) if k<=26 else chr(ord('z') - (k - w - 27))
        return chr(ord('a') + (w - k))
    else:
        w = ord(c) - ord('A')
        if w < k:
            return chr(ord('z') - (k - w - 1)) if k<=26 else chr(ord('Z') - (k - w - 27))
        return chr(ord('A') + (w - k))
while True:
    n = input()
    if n==0:
        break
    k = map(int, raw_input().split())
    s = raw_input()
    print "".join(suber(s[i], k[i%n]) for i in xrange(len(s)))

Fixed submission:
station = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
while True:
    n = input()
    if n==0:
        break
    k = map(int, raw_input().split())
    s = raw_input()
    print "".join(station[(station.find(s[i])-k[i%n]+52)%52] for i in xrange(len(s)))

Buggy python submission (verdict: Runtime Error):
station = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQESTUVWXYZ"
while True:
    n = input()
    if n==0:
        break
    k = map(int, raw_input().split())
    s = raw_input()
    print "".join(station[station.find(s[i])-k[i%n]] for i in xrange(len(s)))

Fixed submission:
station = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
while True:
    n = input()
    if n==0:
        break
    k = map(int, raw_input().split())
    s = raw_input()
    print "".join(station[(station.find(s[i])-k[i%n]+52)%52] for i in xrange(len(s)))
Buggy python submission (verdict: Runtime Error):
while 1:
    n = int(input())
    S = []
    for i in range(n):
        s = input()
        b = []
        for j in range(len(s)):
            if j < 1 or s[j-1] in "aiueo":
                b.append(s[j])
        S.append(b)
    ans = 0
    S.sort()
    for i in range(n-1):
        A = S[i]; B = S[i+1]
        if A == B:
            print(-1)
            break
        res = min(len(A), len(B))
        for j in range(res):
            if A[j] != B[j]:
                res = j
                break
        ans = max(ans, res+1)
    else:
        print(ans)

Fixed submission:
while 1:
    n = int(input())
    if n == 0:
        break
    S = []
    for i in range(n):
        s = input()
        b = []
        for j in range(len(s)):
            if j < 1 or s[j-1] in "aiueo":
                b.append(s[j])
        S.append(b)
    ans = 0
    S.sort()
    for i in range(n-1):
        A = S[i]; B = S[i+1]
        if A == B:
            print(-1)
            break
        res = min(len(A), len(B))
        for j in range(res):
            if A[j] != B[j]:
                res = j
                break
        ans = max(ans, res+1)
    else:
        print(ans)
Buggy python submission (verdict: Runtime Error):
s = raw_input()
used = set()
def dfs(s):
    if s in used:
        return 0
    if s == "ABC":
        return 1
    if "ABC" in s:
        for c in "ABC":
            if dfs(s.replace("ABC", c)):
                return 1
    used.add(s)
    return 0
print dfs(s)*"Yes"or"No"

Fixed submission:
s = raw_input()
p = "ABC"
used = set()

while s != p:
    idx = 0
    c = [0]*3
    while idx < len(s):
        if idx+3 <= len(s) and s[idx:idx+3] == p:
            idx += 3
        else:
            c[p.index(s[idx])] += 1
            idx += 1
    if sum(e == 0 for e in c) != 1 or sum(c) == len(s):
        print "No"
        exit(0)
    s = s.replace("ABC", p[[i for i in [0,1,2] if c[i]==0][0]])
print "Yes"

Buggy python submission (verdict: Runtime Error):
import sys
s = raw_input()
used = set()
sys.setrecursionlimit(len(s))
def dfs(s):
    if s in used:
        return 0
    if s == "ABC":
        return 1
    if "ABC" in s:
        for c in "ABC":
            if dfs(s.replace("ABC", c)):
                return 1
    used.add(s)
    return 0
print dfs(s)*"Yes"or"No"

Fixed submission:
s = raw_input()
p = "ABC"
used = set()

while s != p:
    idx = 0
    c = [0]*3
    while idx < len(s):
        if idx+3 <= len(s) and s[idx:idx+3] == p:
            idx += 3
        else:
            c[p.index(s[idx])] += 1
            idx += 1
    if sum(e == 0 for e in c) != 1 or sum(c) == len(s):
        print "No"
        exit(0)
    s = s.replace("ABC", p[[i for i in [0,1,2] if c[i]==0][0]])
print "Yes"

Buggy python submission (verdict: Time Limit Exceeded):
s = raw_input()
p = "ABC"
used = set()

while s != p:
    idx = 0
    c = [0]*3
    while idx < len(s):
        if idx+3 <= len(s) and s[idx:idx+3] == p:
            idx += 3
        else:
            c[p.index(s[idx])] += 1
            idx += 1
    if sum(e == 0 for e in c) != 1 or sum(c) == s:
        print "No"
        exit(0)
    s = s.replace("ABC", p[[i for i in [0,1,2] if c[i]==0][0]])
print "Yes"

Fixed submission:
s = raw_input()
p = "ABC"
used = set()

while s != p:
    idx = 0
    c = [0]*3
    while idx < len(s):
        if idx+3 <= len(s) and s[idx:idx+3] == p:
            idx += 3
        else:
            c[p.index(s[idx])] += 1
            idx += 1
    if sum(e == 0 for e in c) != 1 or sum(c) == len(s):
        print "No"
        exit(0)
    s = s.replace("ABC", p[[i for i in [0,1,2] if c[i]==0][0]])
print "Yes"
Buggy python submission (verdict: Runtime Error):
def convert(S):
    return eval(S.replace(")[","),[").replace("](","],("))
def dfs(A, B):
    if not A or not B:
        return ()
    return (dfs(A[0], B[0]), [A[1][0] + B[1][0]], dfs(A[2], B[2]))
print(str(dfs(convert(input()), convert(input()))).replace(", ","")[1:-1])

Fixed submission:
def convert(S):
    cur = 0
    def parse():
        nonlocal cur
        if S[cur] == ')':
            return ()
        cur += 1 # '('
        left = parse()
        cur += 2 # ')['
        num = 0
        while S[cur] != ']':
            num = 10*num + int(S[cur])
            cur += 1
        cur += 2 # ']('
        right = parse()
        cur += 1 # ')'
        return left, num, right
    return parse()
def dfs(A, B):
    if not A or not B:
        return ()
    return (dfs(A[0], B[0]), [A[1] + B[1]], dfs(A[2], B[2]))
print(str(dfs(convert(input()), convert(input()))).replace(", ","")[1:-1])