Problem: A robot is put at the origin in a two-dimensional plane. Initially, the robot is facing in the positive x-axis direction.  This robot will be given an instruction sequence s. s consists of the following two kinds of letters, and will be executed in order from front to back.    * `F` : Move in the current direction by distance 1.   * `T` : Turn 90 degrees, either clockwise or counterclockwise.  The objective of the robot is to be at coordinates (x, y) after all the instructions are executed. Determine whether this objective is achievable.

Submission attempts (in order):
from collections import defaultdict
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve():
    S = input()[:-1]
    GX, GY = map(int, input().split())

    
    
    S_parsed = []
    prev = S[0]
    seq = 1
    for c in S[1:]:
        if c == "F":
            if c == prev:
                seq += 1
            else:
                prev = c
                if seq % 2 == 0:
                    pass
                else:
                    S_parsed.append("T")
                seq = 1
        elif c == "T":
            if c == prev:
                seq += 1
            else:
                S_parsed.append(seq)
                seq = 1
            prev = c
    else:
        if S[-1] == "F":
            S_parsed.append(seq)

    
    x_move = []
    y_move = []

    dir_x = True
    for c in S_parsed:
        if c == "T":
            dir_x = not dir_x
        else:
            if dir_x:
                x_move.append(c)
            else:
                y_move.append(c)

    
    dp_x = {}
    if len(x_move) > 0:
        if S[0] != "T":
            
            dp_x.add(x_move[0])
        else:
            
            dp_x.add(x_move[0])
            dp_x.add(-x_move[0])
    else:
        dp_x.add(0)

    for x in x_move[1:]:
        dp_x = {key+x for key in dp_x} | {key-y for key in dp_x}

    dp_y = {}
    if len(y_move) == 0:
        dp_y.add(0)
    else:
        dp_y.add(y_move[0])
        dp_y.add(-y_move[0])
    for y in y_move[1:]:
        dp_y = {key+y for key in dp_y} | {key-y for key in dp_y}

    if GX in dp_x and GY in dp_y:
        print("Yes")
    else:
        print("No")


solve()


from collections import defaultdict
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve():
    S = input()[:-1]
    GX, GY = map(int, input().split())

    
    
    S_parsed = []
    prev = S[0]
    seq = 1
    for c in S[1:]:
        if c == "F":
            if c == prev:
                seq += 1
            else:
                prev = c
                if seq % 2 == 0:
                    pass
                else:
                    S_parsed.append("T")
                seq = 1
        elif c == "T":
            if c == prev:
                seq += 1
            else:
                S_parsed.append(seq)
                seq = 1
            prev = c
    else:
        if S[-1] == "F":
            S_parsed.append(seq)

    
    x_move = []
    y_move = []

    dir_x = True
    for c in S_parsed:
        if c == "T":
            dir_x = not dir_x
        else:
            if dir_x:
                x_move.append(c)
            else:
                y_move.append(c)

    
    dp_x = set()
    if len(x_move) > 0:
        if S[0] != "T":
            
            dp_x.add(x_move[0])
        else:
            
            dp_x.add(x_move[0])
            dp_x.add(-x_move[0])
    else:
        dp_x.add(0)

    for x in x_move[1:]:
        dp_x = {key+x for key in dp_x} | {key-y for key in dp_x}

    dp_y = set()
    if len(y_move) == 0:
        dp_y.add(0)
    else:
        dp_y.add(y_move[0])
        dp_y.add(-y_move[0])
    for y in y_move[1:]:
        dp_y = {key+y for key in dp_y} | {key-y for key in dp_y}

    if GX in dp_x and GY in dp_y:
        print("Yes")
    else:
        print("No")


solve()


from collections import defaultdict
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve():
    S = input()[:-1]
    GX, GY = map(int, input().split())

    
    
    S_parsed = []
    prev = S[0]
    seq = 1
    for c in S[1:]:
        if c == "F":
            if c == prev:
                seq += 1
            else:
                prev = c
                if seq % 2 == 0:
                    pass
                else:
                    S_parsed.append("T")
                seq = 1
        elif c == "T":
            if c == prev:
                seq += 1
            else:
                S_parsed.append(seq)
                seq = 1
            prev = c
    else:
        if S[-1] == "F":
            S_parsed.append(seq)

    
    x_move = []
    y_move = []

    dir_x = True
    for c in S_parsed:
        if c == "T":
            dir_x = not dir_x
        else:
            if dir_x:
                x_move.append(c)
            else:
                y_move.append(c)

    
    dp_x = set()
    if len(x_move) > 0:
        if S[0] != "T":
            
            dp_x.add(x_move[0])
        else:
            
            dp_x.add(x_move[0])
            dp_x.add(-x_move[0])
    else:
        dp_x.add(0)

    for x in x_move[1:]:
        dp_x = {key+x for key in dp_x} | {key-x for key in dp_x}

    dp_y = set()
    if len(y_move) == 0:
        dp_y.add(0)
    else:
        dp_y.add(y_move[0])
        dp_y.add(-y_move[0])
    for y in y_move[1:]:
        dp_y = {key+y for key in dp_y} | {key-y for key in dp_y}

    if GX in dp_x and GY in dp_y:
        print("Yes")
    else:
        print("No")


solve()


Verdicts: Runtime Error

Runtime Error

Accepted