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

Solution (Python):
import sys
INF = 10 ** 9
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)

def main(): 
    s = input() + 'a'
    x,y = map(int,input().split())
    
    cnt_t = 0
    cnt_f = 0
    p = []
    q = []
    before = True
    for i in range(len(s)-1):
        if s[i] == 'T' :
            cnt_t += 1
        elif s[i] == 'F' and s[i+1] == 'F':
            cnt_f += 1
        else:
            cnt_f += 1
            if cnt_t == 0:
                x -= cnt_f
            elif cnt_t%2 == 0:
                if before:
                    p.append(cnt_f)
                else:
                    q.append(cnt_f)
            else:
                if before:
                    q.append(cnt_f)
                    before = False
                else:
                    p.append(cnt_f)
                    before = True
            cnt_t = 0
            cnt_f = 0
    
    
    p.sort(reverse=True)
    q.sort(reverse=True)

    s = 0
    for i in range(len(p)):
        if s <= x:
            s += p[i]
        else:
            s -= p[i]
    
    t = 0
    for i in range(len(q)):
        if t <= y:
            t += q[i]
        else:
            t -= q[i]
    
    if s == x and t == y:
        print('Yes')
    else:
        print('No')

if __name__=='__main__':
    main()

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

mv = [len(e) for e in s.split("T")]
mv_x = mv[2::2]
mv_y = mv[1::2]


def solve_dp(li, start, end):
    l = len(li)
    mx = sum(li)

    if l == 0:
        return start == end

    left = start - mx
    right = start + mx

    if end > right or end < left:
        return 0

    dp = [[0] * (2 * mx + 1) for _ in range(l + 1)]
    dp[0][start] = 1
    for i, e in enumerate(li, 1):
        for j in range(left, right + 1):
            if left <= j - e <= right:
                dp[i][j] |= dp[i-1][j-e]
            if left <= j + e <= right:
                dp[i][j] |= dp[i-1][j+e]

    return dp[l][end]


bl_x = solve_dp(mv_x, mv[0], x)
bl_y = solve_dp(mv_y, 0, y)

if bl_x and bl_y:
    ans = "Yes"
else:
    ans = "No"

print(ans)

Solution (Python):
s=input()
x,y=map(int,input().split())
lx=[]
ly=[]
t=0
k=0
for i in s:
    if i=="F":
        t+=1
    else:
        if k:
            ly.append(t)
            k=0
        else:
            lx.append(t)
            k=1
        t=0
if k:
    ly.append(t)
else:
    lx.append(t)
x-=lx.pop(0)
sx,sy={0},{0}
for i in lx:
    t=set()
    for j in sx:
        t.add(i+j)
        t.add(j-i)
    sx=t
for i in ly:
    t=set()
    for j in sy:
        t.add(i+j)
        t.add(j-i)
    sy=t
if x in sx and y in sy:
    print("Yes")
else:
    print("No")

Solution (Python):
s=input()
x,y=map(int,input().split())
lx=[]
ly=[]
t=0
k=0
for i in s:
    if i=="F":
        t+=1
    else:
        if k:
            ly.append(t)
            k=0
        else:
            lx.append(t)
            k=1
        t=0
if k:
    ly.append(t)
else:
    lx.append(t)
x-=lx.pop(0)
sx,sy=sum(lx),sum(ly)
dx=[0]*(sx*2+1)
dy=[0]*(sy*2+1)
dx[0],dy[0]=1,1
for i in lx:
    t=[0]*(sx*2+1)
    for j in range(-sx,sx+1):
        if dx[j]:
            t[i+j]=1
            t[j-i]=1
    dx=t
for i in ly:
    t=[0]*(sy*2+1)
    for j in range(-sy,sy+1):
        if dy[j]:
            t[i+j]=1
            t[j-i]=1
    dy=t
if sx>=abs(x) and sy>=abs(y) and dx[x] and dy[y]:
    print("Yes")
else:
    print("No")

Solution (Python):
"""from collections import *
from itertools import *
from bisect import *
from heapq import *

import math
from fractions import gcd"""
S=input()
x,y=map(int,input().split())

Xlst=[]
Ylst=[]
count=0
flag=0
for s in S:
    if s=="F":
        count+=1
    else:
        if flag:
            if count:
                Ylst.append(count)
            flag=0
        else:
            if count:
                Xlst.append(count)
            flag=1
        count=0
if flag:
    if count:
        Ylst.append(count)
else:
    if count:
        Xlst.append(count)

if S[0]=="T":
    numx=set([0])
else:
    numx=set([Xlst[0]])
nlst=set()
c=0
for i in range(0 if 0 in numx else 1,len(Xlst)):
    X=Xlst[i]
    if c%2==0:
        while numx:
            a=numx.pop()
            nlst.add(a+X)
            nlst.add(a-X)
    else:
        while nlst:
            a=nlst.pop()
            numx.add(a+X)
            numx.add(a-X)
    c+=1
if x in numx|nlst:
    numx=set([0])
    nlst=set()
    c=0
    for X in Ylst:
        if c%2==0:
            while numx:
                a=numx.pop()
                nlst.add(a+X)
                nlst.add(a-X)
        else:
            while nlst:
                a=nlst.pop()
                numx.add(a+X)
                numx.add(a-X)
        c+=1
    if y in numx|nlst:
        print("Yes")
    else:
        print("No")
else:
    print("No")

Solution (Python):
order = list(map(len, input().split("T")))
x, y = map(int, input().split())
ans = "No"
lon = [order[i] for i in range(len(order)) if i % 2 == 0]
lat = [order[i] for i in range(len(order)) if i % 2 == 1]
x -= lon[0]
lon = lon[1:]
def f(x, A):
  A.append(abs(x))
  A.sort(reverse=True)
  k = A[0]
  for p in A[1:]:
      if k > 0:
        k -= p
      else:
        k += p
  if k == 0:
    return True
  else:
    return False
  

if f(x, lon) == f(y, lat) == True:
  ans = "Yes"
  
print(ans)

Solution (Python):
s = input().split('T')
X, Y = map(int, input().split())

x = [len(i) for i in s[0::2]]
y = [len(i) for i in s[1::2]]

X = X - x[0]

x, y = sorted(x[1:]), sorted(y)

while x:
    mx = x.pop()
    if X < 0:
        X += mx
    else:
        X -= mx

while y:
    my = y.pop()
    if Y < 0:
        Y += my
    else:
        Y -= my

print('Yes' if X == 0 and Y == 0 else 'No')

Solution (Python):
s = input()
N = len(s)
x,y = map(int, input().split())
ls = s.split('T')
A = []
B = []
dpx = [0]*(2*N+1)
dpy = [0]*(2*N+1)
for i,c in enumerate(ls):
  if i%2==0:
    A += [len(c)]
  else:
    B += [len(c)]
x -= A[0]
A = A[1:]
dpx[N] = 1
dpy[N] = 1
for a in A:
  dpx = [1 if (i>=a and dpx[i-a]) or (i<=2*N-a and dpx[i+a]) else 0 for i in range(2*N+1)]
for b in B:
  dpy = [1 if (i>=b and dpy[i-b]) or (i<=2*N-b and dpy[i+b]) else 0 for i in range(2*N+1)]
if x>N or y>N:
  print('No')
elif dpx[x+N] and dpy[y+N]:
  print('Yes')
else:
  print('No')

Solution (Python):
s = input()
x, y = map(int, input().split())
n = len(s)
S = [[], []]
d = 0
f = 0
for i in range(n):
  if s[i] == 'T':
    if f != 0:
      S[d].append(f)
    d += 1
    d %= 2
    f = 0
  elif s[i] == 'F':
    f += 1
if s[n-1] == 'F':
  S[d].append(f)
if S[0] == []:
  S[0].append(0)
if S[1] == []:
  S[1].append(0)
px, py = 0, 0
if s[0] == 'F':
  px += S[0][0]
  S[0] = S[0][1:]
for i in range(len(S[0])):
  px -= S[0][i]
  S[0][i] *= 2
for i in range(len(S[1])):
  py -= S[1][i]
  S[1][i] *= 2
mpx, mpy = x - px, y - py
X = S[0][:]
Y = S[1][:]
Dx = {0: 1}
Dy = {0: 1}
for i in X:
  L = [i for i in Dx]
  for j in L:
    if i + j <= mpx and i + j not in Dx:
      Dx[i+j] = 1
for i in Y:
  L = [i for i in Dy]
  for j in L:
    if i + j <= mpy and i + j not in Dy:
      Dy[i+j] = 1

if mpx in Dx and mpy in Dy:
  print("Yes")
else:
  print("No")

Solution (Python):
import sys
input = sys.stdin.readline
s = input()[: -1]
t = s.split("T")
x, y = map(int, input().split())
x -= t[0].count("F")
xm = t[: : 2]
ym = t[1: : 2]
xl = 0
yl = 0
if "F" in t[0]: xm = xm[1: ]
for i in range(len(xm)): xl += len(xm[i])
for i in range(len(ym)): yl += len(ym[i])
if xl < abs(x) or (yl < abs(y)):
  print("No")
  exit(0)

dp = [[0] * (xl * 2 + 1) for _ in range(len(xm) + 1)]
dp[0][xl] = 1
for i in range(len(xm)):
  for j in range(xl * 2 + 1):
    z = len(xm[i])
    if (j + z) in range(xl * 2 + 1):
      dp[i + 1][j + z] |= dp[i][j]
    if (j - z) in range(xl * 2 + 1):
      dp[i + 1][j - z] |= dp[i][j]
if dp[-1][xl + x] == 0:
  print("No")
  exit(0)
dp = [[0] * (yl * 2 + 1) for _ in range(len(ym) + 1)]
dp[0][yl] = 1
for i in range(len(ym)):
  for j in range(yl * 2 + 1):
    z = len(ym[i])
    if (j + z) in range(yl * 2 + 1):
      dp[i + 1][j + z] |= dp[i][j]
    if (j - z) in range(yl * 2 + 1):
      dp[i + 1][j - z] |= dp[i][j]
if dp[-1][yl + y] == 0:
  print("No")
  exit(0)
print("Yes")

Solution (Python):
S = input()
x, y = map(int, input().split())

S = S.split("T")
X_move = [len(s) for s in S[0::2]]
Y_move = [len(s) for s in S[1::2]]

x -= X_move[0]
X_move = sorted(X_move[1:])
Y_move = sorted(Y_move)

while X_move:
    mx = X_move.pop()
    if x >= 0:
        x -= mx
    else:
        x += mx

while Y_move:
    my = Y_move.pop()
    if y >= 0:
        y -= my
    else:
        y += my

if x == 0 and y == 0:
    ans = "Yes"
else:
    ans = "No"

print(ans)

Solution (Python):
def main():
    order = [len(el) for el in list(map(str, input().rstrip().split("T")))]
    x_points = {order[0]}
    y_points = {0}
    for val in order[2::2]:
        plus_val = {x + val for x in list(x_points)}
        minus_val = {x - val for x in list(x_points)}
        x_points = plus_val | minus_val
    for val in order[1::2]:
        plus_val = {y + val for y in list(y_points)}
        minus_val = {y - val for y in list(y_points)}
        y_points = plus_val | minus_val
    target_x, target_y = map(int, input().split())
    if target_x in x_points and target_y in y_points:
        print("Yes")
    else:
        print("No")

if __name__ == "__main__":
    main()

Solution (Python):
s = input()
l = len(s)
s = 'F' + s
x,y = map(int,input().split())
raw = [1]
for i in range(1,l+1):
  if s[i] == 'F':
    if s[i-1] == 'F':
      raw[-1] += 1
    else:
      raw.append(1)
  if s[i] == 'T':
    if s[i-1] == 'T':
      raw.append(0)
l2 = len(raw)
a = []
b = []
for i in range(l2):
  if i == 0:
    x -= raw[i]-1
  elif i % 2 == 0:
    a.append(raw[i])
  else:
    b.append(raw[i])



nowa = {0}
for aa in a:
  lasta = nowa
  nowa = set()
  for aaa in lasta:
    nowa.add(aaa+aa)
    nowa.add(aaa-aa)
nowb = {0}
for bb in b:
  lastb = nowb
  nowb = set()
  for bbb in lastb:
    nowb.add(bbb+bb)
    nowb.add(bbb-bb)


if x in nowa and y in nowb:
  print('Yes')
else:
  print('No')

Solution (Python):
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
mod=10**9+7

s=input()+"T"
N=len(s)
x,y=MI()

F=[[],[]]

temp=0
bit=0
for i in range(N):
    if s[i]=="F":
        temp+=1
    else:
        if temp!=0:
            F[bit].append(temp)
        bit=(bit+1)%2
        temp=0
        
XY=[set([0]), set([0])]
    
for bit in range(2):
    for i in range(len(F[bit])):
        temp=set([])
        a=F[bit][i]
        b=-a
        if bit==0 and i==0 and s[0]=="F":
            b=a
        for j in range(len(XY[bit])):
            aaa=XY[bit].pop()
            temp.add(aaa+a)
            temp.add(aaa+b)
        XY[bit]=temp
        
if (x in XY[0] ) and (y in XY[1] ):
    print("Yes")
else:
    print("No")

Solution (Python):
import sys

def solve():
    input = sys.stdin.readline
    s = input().strip("\n")
    s += "T"
    lens = len(s)
    x, y = map(int, input().split())
    direc = 0 
    move = 0
    init = True
    xPossible = {0}
    yPossible = {0}
    for i in range(lens):
        if s[i] == "F": move += 1
        else:
            if direc == 0: 
                newSet = set()
                for item in xPossible:
                    if init:
                        newSet |= {item + move}
                    else:
                        newSet |= {item + move}
                        newSet |= {item - move}
                xPossible = newSet
                
                
            else: 
                newSet = set()
                for item in yPossible:
                    newSet |= {item + move}
                    newSet |= {item - move}
                yPossible = newSet
                
            move = 0
            direc = (direc + 1) % 2
            if init: init = False

    if x in xPossible and y in yPossible: print("Yes")
    else: print("No")

    return 0

if __name__ == "__main__":
    solve()

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

mx = []
my = []
mx2 = []
my2 = []


cnt = 0
it = 0
firstFlag = (s[0]=='F')
for t in s:
    if t == 'T':
        
        
        if cnt:
            if it%2==0:
                mx.append(cnt)
                mx2.append(cnt*2)
            else:
                my.append(cnt)
                my2.append(cnt*2)
        it += 1
        cnt = 0
    else:
        cnt += 1
    
if it%2==0:
    mx.append(cnt)
    mx2.append(cnt*2)
else:
    my.append(cnt)
    my2.append(cnt*2)

if firstFlag:
    x -= mx[0]
    mx[0] = 0
    mx2[0] = 0
        
dx = sum(mx)-x
dy = sum(my)-y




MAX = max(sum(mx2),sum(my2)) + 10
dpx = [[False]*MAX for _ in range(len(mx2)+1)]
dpy = [[False]*MAX for _ in range(len(my2)+1)]

dpx[0][0] = True
dpy[0][0] = True

for i in range(len(mx2)):
    for j in range(MAX):
        if j<mx2[i]:
            dpx[i+1][j] = dpx[i][j]
        else:
            dpx[i+1][j] = dpx[i][j-mx2[i]] or dpx[i][j]

for i in range(len(my2)):
    for j in range(MAX):
        if j<my2[i]:
            dpy[i+1][j] = dpy[i][j]
        else:
            dpy[i+1][j] = dpy[i][j-my2[i]] or dpy[i][j]



if dx<0 or dy<0:
    print('No')

elif dx>sum(mx2) or dy>sum(my2):
    print('No')

else:
    if dpx[len(mx2)][dx] and dpy[len(my2)][dy]:
        print('Yes')
    else:
        print('No')

Solution (Python):
def get_move(S):
    S = S.split("T")
    x_move = [len(x) for x in S[0::2]]
    y_move = [len(x) for x in S[1::2]]
    return x_move, y_move


def check(x, move):
    move.sort()
    while move:
        m = move.pop()
        if x >= 0:
            x -= m
        else:
            x += m
    return x == 0


if __name__ == "__main__":
    S = input()
    x, y = map(int, input().split())

    x_move, y_move = get_move(S)
    
    x -= x_move[0]

    if check(x, x_move[1:]) and check(y, y_move):
        ans = "Yes"
    else:
        ans = "No"

    print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 9)




s = input().split('T')
x, y = [int(_) for _ in input().split()]


def m(dp, first_idx):
    for i in range(first_idx, len(s), 2):
        step = len(s[i])

        next_dp = set()
        for item in dp:
            next_dp.add(item + step)
            next_dp.add(item - step)
        dp = next_dp

    return dp


if len(s) > 0:
    xs = {len(s[0])}
    xs = m(xs, 2)
else:
    xs = {0}
if len(s) > 1:
    ys = m({0}, 1)
else:
    ys = {0}

if x in xs and y in ys:
    print('Yes')
else:
    print('No')

Solution (Python):
def solve():
    S = input()
    X,Y = map(int, input().split())
    
    S_F = S.split('T')
    move_x = list(map(len, S_F[::2]))
    move_y = list(map(len,S_F[1::2]))
    
    if move_x:
        X -= move_x[0]
        move_x = move_x[1:]
    
    
    if sum(move_x) < abs(X) or sum(move_y) < abs(Y):
        print('No')
        return
    
 
    move_x_process = [mx for mx in move_x if mx > 0]
    move_y_process = [my for my in move_y if my > 0]
    X = abs(X); Y = abs(Y)
    len_move_x = len(move_x_process)
    sum_x = sum(move_x_process)
    len_move_y = len(move_y_process)
    sum_y = sum(move_y_process)
 
    dp_table = [[False for _ in range(sum_x+1)] for _ in range(len_move_x+1)]
    dp_table[0][0] = True
    
    for i in range(0,len_move_x):
        for j in range(0, sum_x+1):
            dp_table[i+1][j] = dp_table[i][abs(j-move_x_process[i])]
            if j+move_x_process[i] <= sum_x: 
                dp_table[i+1][j] = dp_table[i+1][j] or dp_table[i][j+move_x_process[i]]
    
    
    ret = dp_table[len_move_x][X]
    dp_table = [[False for _ in range(sum_y+1)] for _ in range(len_move_y+1)]
    dp_table[0][0] = True
    for i in range(0,len_move_y):
        for j in range(0, sum_y+1):
            
            dp_table[i+1][j] = dp_table[i][abs(j-move_y_process[i])]
            if j+move_y_process[i] <= sum_y:
                dp_table[i+1][j] = dp_table[i+1][j] or dp_table[i][j+move_y_process[i]]
    
    ret = ret and dp_table[len_move_y][Y]
    print('Yes' if ret else 'No')
    
solve()

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

S = list(map(len, s.split("T")))
Sx = []
Sy = []

for i in range(len(S)):
  if i % 2 == 0:
    Sx.append(S[i])
  else:
    Sy.append(S[i])

maxX = sum(Sx)
maxY = sum(Sy)

setX = set([maxX])
flg = True
for i in Sx:
  if flg:
    
    flg = False
    continue
  li = list(setX)
  for a in li:
    setX.add(a - 2*i)
    
setY = set([maxY])
for i in Sy:
  li = list(setY)
  for a in li:
    setY.add(a - 2*i)
    
if x in setX and y in setY:
  print("Yes")
else:
  print("No")

Solution (Python):
import numpy as np

s = input() + "T"
x, y = map(int, input().split())
A = []
B = []
now = 0
di = 1
for i in s:
  if i=="T":
    if di:
      A.append(now)
    else:
      B.append(now)
    now = 0
    di ^= 1
  else:
    now += 1

def test(L, dist):
  dist = abs(dist)
  su = sum(L)
  l = len(L)
  if su < dist or (su-dist)%2:
    return False
  can = (su-dist)//2
  dp = np.zeros(can+1, dtype=bool)
  dp[0] = True
  for i in L:
    if i <= can:
      dp[i:] |= dp[:can+1-i]
  return dp[-1]

if s[0] == "F":
  a = test(A[1:], x-A[0])
else:
  a = test(A, x)
b = test(B, y)
if a and b:
  print("Yes")
else:
  print("No")

Solution (Python):
s = input()
x,y = map(int,input().split())
l = []
cnt = 0
for i in s:
    if i=='T':
        l.append(cnt)
        cnt = 0
    else:
        cnt+=1
l.append(cnt)
horizontal = l[2::2]
vartical = l[1::2]
horizontal.sort()
horizontal = horizontal[::-1]
vartical.sort()
vartical = vartical[::-1]
x-=l[0]
now_x = 0
now_y = 0
for i in horizontal:
    if now_x<x:
        now_x+=i
    else:
        now_x-=i
for i in vartical:
    if now_y<y:
        now_y+=i
    else:
        now_y-=i
if now_x==x and now_y==y:
    print('Yes')
else:
    print('No')

Solution (Python):
import sys


def main():
    s = input().split("T")
    x,y = map(int,input().split())
    l = len(s)
    dx,dy = [len(s[0])],[0]
    nx,ny = [],[]
    for i in range(1,l):
        d = len(s[i])
        if i%2 == 0:
            nx = []
            for num in dx:
                nx.append(num+d)
                nx.append(num-d)
            dx = list(set(nx))
        else:
            ny = []
            for num in dy:
                ny.append(num+d)
                ny.append(num-d)
            dy = list(set(ny))

    if (x in dx and y in dy):
        print("Yes")
    else:
        print("No")
    
if __name__ == "__main__":
    main()

Solution (Python):
s = input().split('T')
dx = list(map(len, s[0::2]))
dy = list(map(len, s[1::2]))
x, y = map(int, input().split())

def check(start, ds, goal):
    cands = {start}
    for d in ds:
        new_cands = set()
        for c in cands:
            new_cands.add(c - d)
            new_cands.add(c + d)
        cands = new_cands
    return goal in cands

x_ok = check(dx[0], dx[1:], x)
y_ok = check(0, dy, y)

print('Yes' if x_ok and y_ok else 'No')

Solution (Python):
def f(start, goal, steps):
  res = set([start])
  for s in steps:
    next_res = set()
    for r in res:
      next_res.add(r + s)
      next_res.add(r - s)
    res = next_res
  return goal in res

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

ss = s.split("T")
xmove = [i.count("F") for i in ss[::2]]
xfirst = xmove[0] 
xmove = xmove[1:] 
ymove = [i.count("F") for i in ss[1::2]]

if f(xfirst, x, xmove) and f(0, y, ymove):
  print("Yes")
else:
  print("No")

Solution (Python):
def f(start, goal, steps):
  res = set([start])
  for s in steps:
    next_res = set()
    for r in res:
      next_res.add(r + s)
      next_res.add(r - s)
    res = next_res
  return goal in res

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

ss = s.split("T")
xmove = [i.count("F") for i in ss[::2]]
xfirst = xmove[0] 
xmove = xmove[1:] 
ymove = [i.count("F") for i in ss[1::2]]

if f(xfirst, x, xmove) and f(0, y, ymove):
  print("Yes")
else:
  print("No")

Solution (Python):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
mod=pow(10,9)+7

s=input()
x,y=map(int,input().split())
lst=[[],[]]
cnt=0
tmp=0
for i in range(len(s)):
    if s[i]=="F":
        tmp+=1
    else:
        if cnt==0:
            x-=tmp
        else:
            if tmp!=0:
                lst[cnt%2].append(tmp)
        tmp=0
        cnt+=1
if cnt==0:
    x-=tmp
elif tmp!=0:
    lst[cnt%2].append(tmp)

sumx=sum(lst[0])
sumy=sum(lst[1])

if sumx<x or x<-sumx:
    print("No")
    exit()
if sumy<y or y<-sumy:
    print("No")
    exit()
dpx=[[0]*(sumx*2+1) for i in range(len(lst[0])+1)]
dpy=[[0]*(sumy*2+1) for i in range(len(lst[1])+1)]
dpx[0][sumx]=1
dpy[0][sumy]=1

for i in range(len(lst[0])):
    for j in range(sumx*2+1):
        if dpx[i][j]!=0:
            dpx[i+1][j+lst[0][i]]+=1
            dpx[i+1][j-lst[0][i]]+=1

for i in range(len(lst[1])):
    for j in range(sumy*2+1):
        if dpy[i][j]!=0:
            dpy[i+1][j+lst[1][i]]+=1
            dpy[i+1][j-lst[1][i]]+=1



if dpx[len(lst[0])][sumx+x]!=0 and dpy[len(lst[1])][sumy+y]!=0:
    print("Yes")
else:
    print("No")

Solution (Python):
S = input()
X, Y = map(int, input().split())

A = [0]
for s in S:
    if s == 'T':
        A.append(0)
    else:
        A[-1] += 1

B = [n for n in A[1:: 2]]
X -= A[0]
A = [n for n in A[2:: 2]]

def canGo(nums, g):
    if not nums:
        return g == 0

    bias = sum(nums) + 10
    g += bias
    if g < 0:
        return False

    dp = (1 << bias)

    for n in nums:
        dp = (dp << n) | (dp >> n)
    return (dp & (1 << g)) > 0

if canGo(A, X) and canGo(B, Y):
    print('Yes')
else:
    print('No')

Solution (Python):
S = input()
X, Y = map(int, input().split())

def run_length(s) -> list:
    res = [[s[0], 1]]
    for i in range(1, len(s)):
        if res[-1][0] == s[i]:
            res[-1][1] += 1
        else:
            res.append([s[i], 1])
    return res

RL = run_length(S)
if RL[0][0] == 'T':
    RL = [['F', 0]] + RL

SIZE = len(S)
nx = 1 << SIZE
ny = 1 << SIZE

x_flag = 1
for i in range(0, len(RL), 2):
    s, l = RL[i]
    if i == 0:
        nx = nx << l
    else:
        x_flag = (x_flag + RL[i - 1][1]) % 2
        if x_flag:
            nx = (nx << l) | (nx >> l)
        else:
            ny = (ny << l) | (ny >> l)

if ((nx >> (SIZE + X)) & 1) & ((ny >> (SIZE + Y)) & 1):
    print("Yes")
else:
    print("No")

Solution (Python):
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

def foo(fe,x,ev):
    k = sum(fe)+1
    n = len(fe)
    d = [ [False]*(2*k+1) for i in range(n+1) ]
    d[0][k] = True
    if len(fe)>0:
        d[1][fe[0]+k] = True
        if not ev:
            d[1][-fe[0]+k] = True
    for i in range(1,n):
        for j in range(-k,k+1):
            if d[i][j+k]:
                d[i+1][j+fe[i]+k] = True
                d[i+1][j-fe[i]+k] = True
    return -k<=x<=k and d[n][x+k]

s = ins()
x,y = inm()
f = [ [], [] ]
strk = 0
turn = 0
n = len(s)
for i in range(n):
    if s[i]=='T':
        f[turn].append(strk)
        turn = 1 - turn
        strk = 0
    else:
        strk += 1
f[turn].append(strk)
if foo(f[0],x,True) and foo(f[1],y,False):
    print('Yes')
else:
    print('No')

Solution (Python):
def check(arr, x):
    pos = set([0])
    for dx in arr:
        npos = set()
        for p in pos:
            npos.add(p+dx)
            npos.add(p-dx)
        pos = npos
    return x in pos


s = input().split('T')
x, y = map(int, input().split())
d = [len(ss) for ss in s]
dx = d[2::2]
dy = d[1::2]
x -= d[0]
if check(dx, x) and check(dy, y):
    print('Yes')
else:
    print('No')

Solution (Python):
import sys

sys.setrecursionlimit(10**6)
input = sys.stdin.readline

def main():
    order_F = []
    order_F = input().rstrip().split('T')
    x, y = map(int, input().split())
    vert_DP = [False] * 8001
    side_DP = [False] * 8001
    vert_DP[0] = True
    side_DP[0] = True
    vert_sum = 0
    side_sum = 0
    distance = [len(order_F[i]) for i in range(len(order_F))]
    order_len = len(order_F)
    x -= len(order_F[0])
    vert_last = 0
    side_last = 0
    for i in range(1, order_len):
        if i % 2 == 0:
            side_sum += distance[i]
            for j in reversed(range(side_last+1)):
                if side_DP[j]:
                    side_DP[j + distance[i]] = True
            side_last += distance[i]
        else:
            vert_sum += distance[i]
            for j in reversed(range(vert_last+1)):
                if vert_DP[j]:
                    vert_DP[j + distance[i]] = True
            vert_last += distance[i]
    if side_sum < x or vert_sum < y:
        print("No")
    elif (side_sum - x) % 2 == 1 or (vert_sum - y) % 2 == 1:
        print("No")
    else:
        if side_DP[(side_sum - x)//2] and vert_DP[(vert_sum - y)//2]:
            print("Yes")
        else:
            print("No")

if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.readline
s=list(input())
x,y = map(int, input().split())
y=abs(y)
s.append('T')
x1=[]
y1=[]

count=0
u=0
for i in range(len(s)):
    if s[i]=='F':
        count+=1
    else:
        if u%2==0:
            x1.append(count)
            u+=1
            count=0
        else:
            y1.append(count)
            u+=1
            count=0
v=sum(x1)
w=sum(y1)
v-=x1[0]
x-=x1[0]
x1.pop(0)
x=abs(x)
if v<x or w<y:
    print('No')
    exit()

dpx=[[0 for i in range(v+1)] for j in range(len(x1)+1)]
dpx[0][-1]=1

for i in range(len(x1)):
    for j in range(v+1):
        if dpx[i][j]==1:
            if j-2*x1[i]>=0:
                dpx[i+1][j-2*x1[i]]=1
            dpx[i+1][j]=1

if len(y1)==0:
    y1.append(0)
dpy=[[0 for i in range(w+1)] for j in range(len(y1)+1)]
dpy[0][-1]=1

for i in range(len(y1)):
    for j in range(w+1):
        if dpy[i][j]==1:
            if j-2*y1[i]>=0:
                dpy[i+1][j-2*y1[i]]=1
            dpy[i+1][j]=1

if dpy[-1][y]==1 and dpx[-1][x]==1:
    print('Yes')
else:
    print('No')

Solution (Python):
s = input()
n = len(s)
xy = list(map(int, input().split()))

right = 0
updown = []
rightleft = []

direction = 0
for i in range(n):
    if s[i]=='F':
        if direction == 0:
            right+=1
        elif direction == 1:
            updown[-1]+=1
        else:
            rightleft[-1]+=1
    else:
        if direction == 0 or direction == 2:
            direction = 1
            updown.append(0)
        else:
            direction = 2
            rightleft.append(0)

updown = [x for x in updown if x!=0]
rightleft = [x for x in rightleft if x!=0]


xy[0]-=right


dpy = [set() for i in range(len(updown)+1)]
dpy[0].add(0)
for i in range(len(updown)):
    for y in dpy[i]:
        dpy[i+1].add(y+updown[i])
        dpy[i+1].add(y-updown[i])

dpx = [set() for i in range(len(rightleft)+1)]
dpx[0].add(0)
for i in range(len(rightleft)):
    for x in dpx[i]:
        dpx[i+1].add(x+rightleft[i])
        dpx[i+1].add(x-rightleft[i])

if xy[1] in dpy[-1] and xy[0] in dpx[-1]:
    print('Yes')
else:
    print('No')

Solution (Python):
s = input()

x,y = map(int,input().split())

now = 0
angle = 0 
length = 0 

if s[0] == "F":
    while now < len(s) and s[now] == "F":
        length += 1
        now += 1
    x -= length


xlst = []
ylst = []

length = 0 

while now < len(s): 
    while now < len(s) and s[now] == "T":
        angle = 1 - angle
        now += 1
    while now < len(s) and s[now] == "F":
        length += 1
        now += 1
    if angle == 0:
        xlst.append(length)
    else:
        ylst.append(length)
    length = 0

now_x = 0
now_y = 0
xlst.sort(reverse=True)
ylst.sort(reverse=True)
for i in xlst:
    if x - now_x >= 0:
        now_x += i
    else:
        now_x -= i

for i in ylst:
    if y - now_y >= 0:
        now_y += i
    else:
        now_y -= i

if now_x == x and now_y == y:
    print("Yes")
else:
    print("No")

Solution (Python):
s = input().strip()
x,y = map(int,input().split())
C = {}
cur = 0
cnt = 0
for i in range(len(s)):
    if s[i]=="F":
        cnt += 1
    else:
        C[cur] = cnt
        cur += 1
        cnt = 0
C[cur] = cnt
AX = []
AY = []
for a in C:
    if a>0 and a%2==0 and C[a]>0:
        AX.append(C[a])
    elif a%2==1 and C[a]>0:
        AY.append(C[a])
cntx = sum(AX)
dpx = [[0 for _ in range(2*cntx+1)] for _ in range(len(AX)+1)]
dpx[0][cntx] = 1
if cntx>0:
    dpx[1][cntx-AX[0]] = 1
    dpx[1][cntx+AX[0]] = 1
    for i in range(2,len(AX)+1):
        for j in range(-cntx,cntx+1):
            if -cntx<=j-AX[i-1]:
                dpx[i][j+cntx] = dpx[i-1][j-AX[i-1]+cntx]
                if j+AX[i-1]<=cntx:
                    dpx[i][j+cntx] = max(dpx[i][j+cntx],dpx[i-1][j+AX[i-1]+cntx])
            elif j+AX[i-1]<=cntx:
                dpx[i][j+cntx] = dpx[i-1][j+AX[i-1]+cntx]
cnty = sum(AY)
dpy = [[0 for _ in range(2*cnty+1)] for _ in range(len(AY)+1)]
dpy[0][cnty] = 1
if cnty>0:
    dpy[1][cnty-AY[0]] = 1
    dpy[1][cnty+AY[0]] = 1
    for i in range(2,len(AY)+1):
        for j in range(-cnty,cnty+1):
            if -cnty<=j-AY[i-1]:
                dpy[i][j+cnty] = dpy[i-1][j-AY[i-1]+cnty]
                if j+AY[i-1]<=cnty:
                    dpy[i][j+cnty] = max(dpy[i][j+cnty],dpy[i-1][j+AY[i-1]+cnty])
            elif j+AY[i-1]<=cnty:
                dpy[i][j+cnty] = dpy[i-1][j+AY[i-1]+cnty]
if C[0]-cntx<=x<=C[0]+cntx and dpx[len(AX)][x-C[0]+cntx]==1 and -cnty<=y<=cnty and dpy[len(AY)][y+cnty]==1:
    print("Yes")
else:
    print("No")

Solution (Python):
S = input()
x, y = map(int, input().split())
M = S.count("F")
F = list(map(len, S.split("T")))
F1 = F[::2]
B1 = 1 << sum(F1)
X = B1
X = X << x if x > 0 else X >> -x
B1 = B1 << F1[0]
for f in F1[1:]:
    B1 = (B1 << f) | (B1 >> f)
b1 = B1 & X
F2 = F[1::2]
B2 = 1 << sum(F2)
Y = B2
Y = Y << y if y > 0 else Y >> -y
for f in F2:
    B2 = (B2 << f) | (B2 >> f)
b2 = B2 & Y
print("YNeos"[not(b1 and b2)::2])