Problem:


Score : 500 points

Problem Statement

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.

Constraints

s consists of F and T.

1 \leq |s| \leq 8 000

x and y are integers.

|x|, |y| \leq |s|

Input

Input is given from Standard Input in the following format:

s
x y

Output

If the objective is achievable, print Yes; if it is not, print No.

Sample Input 1

FTFFTFFF
4 2

Sample Output 1

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning clockwise in the second T.

Sample Input 2

FTFFTFFF
-2 -2

Sample Output 2

Yes

The objective can be achieved by, for example, turning clockwise in the first T and turning clockwise in the second T.

Sample Input 3

FF
1 0

Sample Output 3

No

Sample Input 4

TF
1 0

Sample Output 4

No

Sample Input 5

FFTTFF
0 0

Sample Output 5

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning counterclockwise in the second T.

Sample Input 6

TTTT
1 0

Sample Output 6

No



Sample input:
FTFFTFFF
4 2


Sample output:
Yes

Submission status: Wrong Answer

Solution code:
s = list(input())
s.append('T')
X,Y =map(int, input().split())
curX = True
cnt = 0
X_s = []
Y_s = []
for i,c in enumerate(s):
    if c == 'T':
        if curX:
            X_s.append(cnt)
        else:
            Y_s.append(cnt)
        curX = not curX
        cnt=0
    else:
        cnt+=1
init = 0
if s[0] == 'F':
    init= X_s.pop(0)
X -= init

SETX = set([0])
SETY = set([0])
for X in X_s:
    SETX.add(X)
    ADD = set()
    for SX in SETX:
        ADD.add(SX+X)
    SETX = ADD | SETX
for Y in Y_s:
    SETY.add(Y)
    ADD = set()
    for SY in SETY:
        ADD.add(SY+Y)
    SETY = ADD | SETY
MODX = sum(X_s)-X
MODY = sum(Y_s)-Y

if MODX % 2 == 0 and MODX//2 in SETX and MODY % 2 == 0 and MODY//2 in SETY:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
from collections import deque
s=list(input())
x,y=map(int,input().split())
#print(s,x,y)
i=0
xflag=1
flag=0
xax=deque()
yax=deque()
cnt=0
while i<len(s):
    if s[i]=="F":
        cnt=cnt+1
    else:
        if cnt!=0:
            if xflag==1:
                xax.append(cnt)
            else:
                yax.append(cnt)
        xflag=(xflag+1)%2
        cnt=0
    i=i+1
if cnt!=0:
    if xflag == 1:
        xax.append(cnt)
    else:
        yax.append(cnt)
#print(xax,yax)
xdeq=deque()
ydeq=deque()
if len(xax)>0:
    x0=xax.popleft()
    xdeq.append(x0)
else:
    xdeq.append(0)
ydeq.append(0)
#print(xdeq,xax)
while len(xax)>0:
    subadd=xax.popleft()
    xdeq2=[]
    while len(xdeq)>0:
        xd=xdeq.popleft()
        xdeq2.append(xd-subadd)
        xdeq2.append(xd+subadd)
    xdeq2=list(set(xdeq2))
    for i in xdeq2:
        xdeq.append(i)
    #print(xdeq)
#print(xdeq)
while len(yax)>0:
    subadd=yax.popleft()
    ydeq2=[]
    while len(ydeq)>0:
        yd=ydeq.popleft()
        ydeq2.append(yd-subadd)
        ydeq2.append(yd+subadd)
    ydeq2=list(set(ydeq2))
    for i in ydeq2:
        ydeq.append(i)
    #print(ydeq)
#print(ydeq)
xlist=list(xdeq)
ylist=list(ydeq)
for i in xlist:
    for j in ylist:
        if (i==x)and(j==y):
            flag=1
            break
    if flag==1:
        break
if flag==1:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
s=input()
x,y=map(int,input().split())

rx=0
ry=0

judge=True
for i in s:
  if i=='F':
    if judge:
      rx+=1
    else:
      ry+=1
      
  else:
    if judge:
      judge=False
      
    else:
      judge=True


if rx%2==x%2 and ry%2==y%2 and rx>=abs(x) and ry>=abs(y):
    print('Yes')

else:
    print('No')

Submission status: Wrong Answer

Solution code:
def isok(f,p):
  s=0
  if not f and p==0:return 1
  for i,j in enumerate(f):
    s+=j
    if s>=p:break
  else:return 0
  for j in f[i+1:]:
    if s>=p:s-=j
    else:s+=j
  return s==p
s=input()
x,y=map(int,input().split())
f=list(map(len,s.split('T')))
x-=f[0]
x,y=abs(x),abs(y)
xf=sorted(f[2::2])[::-1]
yf=sorted(f[1::2])[::-1]
print(['No','Yes'][isok(xf,x) and isok(yf,y)])
print(f)

Submission status: Wrong Answer

Solution code:
def isok(f,p):
  s=0
  for i,j in enumerate(f):
    s+=j
    if s>=p:break
  else:return 0
  for j in f[i+1:]:
    if s>=p:s+=j
    else:s-=j
  return s==p
s=input()
x,y=map(int,input().split())
f=list(map(len,s.split('T')))
x-=f[0]
x,y=abs(x),abs(y)
xf=sorted(f[2::2])[::-1]
yf=sorted(f[1::2])[::-1]
print(['No','Yes'][isok(xf,x) and isok(yf,y)])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

S = readline().decode().rstrip()
N = len(S)
x,y = map(int,readline().split())
t_cnt = 0
cnt_x1 = 0
cnt_x2 = 0
cnt_y = 0

for i in range(N):
    if S[i] == 'F':
        if t_cnt == 0:
            cnt_x1 += 1
        else:
            if t_cnt % 2 == 0:
                cnt_x2 += 1
            else:
                cnt_y += 1
    else:
        t_cnt += 1

 
X = list(range(-cnt_x2,cnt_x2+1,2))
Y = list(range(-cnt_y,cnt_y+1,2))
x -= cnt_x1

if x in X and y in Y:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    S = list(input().split("T"))
    gx, gy = map(int, input().split())

    to_X = 1
    X, Y = [], []
    for s in S:
        X.append(len(s)) if to_X else Y.append(len(s))
        to_X ^= 1

    now_x = {0}
    for x in X:
        tmp = set()
        for prev_x in now_x:
            tmp.add(prev_x + x)
            tmp.add(prev_x - x)
        now_x = tmp

    now_y = {0}
    for y in Y:
        tmp = set()
        for prev_y in now_y:
            tmp.add(prev_y + y)
            tmp.add(prev_y - y)
        now_y = tmp

    print("Yes" if gx in now_x and gy in now_y else "No")


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
"""
ベクトルが(a,b)離れた地点が目的地の状況で命令Sが与えられたとき、目的地に到達可能か？
というDPをすればよい。
"""
s = input()
x,y = map(int,input().split())

splitS = list(s.split("T"))
default = (splitS[0].count("F"),0)

yoko = []
for i in range(2,len(splitS),2):
    yoko.append(splitS[i].count("F"))

tate = []
for i in range(1,len(splitS),2):
    tate.append(splitS[i].count("F"))

def f(i,n,data,dp,distination):
    if (i,n) not in dp:
        if i == len(data) and n == distination:
            dp[(i,n)] = True
        elif i == len(data):
            dp[(i,n)] = False
        else:
            dp[(i,n)] = False or (i+1,n+data[i],data,dp,distination)
            dp[(i,n)] = dp[(i,n)] or(i+1,n-data[i],data,dp,distination)
    return dp[(i,n)]
dp1 = {}
dp2 = {}
f(0,default[0],yoko,dp1,x)
f(0,default[1],tate,dp2,y)
if dp1[(0,default[0])] and dp2[(0,default[1])]:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    S = in_s() + "T"
    N = len(S)

    X, Y = in_nn()

    x_num = []
    y_num = []

    f = False
    first = True

    cnt = 0
    for i in range(N):
        if S[i] == "T":
            if cnt > 0:
                if f:
                    if first:
                        Y -= cnt
                    else:
                        y_num.append(cnt)
                else:
                    if first:
                        X -= cnt
                    else:
                        x_num.append(cnt)
                first = False
            cnt = 0
            f = not f
        else:
            cnt += 1

    x_ok = False
    y_ok = False

    if len(x_num) == 0:
        if X == 0:
            x_ok = True
    else:
        dp_x = [False] * N
        dp_x[0] = True
        for tx in x_num:
            for i in range(N - 1, -1, -1):
                if i + tx < N and dp_x[i]:
                    dp_x[i + tx] = True

        xsum = sum(x_num)
        for i in range(N):
            if dp_x[i]:
                if abs(i - abs(xsum - i)) == abs(X):
                    x_ok = True

    if len(y_num) == 0:
        if Y == 0:
            y_ok = True
    else:
        dp_y = [False] * N
        dp_y[0] = True
        for ty in y_num:
            for i in range(N - 1, -1, -1):
                if i + ty < N and dp_y[i]:
                    dp_y[i + ty] = True

        ysum = sum(y_num)
        for i in range(N):
            if dp_y[i]:
                if abs(i - abs(ysum - i)) == abs(Y):
                    y_ok = True

    # print(X, Y)
    # print(x_num)
    # print(y_num)

    if x_ok and y_ok:
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    S = in_s() + "T"
    N = len(S)

    X, Y = in_nn()

    x_num = []
    y_num = []

    f = False

    cnt = 0
    for i in range(N):
        if S[i] == "T":
            if cnt > 0:
                if f:
                    y_num.append(cnt)
                else:
                    x_num.append(cnt)
            cnt = 0
            f = not f
        else:
            cnt += 1

    x_ok = False
    y_ok = False

    if len(x_num) == 0:
        if X == 0:
            x_ok = True
    else:
        dp_x = [False] * N
        dp_x[0] = True
        for tx in x_num:
            for i in range(N - 1, -1, -1):
                if i + tx < N and dp_x[i]:
                    dp_x[i + tx] = True

        xsum = sum(x_num)
        for i in range(N):
            if dp_x[i]:
                if abs(i - abs(xsum - i)) == abs(X):
                    x_ok = True

    if len(y_num) == 0:
        if Y == 0:
            y_ok = True
    else:
        dp_y = [False] * N
        dp_y[0] = True
        for ty in y_num:
            for i in range(N - 1, -1, -1):
                if i + ty < N and dp_y[i]:
                    dp_y[i + ty] = True

        ysum = sum(y_num)
        for i in range(N):
            if dp_y[i]:
                if abs(i - abs(ysum - i)) == abs(Y):
                    y_ok = True

    # print(x_num)
    # print(y_num)
    # print(dp_x)
    # print(dp_y)

    if x_ok and y_ok:
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
s=input().split("T")
x,y=map(int,input().split())
a,b=deque([len(i)for i in s[0::2]]),deque([len(i)for i in s[1::2]])
x-=a.popleft()
a,b=deque(sorted(a)),deque(sorted(b))
while a:
 if x<0:x+=a.popleft()
 else:x-=a.popleft()
while b:
 if y<0:y+=b.popleft()
 else:y-=b.popleft()
print("YNeos"[x!=0 or y!=0::2])

Submission status: Wrong Answer

Solution code:
import sys
import math
import copy
import random
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  # inv[j] = j^{-1} mod MOD
    fac = [0] * SIZE  # fac[j] = j! mod MOD
    finv = [0] * SIZE  # finv[j] = (j!)^{-1} mod MOD
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def renritsu(A, Y):
    # example 2x + y = 3, x + 3y = 4
    # A = [[2,1], [1,3]])
    # Y = [[3],[4]] または [3,4]
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    # [1.0, 1.0]
    return X.flatten().tolist()[0]

class TwoDimGrid:
    # 2次元座標 -> 1次元
    def __init__(self, h, w, wall="#"):
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        # self.init_cost()

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        # 壁も含めて0-indexed 元々の座標だけ考えると1-indexed
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return
    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        # for i in range(1, self.h+1):
        #     for j in range(1, self.w+1):
        #         cx, cy = j, i
        #         for dx, dy in move_eight:
        #             nx, ny = dx + cx, dy + cy

def solve():
    s = getS()
    x, y = getList()
    ls = len(s)
    hori = True
    h,v = [],[]
    cnt = 0
    for c in s:
        if c == "F":
            cnt += 1
        else:
            if hori:
                h.append(cnt)
            else:
                v.append(cnt)
            cnt = 0
            hori = not hori

    if cnt != 0:
        if hori:
            h.append(cnt)
        else:
            v.append(cnt)

    print(h, v)
    x -= h[0]
    h[0] = 0
    dp = [0] * (ls * 2 + 100)
    dp[ls + 50] = 1
    for num in h:
        tmp = [0] * (len(dp))
        for i, d in enumerate(dp):
            if d == 1:
                tmp[i - num] = 1
                tmp[i + num] = 1
        dp = tmp
    # print(dp)
    if dp[x + ls + 50] == 0:
        print("No")
        return

    dp = [0] * (ls * 2 + 100)
    dp[ls + 50] = 1
    for num in v:
        tmp = [0] * (len(dp))
        for i, d in enumerate(dp):
            if d == 1:
                tmp[i - num] = 1
                tmp[i + num] = 1
        dp = tmp
    # print(dp)
    if dp[y + ls + 50] == 0:
        print("No")
        return

    print("Yes")
    return


def main():
    n = getN()
    for _ in range(n):
        s = "".join([random.choice(["F", "T"]) for i in range(20)])
        print(s)
        solve(s, 1, 0)

    return
if __name__ == "__main__":
    # main()
    solve()

Submission status: Wrong Answer

Solution code:
import sys
import math
import copy
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  # inv[j] = j^{-1} mod MOD
    fac = [0] * SIZE  # fac[j] = j! mod MOD
    finv = [0] * SIZE  # finv[j] = (j!)^{-1} mod MOD
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def renritsu(A, Y):
    # example 2x + y = 3, x + 3y = 4
    # A = [[2,1], [1,3]])
    # Y = [[3],[4]] または [3,4]
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    # [1.0, 1.0]
    return X.flatten().tolist()[0]

class TwoDimGrid:
    # 2次元座標 -> 1次元
    def __init__(self, h, w, wall="#"):
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        # self.init_cost()

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        # 壁も含めて0-indexed 元々の座標だけ考えると1-indexed
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return
    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        # for i in range(1, self.h+1):
        #     for j in range(1, self.w+1):
        #         cx, cy = j, i
        #         for dx, dy in move_eight:
        #             nx, ny = dx + cx, dy + cy

def solve():
    s = getS()
    x, y = getList()
    ls = len(s)
    hori = True
    h,v = [],[]
    cnt = 0
    for c in s:
        if c == "F":
            cnt += 1
        else:
            if hori:
                h.append(cnt)
            else:
                v.append(cnt)
            cnt = 0
            hori = not hori

    if cnt != 0:
        if hori:
            h.append(cnt)
        else:
            v.append(cnt)

    # print(h, v)
    dp = [0] * (ls * 2 + 100)
    dp[ls + 50] = 1
    for num in h:
        tmp = [0] * (len(dp))
        for i, d in enumerate(dp):
            if d == 1:
                tmp[i - num] = 1
                tmp[i + num] = 1
        dp = tmp
    # print(dp)
    if dp[x + ls + 50] == 0:
        print("No")
        return

    dp = [0] * (ls * 2 + 100)
    dp[ls + 50] = 1
    for num in v:
        tmp = [0] * (len(dp))
        for i, d in enumerate(dp):
            if d == 1:
                tmp[i - num] = 1
                tmp[i + num] = 1
        dp = tmp
    # print(dp)
    if dp[y + ls + 50] == 0:
        print("No")
        return

    print("Yes")
    return


def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    # main()
    solve()

Submission status: Wrong Answer

Solution code:
import sys
import math
import copy
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  # inv[j] = j^{-1} mod MOD
    fac = [0] * SIZE  # fac[j] = j! mod MOD
    finv = [0] * SIZE  # finv[j] = (j!)^{-1} mod MOD
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def renritsu(A, Y):
    # example 2x + y = 3, x + 3y = 4
    # A = [[2,1], [1,3]])
    # Y = [[3],[4]] または [3,4]
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    # [1.0, 1.0]
    return X.flatten().tolist()[0]

class TwoDimGrid:
    # 2次元座標 -> 1次元
    def __init__(self, h, w, wall="#"):
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        # self.init_cost()

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        # 壁も含めて0-indexed 元々の座標だけ考えると1-indexed
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return
    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        # for i in range(1, self.h+1):
        #     for j in range(1, self.w+1):
        #         cx, cy = j, i
        #         for dx, dy in move_eight:
        #             nx, ny = dx + cx, dy + cy

def solve():
    s = getS()
    x, y = getList()
    ls = len(s)
    hori = True
    h,v = [],[]
    cnt = 0
    for c in s:
        if c == "F":
            cnt += 1
        else:
            if hori:
                h.append(cnt)
            else:
                v.append(cnt)
            cnt = 0
            hori = not hori

    if cnt != 0:
        if hori:
            h.append(cnt)
        else:
            v.append(cnt)

    # print(h, v)
    dp = [0 for i in range(-ls - 100, ls + 100)]
    dp[ls + 100] = 1
    for num in h:
        tmp = [0 for i in range(len(dp))]
        for i, d in enumerate(dp):
            if d == 1:
                if i - num >= 0:
                    tmp[i - num] = 1
                if i + num < len(dp):
                    tmp[i + num] = 1
        dp = tmp
    # print(dp)
    if dp[x + ls + 100] == 0:
        print("No")
        return

    dp = [0 for i in range(-ls - 100, ls + 100)]
    dp[ls+100] = 1
    for num in v:
        tmp = [0 for i in range(len(dp))]
        for i, d in enumerate(dp):
            if d == 1:
                if i - num >= 0:
                    tmp[i - num] = 1
                if i + num < len(dp):
                    tmp[i + num] = 1
        dp = tmp
    # print(dp)
    if dp[y + ls + 100] == 0:
        print("No")
        return

    print("Yes")
    return


def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    # main()
    solve()

Submission status: Wrong Answer

Solution code:
import sys
import math
import copy
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  # inv[j] = j^{-1} mod MOD
    fac = [0] * SIZE  # fac[j] = j! mod MOD
    finv = [0] * SIZE  # finv[j] = (j!)^{-1} mod MOD
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def renritsu(A, Y):
    # example 2x + y = 3, x + 3y = 4
    # A = [[2,1], [1,3]])
    # Y = [[3],[4]] または [3,4]
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    # [1.0, 1.0]
    return X.flatten().tolist()[0]

class TwoDimGrid:
    # 2次元座標 -> 1次元
    def __init__(self, h, w, wall="#"):
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        # self.init_cost()

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        # 壁も含めて0-indexed 元々の座標だけ考えると1-indexed
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return
    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        # for i in range(1, self.h+1):
        #     for j in range(1, self.w+1):
        #         cx, cy = j, i
        #         for dx, dy in move_eight:
        #             nx, ny = dx + cx, dy + cy

def solve():
    s = getS()
    x, y = getList()
    ls = len(s)
    hori = True
    h,v = [],[]
    cnt = 0
    for c in s:
        if c == "F":
            cnt += 1
        else:
            if hori:
                h.append(cnt)
            else:
                v.append(cnt)
            cnt = 0
            hori = not hori

    if cnt != 0:
        if hori:
            h.append(cnt)
        else:
            v.append(cnt)

    # print(h, v)
    dp = [0 for i in range(-ls - 100, ls + 100)]
    dp[ls + 100] = 1
    for num in h:
        tmp = copy.copy(dp)
        for i, d in enumerate(dp):
            if d == 1:
                if i - num >= 0:
                    tmp[i - num] = 1
                if i + num < len(dp):
                    tmp[i + num] = 1
        dp = tmp
    if dp[x + ls + 100] == 0:
        print("No")
        return

    dp = [0 for i in range(-ls - 100, ls + 100)]
    dp[ls+100] = 1
    for num in v:
        tmp = copy.copy(dp)
        for i, d in enumerate(dp):
            if d == 1:
                if i - num >= 0:
                    tmp[i - num] = 1
                if i + num < len(dp):
                    tmp[i + num] = 1
        dp = tmp
        # print(dp)
    if dp[y + ls + 100] == 0:
        print("No")
        return
    # print(dp)
    print("Yes")
    return


def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    # main()
    solve()