Problem p03488 (python)

Fixed submission:
#!/usr/bin/env python
# coding: utf-8

def no():
    print("No")
    return

def main():
    s = input()
    x, y = list(map(int, input().split()))
    x_start = 0
    f_count = 0
    x_dirs = []
    y_dirs = []
    is_x = True # xを向いてるか
    t_exist = False
    for c in s:
        if c == "F":
            f_count += 1
            continue
        # T
        if f_count > 0:
            if is_x:
                if t_exist:
                    x_dirs.append(f_count)
                else:
                    x_start = f_count
            else:
                y_dirs.append(f_count)
        t_exist = True
        is_x = not is_x
        f_count = 0
        continue
    if f_count > 0:
        if is_x:
            if t_exist:
                x_dirs.append(f_count)
            else:
                x_start = f_count
        else:
            y_dirs.append(f_count)

    memo_x = [{} for _ in range(len(x_dirs)+1)]
    memo_y = [{} for _ in range(len(y_dirs)+1)]
    memo_x[0][x_start] = True
    memo_y[0][0] = True

    for i, dx in enumerate(x_dirs):
        for j in memo_x[i]:
            memo_x[i+1][j-dx] = True
            memo_x[i+1][j+dx] = True
    for i, dy in enumerate(y_dirs):
        for j in memo_y[i]:
            memo_y[i+1][j-dy] = True
            memo_y[i+1][j+dy] = True

    # print(x_dirs, y_dirs)
    # print(memo_x)
    # print(memo_y)

    if x not in memo_x[len(x_dirs)]:
        return no()
    if y not in memo_y[len(y_dirs)]:
        return no()
    print("Yes")


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python
# coding: utf-8

def no():
    print("No")
    return

def shift(i):
    return i+8001

def main():
    s = input()
    x, y = list(map(int, input().split()))
    x_start = 0
    x_count = 0
    y_count = 0
    t_count = 0
    f_count = 0
    x_dirs = []
    y_dirs = []
    is_x = True # xを向いてるか
    f_before_t = 0
    t_exist = False
    for c in s:
        if c == "T":
            t_count += 1
            if f_count > 0:
                if is_x and not t_exist:
                    x_dirs.append(f_count)
                else:
                    y_dirs.append(f_count)
                f_count = 0
            t_exist = True
            continue
        f_count += 1
        if not t_exist:
            x_start += 1
            f_before_t += 1
        if t_count % 2 == 0:
            # 向いている方向に進む
            if is_x:
                x_count += 1
            else:
                y_count += 1
        else:
            if is_x:
                y_count += 1
            else:
                x_count += 1
            is_x = not is_x
        t_count = 0
    if f_count > 0:
        if is_x:
            x_dirs.append(f_count)
        else:
            y_dirs.append(f_count)
    if (x % 2) != (x_count % 2):
        return no()
    if abs(x) > x_count:
        return no()
    if (y % 2) != (y_count % 2):
        return no()
    if abs(y) > y_count:
        return no()
    if (x_count - f_before_t*2) < x*-1:
        return no()


    memo_x = [[False for _ in range(16001)] for _ in range(len(x_dirs)+1)]
    memo_y = [[False for _ in range(16001)] for _ in range(len(y_dirs)+1)]
    memo_x[0][shift(x_start)] = True
    memo_y[0][0] = True

    for i, dx in enumerate(x_dirs):
        for j in range(16001):
            if memo_x[i][j]:
                memo_x[i+1][j-dx] = True
                memo_x[i+1][j+dx] = True
    for i, dy in enumerate(y_dirs):
        for j in range(16001):
            if memo_y[i][j]:
                memo_y[i+1][j-dy] = True
                memo_y[i+1][j+dy] = True
    if not memo_x[len(x_dirs)][shift(x)]:
        return no()
    if not memo_y[len(y_dirs)][shift(y)]:
        return no()
    print("Yes")


if __name__ == '__main__':
    main()