Problem p03488 (python)

Fixed submission:
S = input()
X, Y = map(int, input().split())

if S.find("F") < 0:
    if X != 0 or Y != 0:
        print("No")
    else:
        print("Yes")
    exit()

n = len(S)
d = list(map(len, S.split("T")))
xs = d[::2]
ys = d[1::2]

dpx = [set() for _ in range(len(xs))]
for i in range(len(xs)):
    if i == 0:
        d = (-1) ** S.find("F")
        dpx[i].add(d * xs[i] + n)
        continue
    
    for j in dpx[i - 1]:
        dpx[i].add(j + xs[i])
        dpx[i].add(j - xs[i])

dpy = [set() for _ in range(len(ys) + 1)]
dpy[0].add(n)
for i in range(len(ys)):
    for j in dpy[i]:
        dpy[i + 1].add(j + ys[i])
        dpy[i + 1].add(j - ys[i])

ans = (X + n in dpx[-1]) & (Y + n in dpy[-1])
print("Yes" if ans else "No")

Buggy submission (verdict: Runtime Error):
S = input()
X, Y = map(int, input().split())

if S.find("F") < 0:
    if X != 0 or Y != 0:
        print("No")
    else:
        print("Yes")
    exit()

n = len(S)
d = list(map(len, S.split("T")))
xs = d[::2]
ys = d[1::2]

dpx = [set() for _ in range(len(xs))]
dpx[0].add(0)
for i in range(len(xs)):
    for j in dpx[i]:
        dpx[i + 1].add(j + xs[i])
        dpx[i + 1].add(j - xs[i])

dpy = [set() for _ in range(len(ys) + 1)]
dpy[0].add(0)
for i in range(len(ys)):
    for j in dpy[i]:
        dpy[i + 1].add(j + ys[i])
        dpy[i + 1].add(j - ys[i])

ans = (X in dpx[-1]) & (Y in dpy[-1])
print("Yes" if ans else "No")

Buggy submission (verdict: Runtime Error):
S = input()
X, Y = map(int, input().split())

if S.find("F") < 0:
    if X != 0 or Y != 0:
        print("No")
        exit()

n = len(S)
d = list(map(len, S.split("T")))
xs = d[::2]
ys = d[1::2]

dpx = [[False] * (2 * n + 1) for _ in range(len(xs))]
for i in range(len(xs)):
    if i == 0:
        d = (-1) ** S.find("F")
        dpx[i][d * xs[i] + n] = True
        continue

    for j in range(2 * n + 1):
        if j + xs[i] <= 2 * n:
            dpx[i][j] |= dpx[i - 1][j + xs[i]]
        if 0 <= j - xs[i] <= 2 * n:
            dpx[i][j] |= dpx[i - 1][j - xs[i]]

dpy = [[False] * (2 * n + 1) for _ in range(len(ys) + 1)]
dpy[0][n] = True
for i in range(len(ys)):
    for j in range(2 * n + 1):
        if j + ys[i] <= 2 * n:
            dpy[i + 1][j] |= dpy[i][j + ys[i]]
        if 0 <= j - ys[i] <= 2 * n:
            dpy[i + 1][j] |= dpy[i][j - ys[i]]

ans = dpx[-1][X + n] & dpy[-1][Y + n]
print("Yes" if ans else "No")

Buggy submission (verdict: Runtime Error):
S = input()
X, Y = map(int, input().split())

n = len(S)
xs = list(map(len, S.split("T")[::2]))
ys = list(map(len, S.split("T")[1::2]))

dpx = [[False] * (2 * n + 1) for _ in range(len(xs))]
for i in range(len(xs)):
    if i == 0:
        d = (-1) ** S.find("F")
        dpx[i][d * xs[i] + n] = True
        continue

    for j in range(2 * n + 1):
        if j + xs[i] <= 2 * n:
            dpx[i][j] |= dpx[i - 1][j + xs[i]]
        if 0 <= j - xs[i] <= 2 * n:
            dpx[i][j] |= dpx[i - 1][j - xs[i]]

ys = [0] + ys
dpy = [[False] * (2 * n + 1) for _ in range(len(ys))]
for i in range(len(ys)):
    if i == 0:
        dpy[i][n] = True
        continue

    for j in range(2 * n + 1):
        if j + ys[i] <= 2 * n:
            dpy[i][j] |= dpy[i - 1][j + ys[i]]
        if 0 <= j - ys[i] <= 2 * n:
            dpy[i][j] |= dpy[i - 1][j - ys[i]]

ans = dpx[-1][X + n] & dpy[-1][Y + n]
print("Yes" if ans else "No")