Problem p03488 (python)

Fixed submission:
from itertools import product
s = input()
x, y = map(int,input().split())
L = [len(i) for i in s.split('T')]
X = {L[0]}
Y = {0}
for i in L[2::2]:
    X = {j + k for j, k in product(X, [-i, i])}
for i in L[1::2]:
    Y = {j + k for j, k in product(Y, [-i, i])}
if x in X and y in Y:
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
s = input()
x, y = map(int,input().split())
flag = 1
X = []
Y = []
cnt = 0
for i in s:
    if i == 'T':
        if flag == 1 and cnt:
            X.append(cnt)
            cnt = 0
        elif flag == -1 and cnt:
            Y.append(cnt)
            cnt = 0
        flag *= -1
    else:
        cnt += 1
if flag == 1 and cnt:
    X.append(cnt)
elif flag == -1 and cnt:
    Y.append(cnt)

dp_x = [[0 for i in range(160001)] for j in range(len(X))]
dp_y = [[0 for i in range(16000)] for j in range(len(Y))]

for i in range(len(X)):
    if i == 0:
        if s[0] == 'T':
            dp_x[0][X[0] + 8000] = 1
            dp_x[0][- X[0] + 8000] = 1
        else:
            dp_x[0][X[0] + 8000] = 1
    else:
        for j in range(16001):
            if dp_x[i-1][j] and 0 <= j + X[i] <= 16000:
                dp_x[i][j + X[i]] = 1
            if dp_x[i-1][j] and 0 <= j - X[i] <= 16000:
                dp_x[i][j - X[i]] = 1

for i in range(len(Y)):
    if i == 0:
        dp_y[0][Y[0] + 8000] = 1
        dp_y[0][- Y[0] + 8000] = 1
    else:
        for j in range(16001):
            if dp_y[i-1][j] and 0 <= j + Y[i] <= 16000:
                dp_y[i][j + Y[i]] = 1
            if dp_y[i-1][j] and 0 <= j - Y[i] <= 16000:
                dp_y[i][j - Y[i]] = 1

if len(X) == 0 and len(Y) == 0:
    if x == 0 and y == 0:
        print('Yes')
    else:
        print('No')
elif len(X) == 0:
    if x == 0 and dp_y[-1][y+8000]:
        print('Yes')
    else:
        print('No')
elif len(Y) == 0:
    if y == 0 and dp_x[-1][x+8000]:
        print('Yes')
    else:
        print('No')
else:
    if dp_y[-1][y+8000] and dp_x[-1][x+8000]:
        print('Yes')
    else:
        print(dp_y[-1][y+8000],dp_x[-1][x+8000])
        print('No')

Buggy submission (verdict: Runtime Error):
s = input()
x, y = map(int,input().split())
flag = 1
X = []
Y = []
cnt = 0
for i in s:
    if i == 'T':
        if flag == 1 and cnt:
            X.append(cnt)
            cnt = 0
        elif flag == -1 and cnt:
            Y.append(cnt)
            cnt = 0
        flag *= -1
    else:
        cnt += 1
if flag == 1 and cnt:
    X.append(cnt)
elif flag == -1 and cnt:
    Y.append(cnt)

dp_x = [[0 for i in range(160001)] for j in range(len(X))]
dp_y = [[0 for i in range(16000)] for j in range(len(Y))]

for i in range(len(X)):
    if i == 0:
        if s[0] == 'T':
            dp_x[0][X[0] + 8000] = 1
            dp_x[0][- X[0] + 8000] = 1
        else:
            dp_x[0][X[0] + 8000] = 1
    else:
        for j in range(16001):
            if dp_x[i-1][j] and 0 <= j + X[i] <= 16000:
                dp_x[i][j + X[i]] = 1
            if dp_x[i-1][j] and 0 <= j - X[i] <= 16000:
                dp_x[i][j - X[i]] = 1

for i in range(len(Y)):
    if i == 0:
        dp_y[0][Y[0] + 8000] = 1
        dp_y[0][- Y[0] + 8000] = 1
    else:
        for j in range(16001):
            if dp_y[i-1][j] and 0 <= j + Y[i] <= 16000:
                dp_y[i][j + Y[i]] = 1
            if dp_y[i-1][j] and 0 <= j - Y[i] <= 16000:
                dp_y[i][j - Y[i]] = 1

if len(X) == 0 and len(Y) == 0:
    if x == 0 and y == 0:
        print('Yes')
    else:
        print('No')
elif len(X) == 0:
    if x == 0 and dp_y[-1][y+8000]:
        print('Yes')
    else:
        print('No')
elif len(Y) == 0:
    if y == 0 and dp_x[-1][x+8000]:
        print('Yes')
    else:
        print('No')
else:
    if dp_y[-1][y+8000] and dp_x[-1][x+8000]:
        print('Yes')
    else:
        print(dp_y[-1][y+8000],dp_x[-1][x+8000])
        print('No')