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):
s=input()+'T'
x,y=list(map(int,input().split()))
x+=8000
y+=8000
n=len(s)
i=0

while i<n and s[i]=='F':
    i+=1

dx=[[0]*16001 for _ in range(2)]
dy=[[0]*16001 for _ in range(2)]

dx[0][i+8000]=1
dy[0][8000]=1

lstx=[]
lsty=[]
d='x'
cnt=0

while i<n:
    if s[i]=='F':
        cnt+=1
    else:
        if cnt>0:
            if d=='x':
                lstx.append(cnt)
            else:
                lsty.append(cnt)
        
        if d=='x':
            d='y'
        else:
            d='x'
        cnt=0
    
    i+=1

for i,cx in enumerate(lstx):
    for j in range(16001):
        if dx[i%2][j]==0:
            continue
        if j+cx<16001:
            dx[(i+1)%2][j+cx]=1
        if j-cx>-1:
            dx[(i+1)%2][j-cx]=1
    dx[i%2]=[0]*16001

for i,cy in enumerate(lsty):
    for j in range(16001):
        if dy[i%2][j]==0:
            continue
        if j+cy<16001:
            dy[(i+1)%2][j+cy]=1
        if j-cy>-1:
            dy[(i+1)%2][j-cy]=1
    dy[i%2]=[0]*16001

if dx[(len(lstx))%2][x]==1 and dy[(len(lsty))%2][y]==1:
    print('Yes')
else:
    print('No')

Solution (Python):
from copy import copy
s = input() + "T"
x,y = map(int, input().split())

d = [len(p) for p in s.split("T")]
init = d[0]
d_x = d[2::2]
d_y = d[1::2]

xs = set([init])
ys = set([0])
dpx = [x in xs]
for d in d_x:
  xs_ = set()
  for u in xs:
    xs_.add(u+d)
    xs_.add(u-d)
  dpx.append(x in xs_)
  xs = copy(xs_)

dpy = []
for d in d_y:
  ys_ = set()
  for v in ys:
    ys_.add(v+d)
    ys_.add(v-d)
  dpy.append(y in ys_)
  ys = copy(ys_)

if dpx[-1] and dpy[-1]: print("Yes")
else: print("No")

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

move = [0]
for si in s:
    if(si  == 'F'):
        move[-1] += 1
    else:
        move.append(0)

move_x = move[2::2]
move_y = move[1::2]
for a,move_a in zip([x-move[0],y],[move_x,move_y]):
    m_max = sum(move_a)
    if(m_max < abs(a)):
        print('No')
        exit()

    dp = 2**m_max
    for mi in move_a:
        dp = (dp<<mi) | (dp>>mi)

    if(dp >> (m_max+a))&1:
        continue
    else:
        print('No')
        exit()

print('Yes')

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

S = list(input())+['T']
X, Y = mapint()
H = []
V = []

now_dirc = 1
cnt = 0
for s in S:
    if s=='F':
        cnt += 1
    else:
        if now_dirc==1:
            H.append(cnt)
        else:
            V.append(cnt)
        now_dirc*=-1
        cnt = 0

def check(now, goal, lis):
    dp = [0]*20000
    dp[10000+now] = 1
    for l in lis:
        nxdp = [0]*20000
        for i in range(20000):
            if i-l>=0 and dp[i-l]==1:
                nxdp[i] = 1
            if i+l<20000 and dp[i+l]==1:
                nxdp[i] = 1
        dp = nxdp
    if dp[goal+10000]:
        return True
    else:
        return False
    
if check(H[0], X, H[1:]) and check(0, Y, V):
    print('Yes')
else:
    print('No')

Solution (Python):
S = str(input())
N = len(S)
x,y = map(int,input().split())
X = []; Y =[]
Flag = True 
seq = 0
for i in range(N):
  if S[i] == "T":
    if Flag:
      X.append(seq)
    if not Flag:
      Y.append(seq)
    Flag = not Flag
    seq = 0
  else:
    seq += 1
    if i == N-1:
      if Flag:
        X.append(seq)
      if not Flag:
        Y.append(seq)

num = 8100 
MAX = num*2+1

dpx = [0 for _ in range(MAX)]
dpx[num] = 1
dpy = [0 for _ in range(MAX)]
dpy[num] = 1
for i in range(len(X)):
  px = [0 for _ in range(MAX)]
  px,dpx = dpx,px
  step = X[i]
  for j in range(MAX):
    if j -step >= 0:
      dpx[j] = max(dpx[j],px[j-step])
    if j + step < MAX and i != 0: 
      dpx[j] = max(dpx[j],px[j+step])
for i in range(len(Y)):
  py = [0 for _ in range(MAX)]
  py,dpy = dpy,py
  step = Y[i]
  for j in range(MAX):
    if j -step >= 0:
      dpy[j] = max(dpy[j],py[j-step])
    if j + step < MAX:
      dpy[j] = max(dpy[j],py[j+step])
x += num;y+= num


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

Solution (Python):
import sys
from collections import deque
def input(): return sys.stdin.readline().strip()


def main():
    s = input()
    x, y = map(int, input().split())
    """
    x, y方向独立に考えられることには気づいたけど、その後の遷移が2^N通りで無理だと思い込んでた。。。
    もらうdpで考えれば確かにO(N^2)で片付く。
    """
    C = [deque([]), []]
    direction = 0 
    cnt = 0
    for c in s:
        if c == 'F':
            cnt += 1
            continue
        if c == 'T':
            C[direction].append(cnt)
            cnt = 0
            direction = 1 - direction
    C[direction].append(cnt)
    
    X = [0] * 16001
    if s[0] == 'F':
        start = C[0].popleft()
        X[8000 + start] = 1
    else:
        X[8000] = 1
    for cnt in C[0]:
        X_new = [0] * 16001
        for i in range(16001):
            if 0 <= i - cnt and i + cnt <= 16000:
                X_new[i] = 1 - (1 - X[i - cnt]) * (1 - X[i + cnt])
            elif 0 <= i - cnt:
                X_new[i] = X[i - cnt]
            else:
                X_new[i] = X[i + cnt]
        X = X_new

    Y = [0] * 16001
    Y[8000] = 1
    for cnt in C[1]:
        Y_new = [0] * 16001
        for i in range(16001):
            if 0 <= i - cnt and i + cnt <= 16000:
                Y_new[i] = 1 - (1 - Y[i - cnt]) * (1 - Y[i + cnt])
            elif 0 <= i - cnt:
                Y_new[i] = Y[i - cnt]
            else:
                Y_new[i] = Y[i + cnt]
        Y = Y_new
    
    if X[8000 + x] == 1 and Y[8000 + y] == 1:
        print("Yes")
    else:
        print("No")

    


if __name__ == "__main__":
    main()

Solution (Python):
import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())  

s = LS2()
x,y = MI()

A = []  
B = []  
r = 0
for i in range(len(s)):
    if i == 0:
        if s[i] == 'F':
            A.append(1)
        else:
            r = 1
            B.append(1)
    else:
        if s[i] == s[i-1]:
            if r == 0:
                A[-1] += 1
            else:
                B[-1] += 1
        else:
            if r == 0:
                B.append(1)
            else:
                A.append(1)
            r = 1-r

C = []  
D = []  
if s[0] == 'F':
    x -= A[0]
    del A[0]
r = 0  
for i in range(len(A)):
    if (r + B[i]) % 2 == 0:
        C.append(A[i])
        r = 0
    else:
        D.append(A[i])
        r = 1




if (x+sum(C)) % 2 != 0:
    print('No')
    exit()
x = (x+sum(C)) // 2

if (y+sum(D)) % 2 != 0:
    print('No')
    exit()
y = (y+sum(D)) // 2

if x < 0 or y < 0:
    print('No')
    exit()




C = [0] + C
from itertools import accumulate
E = list(accumulate(C))
e = 0
if x == 0:
    e = 1
for i in range(1,len(C)):
    for j in range(i,len(C)):
        if E[j] - E[i-1] == x:
            e = 1
if e == 0:
    print('No')
    exit()

D = [0] + D
from itertools import accumulate
F = list(accumulate(D))
f = 0
if y == 0:
    f = 1
for i in range(1,len(D)):
    for j in range(i,len(D)):
        if F[j] - F[i-1] == y:
            f = 1
if f == 0:
    print('No')
else:
    print('Yes')

Solution (Python):
from copy import copy
Ss = input() + "T"
X, Y = map(int, input().split())

d = [len(s) for s in Ss.split("T")]
x = d[0]

d_x = d[2::2]
d_y = d[1::2]

xs = set([x])
ys = set([0])
blx = [X in xs]
for d in d_x:
    
    xs_ = set()
    for x in xs:
        xs_.add(x+d)
        xs_.add(x-d)
    blx.append(X in xs_)
    xs = copy(xs_)

bly = []
for d in d_y:
    ys_ = set()
    for y in ys:
        ys_.add(y+d)
        ys_.add(y-d)
    bly.append(Y in ys_)
    ys = copy(ys_)

if blx[-1] and bly[-1]: print("Yes")
else: print("No")

Solution (Python):
s = input()
n = len(s)
x, y = map(int, input().split())
for i in range(n):
    if s[i]=='T':
        break
else:
    i += 1
dp = {i}
xs = []
ys = []
now = 0
xy = 'y'
for j in range(i+1, n+1):
    if j<n and s[j]=='F':
        now += 1
        continue
    if xy=='x':
        if now>0:
            xs.append(now)
        xy = 'y'
    else:
        if now>0:
            ys.append(now)
        xy = 'x'
    now = 0

for i in xs:
    newdp = set()
    for j in dp:
        newdp.add(j+i)
        newdp.add(j-i)
    dp = newdp
if x not in dp:
    print('No')
    exit(0)
dp = {0}
for i in ys:
    newdp = set()
    for j in dp:
        newdp.add(j+i)
        newdp.add(j-i)
    dp = newdp
if y in dp:
    print('Yes')
else:
    print('No')

Solution (Python):
import copy

s = input()
target_x, target_y = map(int, input().split())

fl = s.split('T')
f_cnts = [ len(v) for v in fl ]

target_x -= f_cnts[0]
f_cnts = f_cnts[1:]

last_xs = set()
last_ys = set()
last_xs.add(0)
last_ys.add(0)
for i, f_cnt in enumerate(f_cnts):
    if i%2 == 0:
        next_y = f_cnts[i]
        next_last_ys = set()
        for y in list(last_ys):
            next_last_ys.add(y+next_y)
            next_last_ys.add(abs(y-next_y))
        last_ys = copy.deepcopy(next_last_ys)
    else:
        next_x = f_cnts[i]
        next_last_xs = set()
        for x in list(last_xs):
            next_last_xs.add(x+next_x)
            next_last_xs.add(abs(x-next_x))
        last_xs = copy.deepcopy(next_last_xs)


if (target_x in last_xs or -1*target_x in last_xs) and (target_y in last_ys or -1*target_y in last_ys):
    print('Yes')
else:
    print('No')

Solution (Python):
s = input()
l = len(s)
X, Y = (int(x) for x in input().split())
F = s.split("T")
x_move = [f.count("F") for f in F[2::2]]
y_move = [f.count("F") for f in F[1::2]]
x_set = set()
x_set.add(F[0].count("F"))
y_set = set()
y_set.add(0)
for move in x_move:
    x_set = set(x + move for x in x_set) | set(x - move for x in x_set)
for move in y_move:
    y_set = set(y + move for y in y_set) | set(y - move for y in y_set)

if X in x_set and Y in y_set:
    print("Yes")
else:
    print("No")

Solution (Python):
import numpy as np
s = input()
x, y = map(int, input().split())

i = 0
stock = 0
sl = []
while i < len(s):
    if s[i] == 'F':
        stock+=1
        i+=1
    else:
        sl.append(stock)
        stock = 0
        i+=1
else:
    sl.append(stock)

x-=sl[0]
ymove = sl[1::2]
xmove = sl[2::2]

dpx = np.zeros(16002).astype(int)
dpx[8000] = 1
for i in range(len(xmove)):
    idx = np.where(dpx==1)[0]
    ndpx = np.zeros(16002)
    ndpx[idx+xmove[i]] = True
    ndpx[idx-xmove[i]] = True
    dpx = ndpx

dpy= np.zeros(16002).astype(int)
dpy[8000] = 1
for i in range(len(ymove)):
    idx = np.where(dpy==1)[0]
    ndpy = np.zeros(16002)
    ndpy[idx+ymove[i]] = True
    ndpy[idx-ymove[i]] = True
    dpy = ndpy

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

Solution (Python):
s=input()
x,y=map(int,input().split())
t=s.split("T")
x-=len(t[0])
dx,dy=[],[]

for i,tt in enumerate(t[1:]):
  if i%2==1:
    dx.append(len(tt))
  else:
    dy.append(len(tt))

def is_ok(n,l):
  dp=set([0])
  for d in l:
    t=[]
    for v in dp:
      t.append(v+d)
      t.append(v-d)
    dp=set(t)
  return True if n in dp else False

if is_ok(x,dx) and is_ok(y,dy):
  print("Yes")
else:
  print("No")

Solution (Python):




import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial,sqrt
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

s=input().rstrip().split("T")
x,y=MI()

lisx=[]
lisy=[]
x-=len(s[0])
for i in range(1,len(s)):
    if i%2==0:
        lisx.append(len(s[i]))
    else:
        lisy.append(len(s[i]))
resx=[[] for i in range(len(lisx)+1)]
resy=[[] for i in range(len(lisy)+1)]
resx[0]=[0]
resy[0]=[0]

for i in range(len(lisx)):
    for j in range(len(resx[i])):
        resx[i+1].append(resx[i][j]+lisx[i])
        resx[i+1].append(resx[i][j]-lisx[i])
    resx[i+1]=list(set(resx[i+1]))

for i in range(len(lisy)):
    for j in range(len(resy[i])):
        resy[i+1].append(resy[i][j]+lisy[i])
        resy[i+1].append(resy[i][j]-lisy[i])
    resy[i+1]=list(set(resy[i+1]))


if x in resx[-1] and y in resy[-1]:
    print("Yes")
else:
    print("No")

Solution (Python):
def main():

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

    n = len(s)

    dir = 1
    dx = []
    dy = []
    cnt = 0
    for i in range(n):
        if s[i] == 'F':
            cnt += 1
        else:
            if dir == 1:
                dx.append(cnt)
                cnt = 0
                dir = -1
            else:
                dy.append(cnt)
                cnt = 0
                dir = 1
    else:
        if dir == 1:
            dx.append(cnt)
        else:
            dy.append(cnt)
    
    
    D = 8000*2+1

    import numpy as np
    dpx = [0]*(len(dx)+1)
    dpy = [0]*(len(dy)+1)
    dpx[0] = np.zeros(D, np.bool)
    dpy[0] = np.zeros(D, np.bool)
    dpx[0][8000] = 1
    dpy[0][8000] = 1
    for i, d in enumerate(dx):
        if i == 0:
            b = np.zeros(D, np.bool)
            b[d:] |= dpx[i][:D-d]
            dpx[i+1] = b
        else:
            b = np.zeros(D, np.bool)
            b[d:] |= dpx[i][:D-d]
            b[:D-d] |= dpx[i][d:]
            dpx[i+1] = b

    for i, d in enumerate(dy):
        b = np.zeros(D, np.bool)
        b[d:] |= dpy[i][:D-d]
        b[:D-d] |= dpy[i][d:]
        dpy[i+1] = b

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

if __name__ == '__main__':
    main()

Solution (Python):

import sys

YES = "Yes"  
NO = "No"  


def _solve(s, n, f):
    m = sum(f)
    r = range(n-m-1, n+m+1)
    cur = set([s])
    for ff in f:
        nex = set()
        for c in cur:
            for op in (1, -1):
                t = c + op * ff
                if t not in r:
                    continue
                nex.add(t)
        cur = nex
    return n in cur

def solve(s: str, x: int, y: int):
    f = tuple(map(lambda x: len(x), s.split("T")))
    return YES if _solve(f[0], x, f[2::2]) and _solve(0, y, f[1::2]) else NO


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    s = next(tokens)  
    x = int(next(tokens))  
    y = int(next(tokens))  
    print(solve(s, x, y))

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    
    main()

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

f = s.find('T')
if f >= 0:
    x = abs(x - f)
    s = s[f:]
else:
    if x == len(s) and y == 0:
        print('Yes')
    else:
        print('No')
    exit()

lx, ly = [], []
flag = True
n = 0
s += 'T'
for i in s:
    if i == 'F':
        n += 1
    else:
        if n > 0:
            if flag:
                lx.append(n)
            else:
                ly.append(n)
            n = 0
        flag = not flag

lx.sort()
ly.sort()
sx = sum(lx)
sy = sum(ly)

if sx < x or (sx - x) % 2 == 1 or sy < y or (sy - y) % 2 == 1:
    print('No')
    exit()


def main(x, sx, lx):
    if x == sx:
        return True

    goal = (sx - x) // 2
    
    dp = [False] * (goal + 1)
    dp[0] = True
    for i in lx:
        if i > goal:
            return False
        for j in range(goal - i, -1, -1):
            if dp[j]:
                dp[j+i] = True
        if dp[goal]:
            return True
    return False


if main(x, sx, lx) and main(y, sy, ly):
    print('Yes')
else:
    print('No')

Solution (Python):
s=input()+"T"
x,y=map(int,input().split())
n=len(s)
prev=-1
cnt=0
x_,y_=[],[]
for i in range(n):
    if s[i]=="T":
        if cnt%2:
            y_.append(i-prev-1)
        else:
            x_.append(i-prev-1)
        cnt+=1
        prev=i
prevx=[0]*(2*n+1)
prevy=[0]*(2*n+1)
prevx[n+x_[0]]=1
prevy[n]=1
x_=x_[1:]
for num in x_:
    curx=[0]*(2*n+1)
    for i in range(2*n+1):
        if prevx[i]:
            if i-num>=0:
                curx[i-num]=1
            if i+num<=2*n:
                curx[i+num]=1
    prevx=curx
for num in y_:
    cury=[0]*(2*n+1)
    for i in range(2*n+1):
        if prevy[i]:
            if i-num>=0:
                cury[i-num]=1
            if i+num<=2*n:
                cury[i+num]=1
    prevy=cury
if prevy[y+n] and prevx[x+n]:
    print("Yes")
else:
    print("No")

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7






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

dpx = {0}
dpy = {0}

f = s.split("T")

fx = []
fy = []

for i, fi in enumerate(f):
    if i % 2:
        fy.append(len(fi))
    else:
        fx.append(len(fi))

for i, fxi in enumerate(fx):
    nex = set([])
    for j in dpx:
        nex.add(j+fxi)
        if i > 0:
            nex.add(j-fxi)
    dpx = nex

for fyi in fy:
    nex = set([])
    for j in dpy:
        nex.add(j+fyi)
        nex.add(j-fyi)
    dpy = nex

if x in dpx and y in dpy:
    print('Yes')
else:
    print('No')

Solution (Python):
s = input()
x, y = map(int, input().split())
d = [len(i) for i in s.split('T')]
 
s1 = d[2::2]
s2 = d[1::2]
 
now_x = [0]
now_y = [0]

x -= d[0]

for i in s1:
    nx = set()
    for j in now_x:
        nx.add(j-i)
        nx.add(j+i)
    now_x = nx

for i in s2:
    ny = set()
    for j in now_y:
        ny.add(j-i)
        ny.add(j+i)
    now_y = ny

if x in now_x and y in now_y:
    print('Yes')
else:
    print('No')

Solution (Python):


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

s=input().rstrip().split("T")
x,y=MI()
    
x0=len(s[0])
x=x-x0
X=[]
Y=[]
for i in range(1,len(s)):
    if i%2==1:
        Y.append(len(s[i]))
    else:
        X.append(len(s[i]))



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

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


if sum(X)+x<0 or sum(X)+x>=len(dpx[-1]) or sum(Y)+y<0 or sum(Y)+y>=len(dpy[-1]):
    print("No")
    sys.exit()
if dpx[-1][sum(X)+x]==1 and dpy[-1][sum(Y)+y]==1:
    print("Yes")
else:
    print("No")

Solution (Python):
from sys import setrecursionlimit

setrecursionlimit(10 ** 6)

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

stepx = []
stepy = []

headx = True
step = 0
for c in s:
    if c == "F":
        step += 1
    else:
        if headx:
            stepx.append(step)
        else:
            stepy.append(step)
        headx = not headx
        step = 0

x -= stepx[0]
stepx = stepx[1:]


class Reachable:
    def __init__(self, steps):
        self.steps = steps

        self.maxs = sum(steps)
        self.r = [{} for _ in range(len(steps))]

    def __call__(self, i, s):
        if i == -1:
            return s == 0
        elif not -self.maxs <= s <= self.maxs:
            return False
        elif self.r[i].get(s, None) is not None:
            return self.r[i][s]
        else:
            step = self.steps[i]
            r = self(i - 1, s - step) or self(i - 1, s + step)
            self.r[i][s] = r
            return r


answer = Reachable(stepx)(len(stepx) - 1, x) and Reachable(stepy)(len(stepy) - 1, y)

if answer:
    print("Yes")
else:
    print("No")

Solution (Python):
import sys
readline = sys.stdin.readline

S = readline().rstrip()
X,Y = map(int,readline().split())


C = [] 
import itertools
for key,g in itertools.groupby(S):
  C += [[key,len(list(g))]]

x = 0
y = 0
ind = 0
if C[0][0] == "F": 
  x += C[0][1]
  ind += 1


dirX = True

Xset = {x}
Yset = {y}

for i in range(ind, len(C)):
  command, num = C[i]
  if command == "F":
    if dirX:
      next_Xset = set()
      for x in Xset:
        next_Xset.add(x + num)
        next_Xset.add(x - num)
      Xset = next_Xset
    else:
      next_Yset = set()
      for y in Yset:
        next_Yset.add(y + num)
        next_Yset.add(y - num)
      Yset = next_Yset
  else:
    if num % 2 == 1:
      dirX ^= True

if X in Xset and Y in Yset:
  print("Yes")
else:
  print("No")

Solution (Python):
def Xok():
    s=set([xp])
    for i in X:
        tmp=set()
        for c in s:
            tmp.add(c+i)
            tmp.add(c-i)
        s=tmp
    return True if x in s else False
def Yok():
    s=set([0])
    for i in Y:
        tmp=set()
        for c in s:
            tmp.add(c+i)
            tmp.add(c-i)
        s=tmp
    return True if y in s else False

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

xp,X,Y=0,[],[]

i=0
while(i<N and s[i]!='T'):
    xp+=1
    i+=1
i+=1
flag=True
while(i<N):
    j=i
    cnt=0
    while(j<N and s[j]!='T'):
        cnt+=1
        j+=1
    if flag:
        Y.append(cnt)
    else:
        X.append(cnt)
    i=j+1
    flag=not flag

print('Yes' if Xok() and Yok() else 'No')

Solution (Python):

from itertools import groupby
import sys
def main(s, x, y):
    g = [x, y]
    o = [[], []]

    direction = count = 0
    first = True
    for c in s:
        if c == 'T':
            if first:
                g[0] -= count
                first = False
            elif count > 0:
                o[direction].append(count)
            count = 0
            direction ^= 1
        else:
            count += 1
    if first:
        g[0] -= count
    elif count > 0:
        o[direction].append(count)

    for d in range(2):
        l = o[d]
        M = sum(l)
        M2 = M * 2
        if abs(g[d]) > M:
            print('No')
            return
        pre = [False] * (M2 + 1)
        pre[M] = True
        for i, v in enumerate(l):
            dp = [False] * (M2 + 1)
            for a in range(M2 + 1):
                if pre[a] == False: continue
                if a >= v:
                    dp[a - v] = True
                if a + v <= M2:
                    dp[a + v] = True
            pre = dp
        if pre[g[d] + M] == False:
            print('No')
            return
    print('Yes')

if __name__ == '__main__':
    input = sys.stdin.readline
    s = input().rstrip()
    x, y = map(int, input().split())
    main(s, x, y)

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache





MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum


def gcd(a, b):
    while b: a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





S = S()
gx,gy = IL()

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

if len(data)%2 == 0:
  data.pop()
data = data[::-1]
shote = data.pop()
gx -= shote
D = [[0],[0]]
f = 0


while data:
  f ^= data.pop()
  D[f].append(data.pop())
SUMX = sum(D[0])
SUMY = sum(D[1])
dpx =  [[0 for i in range(SUMX*2+1)] for j in range(len(D[0]))]
dpy =  [[0 for i in range(SUMY*2+1)] for j in range(len(D[1]))]
dpx[0][SUMX] = 1
dpy[0][SUMY] = 1
for j in range(1,len(D[0])):
  d = D[0][j]
  for i in range(SUMX*2+1):
    if i+d < SUMX*2+1:
      if dpx[j-1][i+d]:
        dpx[j][i] = 1
    if 0 <= i-d:
      if dpx[j-1][i-d]:
        dpx[j][i] = 1
for j in range(1,len(D[1])):
  d = D[1][j]
  for i in range(SUMY*2+1):
    if i+d < SUMY*2+1:
      if dpy[j-1][i+d]:
        dpy[j][i] = 1
    if 0 <= i-d:
      if dpy[j-1][i-d]:
        dpy[j][i] = 1
if (0<= SUMX+gx < SUMX*2+1) and (0<= SUMY+gy < SUMY*2+1):
  if dpx[-1][SUMX+gx] and dpy[-1][SUMY+gy]: 
    print("Yes")
    exit()
print("No")

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


from collections import deque
sumS = deque()
import itertools
for key,g in itertools.groupby(S):
  sumS.append([key,len(list(g))])
  
startx = 0 

if sumS[0][0] == "F":
  startx = sumS[0][1]
  sumS.popleft()

xdir = True 
dpXcur = set()
dpXcur.add(startx)
dpYcur = set()
dpYcur.add(0) 
dpXnex = set()
dpYnex = set()
while sumS:
  mark, steps = sumS.popleft()
  if mark == "F":
    if xdir: 
      for x in dpXcur:
        dpXnex.add(x + steps)
        dpXnex.add(x - steps)
      dpXcur = dpXnex
      dpXnex = set()
    else:
      for y in dpYcur:
        dpYnex.add(y + steps)
        dpYnex.add(y - steps)
      dpYcur = dpYnex
      dpYnex = set()
  elif mark == "T":
    if steps % 2 == 1:
      
      xdir ^= True
      
if X in dpXcur and Y in dpYcur:
  print("Yes")
else:
  print("No")

Solution (Python):
import sys
readline = sys.stdin.readline

S = readline().rstrip()
gx,gy = map(int,readline().split())

C = []

import itertools
for key,g in itertools.groupby(S):
  C.append([key,len(list(g))])
  
x,y = 0,0
ind = 0
if C[0][0] == "F":
  x += C[0][1]
  ind = 1


dirX = True
now_x = {x}
now_y = {y}
for i in range(ind,len(C)):
  if C[i][0] == "F":
    step = C[i][1]
    if dirX:
      nex_x = set()
      for n in now_x:
        nex_x.add(n + step)
        nex_x.add(n - step)
      now_x = nex_x
    else:
      nex_y = set()
      for n in now_y:
        nex_y.add(n + step)
        nex_y.add(n - step)
      now_y = nex_y
  else:
    if C[i][1] % 2 == 1:
      dirX ^= True
    
if gx in now_x and gy in now_y:
  print("Yes")
else:
  print("No")