Problem p03488 (python)

Fixed submission:
S = input() + 'E'
gx, gy = map(int, input().split())
X, Y = [], []
x, y, i, flag = 0, 0, 0, 1
while i <= len(S):
    f = 0
    while S[i] == 'F':
        f += 1
        i += 1
    if flag:
        X.append(f)
    else:
        Y.append(f)
    flag = (flag + 1) % 2
    if S[i] == 'E':
        break
    i += 1
gx -= X[0]
for x in sorted(X[1:], reverse = True):
    if gx > 0:
        gx -= x
    else:
        gx += x
for y in sorted(Y, reverse = True):
    if gy > 0:
        gy -= y
    else:
        gy += y
if gx == gy == 0:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
s = input()
gx, gy = map(int, input().split())
n = len(s)
X, Y = [], []
x, y, flag = 0, 0, 0
for i in range(n):
    if s[i] == 'F' and flag == 0:
        x += 1
    elif s[i] == 'F' and flag == 1:
        y += 1
    else:
        if flag and y > 0:
            Y.append(y)
            y = 0
        elif not flag and x > 0:
            X.append(x)
            x = 0
        flag = (flag + 1) % 2
if flag and y > 0:
    Y.append(y)
elif not flag and x > 0:
    X.append(x)
gx -= X[0]
if len(X):
    X.pop(0)
for x in sorted(X, reverse = True):
    if gx > 0:
        gx -= x
    else:
        gx += x
for y in sorted(Y, reverse = True):
    if gy > 0:
        gy -= y
    else:
        gy += y
if gx == gy == 0:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
s = input()
gx, gy = map(int, input().split())
n = len(s)
X, Y = [], []
x, y, flag = 0, 0, 0
for i in range(n):
    if s[i] == 'F' and flag == 0:
        x += 1
    elif s[i] == 'F' and flag == 1:
        y += 1
    else:
        if flag and y > 0:
            Y.append(y)
            y = 0
        elif not flag and x > 0:
            X.append(x)
            x = 0
        flag = (flag + 1) % 2
if flag and y > 0:
    Y.append(y)
elif not flag and x > 0:
    X.append(x)
gx -= X[0]
X.pop(0)
for x in sorted(X, reverse = True):
    if gx > 0:
        gx -= x
    else:
        gx += x
for y in sorted(Y, reverse = True):
    if gy > 0:
        gy -= y
    else:
        gy += y
if gx == gy == 0:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
s = input()
gx, gy = map(int, input().split())
n = len(s)
X, Y = [], []
x, y, flag = 0, 0, 0
for i in range(n):
    if s[i] == 'F' and flag == 0:
        x += 1
    elif s[i] == 'F' and flag == 1:
        y += 1
    else:
        if flag and y > 0:
            Y.append(y)
            y = 0
        elif not flag and x > 0:
            X.append(x)
            x = 0
        flag = (flag + 1) % 2
if flag and y > 0:
    Y.append(y)
elif not flag and x > 0:
    X.append(x)
MAX_X, MAX_Y, l_X, l_Y = sum(X), sum(Y), len(X), len(Y)
if MAX_X < abs(gx) or MAX_Y < abs(gy):
    print("No")
else:
    dp_x = [[False for _ in range(MAX_X * 2 + 1)] for _ in range(l_X + 1)]
    dp_y = [[False for _ in range(MAX_Y * 2 + 1)] for _ in range(l_Y + 1)]
    dp_x[0][MAX_X], dp_y[0][MAX_Y] = True, True
    for i in range(l_X):
        for x in range(MAX_X * 2 + 1):
            if x + X[i] < MAX_X * 2 + 1:
                dp_x[i + 1][x] |= dp_x[i][x + X[i]]
            if x - X[i] >= 0:
                dp_x[i + 1][x] |= dp_x[i][x - X[i]]
    for i in range(l_Y):
        for y in range(MAX_Y * 2 + 1):
            if y + Y[i] < MAX_Y * 2 + 1:
                dp_y[i + 1][y] |= dp_y[i][y + Y[i]]
            if x - X[i] >= 0:
                dp_y[i + 1][y] |= dp_y[i][y - Y[i]]
    if dp_x[l_X][gx + MAX_X] and dp_y[l_Y][gy + MAX_Y]:
        print("Yes")
    else:
        print("No")

Buggy submission (verdict: Runtime Error):
s = input()
gx, gy = map(int, input().split())
n = len(s)
X, Y = [], []
x, y, flag = 0, 0, 0
for i in range(n):
    if s[i] == 'F' and flag == 0:
        x += 1
    elif s[i] == 'F' and flag == 1:
        y += 1
    else:
        if flag and y > 0:
            Y.append(y)
            y = 0
        elif not flag and x > 0:
            X.append(x)
            x = 0
        flag = (flag + 1) % 2
if flag and y > 0:
    Y.append(y)
elif not flag and x > 0:
    X.append(x)
MAX_X, MAX_Y, l_X, l_Y = sum(X), sum(Y), len(X), len(Y)
if MAX_X < gx or MAX_Y < gy:
    print("No")
else:
    dp_x = [[False for _ in range(MAX_X * 2 + 1)] for _ in range(l_X + 1)]
    dp_y = [[False for _ in range(MAX_Y * 2 + 1)] for _ in range(l_Y + 1)]
    dp_x[0][MAX_X], dp_y[0][MAX_Y] = True, True
    for i in range(l_X):
        for x in range(MAX_X * 2 + 1):
            if x + X[i] < MAX_X * 2 + 1:
                dp_x[i + 1][x] |= dp_x[i][x + X[i]]
            if x - X[i] >= 0:
                dp_x[i + 1][x] |= dp_x[i][x - X[i]]
    for i in range(l_Y):
        for y in range(MAX_Y * 2 + 1):
            if y + Y[i] < MAX_Y * 2 + 1:
                dp_y[i + 1][y] |= dp_y[i][y + Y[i]]
            if x - X[i] >= 0:
                dp_y[i + 1][y] |= dp_y[i][y - Y[i]]
    if dp_x[l_X][gx + MAX_X] and dp_y[l_Y][gy + MAX_Y]:
        print("Yes")
    else:
        print("No")