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
import re
s = input()
x, y = [int(i) for i in sys.stdin.readline().split()]
s = [len(i) for i in re.split("T", s)]
v_ls = s[1::2]
h_ls = s[::2]
x -= h_ls[0]
h_ls.pop(0)
v_sum = sum(v_ls)
h_sum = sum(h_ls)

width = max(abs(y), v_sum)
memo_v_ls = [[0 for i in range(width * 2 + 1)] for j in range(len(v_ls) + 1)]
memo_v_ls[0][width] = 1
for j, v in enumerate(v_ls, 1):
    for i in range(width * 2 + 1):
        if i >= v and i <= 2 * width - v:
            memo_v_ls[j][i] = max(memo_v_ls[j - 1][i - v], memo_v_ls[j - 1][i + v])
        elif i >= v:
            memo_v_ls[j][i] = memo_v_ls[j - 1][i - v]
        else:
            memo_v_ls[j][i] = memo_v_ls[j - 1][i + v]

flg = memo_v_ls[-1][y + width] == 1

width = max(abs(x), h_sum)
memo_h_ls = [[0 for i in range(width * 2 + 1)] for j in range(len(h_ls) + 1)]
memo_h_ls[0][width] = 1
for j, h in enumerate(h_ls, 1):
    for i in range(width * 2 + 1):
        if i >= h and i <= 2 * width - h:
            memo_h_ls[j][i] = max(memo_h_ls[j - 1][i - h], memo_h_ls[j - 1][i + h])
        elif i >= h:
            memo_h_ls[j][i] = memo_h_ls[j - 1][i - h]
        else:
            memo_h_ls[j][i] = memo_h_ls[j - 1][i + h]

flg &= memo_h_ls[-1][x + width] == 1
if flg:
    print("Yes")
else:
    print("No")

Solution (Python):
sl = list(input())
x,y = map(int,input().split())
lrl = [[],[]]
mode = 0
stack = 0
flag = True
first_t = 0
for s in sl:
    if s == "F":
        stack += 1
    else:
        if flag:
            first_t = stack
            flag = False
        elif stack > 0:
            lrl[mode%2].append(stack)
        mode += 1
        stack = 0
else:
    if flag:
        first_t = stack
        flag = False
    elif stack > 0:
        lrl[mode%2].append(stack)

x = abs(x - first_t)
y = abs(y)
from collections import defaultdict
l = defaultdict(int)
r = defaultdict(int)
l[0] = 1
r[0] = 1
ol = x + sum(lrl[0])
for a in lrl[0]:
    kouho = []
    for ll in l:
        if ll + 2 * a <= ol:
            kouho.append(ll+2*a)
    for i in kouho:
        l[i] = 1

orr = y + sum(lrl[1])
for a in lrl[1]:
    kouho = []
    for rr in r:
        if rr + 2 * a <= orr:
            kouho.append(rr+2*a)
    for i in kouho:
        r[i] = 1


if l[ol] == 1 and r[orr] == 1:
    print("Yes")
    import sys
    sys.exit()

print("No")

Solution (Python):
s=[len(c) for c in list(input().split("T"))]
x,y=map(int,input().split())
XX=s[::2]
YY=s[1::2]
X=set([XX[0]])
Y=set([0])
XXX=[]
for i in range(1,len(XX)):
    for j in X:
        XXX.append(j+XX[i])
        XXX.append(j-XX[i])
    X=set(XXX)
    XXX=[]

YYY=[]
for i in range(len(YY)):
    for j in Y:
        YYY.append(j+YY[i])
        YYY.append(j-YY[i])
    Y=set(YYY)
    YYY=[]

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

Solution (Python):

import sys

YES = "Yes"  
NO = "No"  

def solve(s: str, x: int, y: int):
    s_list = list(map(len,s.split("T")))
    x_s_list = s_list[0::2]
    y_s_list = s_list[1::2]

    x_dp = [set() for _ in range(len(x_s_list))]
    for index,count in enumerate(x_s_list):
        
        if index == 0:
            x_dp[index].add(count)
            continue
        
        for j in x_dp[index-1]:
            x_dp[index].add(j-count)
            x_dp[index].add(j+count)


    y_dp = [set() for _ in range(len(y_s_list))]
    for index,count in enumerate(y_s_list):
        
        if index == 0:
            y_dp[index].add(count)
            y_dp[index].add(-count)  
            continue
        
        for j in y_dp[index-1]:
            y_dp[index].add(j-count)
            y_dp[index].add(j+count)
    
    if not y_dp:
        if y != 0:
            print(NO)
        else:
            if x in x_dp[-1]:
                print(YES)
            else:
                print(NO)
        return

    if not x_dp:
        if x != 0:
            print(NO)
        else:
            if y in y_dp[-1]:
                print(YES)
            else:
                print(NO)
        return

    if x in x_dp[-1] and y in y_dp[-1]:
        print(YES)
    else:
        print(NO)

    return

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))  
    solve(s, x, y)

if __name__ == '__main__':
    main()

Solution (Python):
s = [len(x) for x in input().split('T')]
x, y = map(int, input().split())

X = set([s[0]])
Y = set([0])

for dx in s[2::2]:
    tX = set()
    for Xi in X:
        tX.add(Xi-dx)
        tX.add(Xi+dx)
    X = tX

for dy in s[1::2]:
    tY = set()
    for Yi in Y:
        tY.add(Yi-dy)
        tY.add(Yi+dy)
    Y = tY

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

Solution (Python):
s = input()
x, y = list(map(int, input().split()))
F = [len(fs) for fs in s.split('T')]
x -= F[0]
Fx = F[2::2]
Fy = F[1::2]

xt2 = sum(Fx)-x
yt2 = sum(Fy)-y
if xt2%2==1 or yt2%2==1 or xt2<0 or yt2<0:
    print('No')
    exit()

xt = xt2//2
yt = yt2//2

dpx = [False]*(xt+1)
dpx[0] = True
for f in Fx:
    for i in range(xt, f-1, -1): dpx[i] |= dpx[i-f]

dpy = [False]*(yt+1)
dpy[0] = True
for f in Fy:
    for i in range(yt, f-1, -1): dpy[i] |= dpy[i-f]

print('Yes' if dpx[xt] and dpy[yt] else 'No')

Solution (Python):

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

    lf = [0]
    tf = 0
    for i in range(len(s)):
        if s[i] =="F":
            lf[tf] += 1
        else:
            lf.append(0)
            tf += 1

    lx = [lf[i] for i in range(len(lf)) if i % 2 == 0]
    ly = [lf[i] for i in range(len(lf)) if i % 2 == 1]
    dpxcenter = sum(lx)
    if abs(x)>dpxcenter: return "No"
    dpxedge = dpxcenter*2+20
    dpx = [[False for _ in range(dpxedge)] for _ in range(len(lx)+1)]
    dpx[1][dpxcenter+lx[0]+10] = True
    for i in range(1,len(lx)):
        for j in range(dpxedge):
            if j-lx[i] >= 0:
                dpx[i+1][j] = dpx[i][j-lx[i]]
            if j+lx[i] < dpxedge:
                dpx[i+1][j] = dpx[i+1][j] or dpx[i][j+lx[i]]

    dpycenter = sum(ly)
    if abs(y)>dpycenter: return "No"
    dpyedge = dpycenter*2+20
    dpy = [[False for _ in range(dpyedge)] for _ in range(len(ly)+1)]
    dpy[0][dpycenter+10] = True
    for i in range(len(ly)):
        for j in range(dpyedge):
            if j-ly[i] >= 0:
                dpy[i+1][j] = dpy[i][j-ly[i]]
            if j+ly[i] < dpyedge:
                dpy[i+1][j] = dpy[i+1][j] or dpy[i][j+ly[i]]

    if dpx[-1][x+dpxcenter+10] and dpy[-1][y+dpycenter+10]:return "Yes"
    else:return "No"

print(main())

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()

from itertools import groupby
def resolve():
    S=input()
    X=[]; Y=[]
    dir=0
    for key,iter in groupby(S):
        h=len(list(iter))
        if(key=='T'): dir^=(h&1)
        else:
            if(dir&1==0): X.append(h)
            else: Y.append(h)

    
    tx,ty=map(int,input().split())
    if(S[0]=='F'):
        tx-=X[0]
        del X[0]

    
    M=8100*2
    dpx=[0]*M; dpx[0]=1
    dpy=[0]*M; dpy[0]=1

    for x in X:
        ndpx=[0]*M
        for i in range(M):
            if(dpx[i]==0): continue
            ndpx[(i+x)%M]=1
            ndpx[(i-x)%M]=1
        dpx=ndpx

    for y in Y:
        ndpy=[0]*M
        for i in range(M):
            if(dpy[i]==0): continue
            ndpy[(i+y)%M]=1
            ndpy[(i-y)%M]=1
        dpy=ndpy

    print("Yes" if(dpx[tx%M] and dpy[ty%M]) else "No")
resolve()

Solution (Python):
def examD():
    St = S(); X, Y = LI()
    N = len(St)
    ans = "No"
    Fx = deque(); Fy = deque()
    ve = int(1); cur = int(0)
    for i in range(N):
        if St[i] == "F":
            cur += 1
        else:
            if ve == 1:
                Fx.append(cur)
                cur = int(0)
                ve *= -1
            else:
                Fy.append(cur)
                cur = int(0)
                ve *= -1
    if ve == 1:
        Fx.append(cur)
    else:
        Fy.append(cur)
    xfirst = Fx.popleft()
    X = X - xfirst


    xlen = len(Fx); ylen = len(Fy)
    xneed = -1; yneed = -1
    if (sum(Fx) - X) % 2 == 0:
        xneed = (sum(Fx) - X) // 2
    if (sum(Fy) - Y) % 2 == 0:
        yneed = (sum(Fy) - Y) // 2

    dp = [[0] * (xneed + 1) for _ in range(xlen + 1)]
    if xneed > 0:
        dp[0][0] = 1
        for i in range(xlen):
            for j in range(max(xneed + 1, 0)):
                if dp[i][j] != 0:
                    if xneed + 1 - Fx[i] > j:
                        dp[i + 1][j + Fx[i]] = dp[i][j]
                    dp[i + 1][j] = dp[i][j]
    
    
    if (xneed > 0 and dp[xlen][xneed] == 1) or xneed == 0:
        dp = [[0] * (yneed + 1) for _ in range(ylen + 1)]
        if yneed>0:
            dp[0][0] = 1
            for i in range(ylen):
                for j in range(max(yneed + 1, 0)):
                    if dp[i][j] != 0:
                        if yneed + 1 - Fy[i] > j:
                            dp[i + 1][j + Fy[i]] = dp[i][j]
                        dp[i + 1][j] = dp[i][j]
        if (yneed > 0 and dp[ylen][yneed] == 1) or yneed == 0:
            ans = "Yes"
    print(ans)

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()

Solution (Python):
from collections import defaultdict

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

arr = list(map(len, s.split('T')))
EW = [arr[ew] for ew in range(0, len(arr), 2)]
NS = [arr[ns] for ns in range(1, len(arr), 2)]


dp = [defaultdict(bool) for i in range(len(EW))]
dp[0][EW[0]] = True
for i in range(1, len(EW)):
    for prev in dp[i - 1].keys():
        dp[i][prev + EW[i]] = True
        dp[i][prev - EW[i]] = True
if not dp[-1][x]:
    print('No')
    exit(0)

if len(NS) > 0:
    dp = [defaultdict(bool) for i in range(len(NS))]
    dp[0][NS[0]] = True
    dp[0][-NS[0]] = True
    for i in range(1, len(NS)):
        for prev in dp[i - 1].keys():
            dp[i][prev + NS[i]] = True
            dp[i][prev - NS[i]] = True
    if not dp[-1][y]:
        print('No')
        exit(0)

print('Yes')

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

X = [0]
Y = [0]

X_append = X.append
Y_append = Y.append

flag = True
tmp = 0
for i in s:
    if i == 'F':
        tmp += 1
    else:
        if flag:
            X_append(tmp)
        else:
            Y_append(tmp)
        tmp = 0
        flag = not flag

if flag:
    X_append(tmp)
else:
    Y_append(tmp)

x -= X[1]
X[1] = 0

x = abs(x)
total_X = sum(X)

lst_X = np.zeros(total_X + 1, dtype = np.int32)
lst_X[0] = 1
for i in X:
    if i == 0:
        continue
    lst_X[i:] = np.maximum(lst_X[i:], lst_X[:-i])

if x > total_X or (total_X - x) % 2 == 1 or lst_X[(total_X - x) // 2] == 0:
    print ('No')
    exit()

y = abs(y)
total_Y = sum(Y)

lst_Y = np.zeros(total_Y + 1)
lst_Y[0] = 1
for i in Y:
    if i == 0:
        continue
    lst_Y[i:] = np.maximum(lst_Y[i:], lst_Y[:-i])

if  y <= total_Y and (total_Y - y) % 2 == 0 and int(lst_Y[(total_Y - y) // 2]) == 1:
    print ('Yes')
else:
    print ('No')

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

X = [0]
Y = [0]

X_append = X.append
Y_append = Y.append

flag = True
tmp = 0
for i in s:
    if i == 'F':
        tmp += 1
    else:
        if flag:
            X_append(tmp)
        else:
            Y_append(tmp)
        tmp = 0
        flag = not flag

if flag:
    X_append(tmp)
else:
    Y_append(tmp)

x -= X[1]
X[1] = 0

x = abs(x)
total_X = sum(X)

lst_X = np.zeros(total_X + 1, dtype = np.int32)
lst_X[0] = 1
for i in X:
    if i == 0:
        continue
    lst_X[i:] = np.maximum(lst_X[i:], lst_X[:-i])

if x > total_X or (total_X - x) % 2 == 1 or lst_X[(total_X - x) // 2] == 0:
    print ('No')
    exit()

y = abs(y)
total_Y = sum(Y)

lst_Y = np.zeros(total_Y + 1)
lst_Y[0] = 1
for i in Y:
    if i == 0:
        continue
    lst_Y[i:] = np.maximum(lst_Y[i:], lst_Y[:-i])

if x > total_X or y > total_Y:
    print ('No')
    exit()

if  (total_Y - y) % 2 == 0 and int(lst_Y[(total_Y - y) // 2]) == 1:
    print ('Yes')
else:
    print ('No')

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

X = [0]
Y = [0]

X_append = X.append
Y_append = Y.append

flag = True
tmp = 0
for i in s:
    if i == 'F':
        tmp += 1
    else:
        if flag:
            X_append(tmp)
        else:
            Y_append(tmp)
        tmp = 0
        flag = not flag

if flag:
    X_append(tmp)
else:
    Y_append(tmp)




x -= X[1]
X[1] = 0

x = abs(x)
y = abs(y)

total_X = sum(X)
total_Y = sum(Y)

lst_X = np.zeros(total_X + 1, dtype = np.int32)
lst_X[0] = 1
for i in X:
    if i == 0:
        continue
    
    
    
    

    lst_X[i:] = np.maximum(lst_X[i:], lst_X[:-i])



lst_Y = np.zeros(total_Y + 1)
lst_Y[0] = 1
for i in Y:
    if i == 0:
        continue
    lst_Y[i:] = np.maximum(lst_Y[i:], lst_Y[:-i])

if x > total_X or y > total_Y:
    print ('No')
    exit()

if (total_X - x) % 2 == (total_Y - y) % 2 == 0 and int(lst_X[(total_X - x) // 2]) == 1 and int(lst_Y[(total_Y - y) // 2]) == 1:
    print ('Yes')
else:
    print ('No')

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

X = [0]
Y = [0]

flag = True
tmp = 0
for i in s:
    if i == 'F':
        tmp += 1
    else:
        if flag:
            X.append(tmp)
        else:
            Y.append(tmp)
        tmp = 0
        flag = not flag

if flag:
    X.append(tmp)
else:
    Y.append(tmp)




x -= X[1]
X[1] = 0

x = abs(x)
y = abs(y)

total_X = sum(X)
total_Y = sum(Y)

lst_X = np.zeros(total_X + 1, dtype = np.int32)
lst_X[0] = 1
for i in X:
    if i == 0:
        continue
    
    
    
    

    lst_X[i:] = np.maximum(lst_X[i:], lst_X[:-i])



lst_Y = np.zeros(total_Y + 1)
lst_Y[0] = 1
for i in Y:
    if i == 0:
        continue
    lst_Y[i:] = np.maximum(lst_Y[i:], lst_Y[:-i])

if x > total_X or y > total_Y:
    print ('No')
    exit()

if (total_X - x) % 2 == (total_Y - y) % 2 == 0 and int(lst_X[(total_X - x) // 2]) == 1 and int(lst_Y[(total_Y - y) // 2]) == 1:
    print ('Yes')
else:
    print ('No')

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

X = [0]
Y = [0]

flag = True
tmp = 0
for i in s:
    if i == 'F':
        tmp += 1
    else:
        if flag:
            X.append(tmp)
        else:
            Y.append(tmp)
        tmp = 0
        flag = not flag

if flag:
    X.append(tmp)
else:
    Y.append(tmp)




x -= X[1]
X[1] = 0

x = abs(x)
y = abs(y)

total_X = sum(X)
total_Y = sum(Y)

lst_X = [0] * (total_X + 1)
lst_X[0] = 1
for i in X:
    if i == 0:
        continue
    for j in range(total_X - i, -1, -1):
        if lst_X[j] == 1:
            lst_X[j + i] = 1



lst_Y = [0] * (total_Y + 1)
lst_Y[0] = 1
for i in Y:
    if i == 0:
        continue
    for j in range(total_Y - i, -1, -1):
        if lst_Y[j] == 1:
            lst_Y[j + i] = 1


if x > total_X or y > total_Y:
    print ('No')
    exit()

if (total_X - x) % 2 == (total_Y - y) % 2 == 0 and lst_X[(total_X - x) // 2] == 1 and lst_Y[(total_Y - y) // 2]:
    print ('Yes')
else:
    print ('No')

Solution (Python):
def main():
    s = input().split('T')
    x, y = map(int, input().split())

    
    
    here_x = [len(s[0])]

    for i in s[2::2]:
        move = len(i)
        here_x = [i + move for i in here_x] + [i - move for i in here_x]
        here_x = set(here_x)

    
    here_y = [0]

    for i in s[1::2]:
        move = len(i)
        here_y = [i + move for i in here_y] + [i - move for i in here_y]
        here_y = set(here_y)

    if x in here_x and y in here_y:
        print('Yes')
    else:
        print('No')


if __name__ == '__main__':
    main()

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



here_x = [len(s[0])]

for i in s[2::2]:
    move = len(i)
    here_x = [i + move for i in here_x] + [i - move for i in here_x]
    here_x = list(set(here_x))



here_y = [0]

for i in s[1::2]:
    move = len(i)
    here_y = [i + move for i in here_y] + [i - move for i in here_y]
    here_y = list(set(here_y))

if x in here_x and y in here_y:
    print('Yes')
else:
    print('No')

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

s.append("last")
X = []
Y = []
f = "x"
cnt = 0
for i in range(len(s)):
    if s[i] == "F":
        cnt += 1
    elif f == "x":
        X.append(cnt)
        cnt = 0
        f = "y"
    else:
        Y.append(cnt)
        cnt = 0
        f = "x"

if X:
    x -= X[0]
    X.pop(0)

X.sort(reverse=True)
Y.sort(reverse=True)
for i in range(len(X)):
    if x >= 0:
        x -= X[i]
    else:
        x += X[i]
        
for i in range(len(Y)):
    if y >= 0:
        y -= Y[i]
    else:
        y += Y[i]
        
if x == 0 and y == 0:
    print("Yes")
else:
    print("No")

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



s = str(input())
x,y = II()

N = len(s)
yoko = []
tate = []
ziku = True
count = 0

for i in range(N):
    if s[i]=='F':
        count += 1
    else:
        if ziku:
            yoko.append(count)
            ziku = False
        else:
            tate.append(count)
            ziku = True
        count = 0

if count!=0:
    if ziku:
        yoko.append(count)
    else:
        tate.append(count)

yoko.append(0)
tate.append(0)

if s[0]=='F':
    fstart = True
else:
    fstart = False

sum1 = sum(yoko)
sum2 = sum(tate)

if abs(x)>sum1 or abs(y)>sum2:
    print('No')
    exit()

yoko_yes = [False]*(2*sum1+1)
yoko_yes[sum1] = True

for k,yk in enumerate(yoko):
    ok = []
    if k==0 and fstart:
        ok.append(sum1+yk)
    else:
        for i in range(2*sum1+1):
            if yoko_yes[i]:
                if 0<=i+yk<=2*sum1:
                    ok.append(i+yk)
                if 0<=i-yk<=2*sum1:
                    ok.append(i-yk)
    yoko_yes = [False]*(2*sum1+1)
    for o in ok:
        yoko_yes[o] = True

tate_yes = [False]*(2*sum2+1)
tate_yes[sum2] = True

for t in tate:
    ok = []
    for i in range(2*sum2+1):
        if tate_yes[i]:
            if 0<=i+t<=2*sum2:
                ok.append(i+t)
            if 0<=i-t<=2*sum2:
                ok.append(i-t)
    tate_yes = [False]*(2*sum2+1)
    for o in ok:
        tate_yes[o] = True

if yoko_yes[x+sum1] and tate_yes[y+sum2]:
    print('Yes')
else:
    print('No')

Solution (Python):





def solve(s, x, y):
    X = 0
    Y = 1

    def update(move):
        st = set()
        for p in reachable[st_ind]:
            st.add(p + move)
            st.add(abs(p - move))
        reachable[st_ind] = st
        

    n = len(s)
    spl = map(len, s.split('T'))
    x -= next(spl)
    
    

    reachable = [{0}, {0}]
    st_ind = 1
    for move in spl:
        update(move)
        st_ind ^= 1

    return (abs(x) in reachable[X]) and (abs(y) in reachable[Y])
    


if __name__ == '__main__':
    s = input()
    x, y = map(int, input().split())
    print('Yes' if solve(s, x, y) else 'No')

Solution (Python):
import sys
s=input()
n=len(s)
x,y=map(int,input().split())
lst=list(s.split("T"))
if len(lst)==1:
    if x==len(s) and y==0:
        print("Yes")
    else:
        print("No")
    sys.exit()
v=[0]*(2*n+1)
u=[0]*(2*n+1)
a=len(lst[0])
b=len(lst[1])
v[n+a]=1
u[n+b]=1
u[n-b]=1
for i in range(2,len(lst)):
    l=len(lst[i])
    if i%2==0:
        h=[0]*(2*n+1)
        for i in range(2*n+1):
            if i-l>=0:
                if v[i-l]==1:
                    h[i]=1
            if i+l<=2*n:
                if v[i+l]==1:
                    h[i]=1
        v=h
    else:
        h=[0]*(2*n+1)
        for i in range(2*n+1):
            if i-l>=0:
                if u[i-l]==1:
                    h[i]=1
            if i+l<=2*n:
                if u[i+l]==1:
                    h[i]=1
        u=h
if v[n+x]==1 and u[n+y]==1:
    print("Yes")
else:
    print("No")

Solution (Python):
s=input()
x,y=map(int,input().split())
xx,yy=[],[]
w=[0,0]
ans=chk=f=0
for i in s:
    if f==1 and i=='F':
        chk+=1
    elif f==1 and i=='T':
        yy.append(chk)
        f=0
    elif f==0 and i=='F':
        chk+=1
    elif f==0 and i=='T':
        xx.append(chk)
        f=1
    if i=='T': chk=0
if chk and f==1:
    yy.append(chk)
elif chk and f==0:
    xx.append(chk)
if s[0]=='F':
    w[0]+=xx[0]
    xx[0]=0
xx.sort()
yy.sort()
for i in xx[::-1]:
    if w[0]<x:
        w[0]+=i
    else:
        w[0]-=i
for i in yy[::-1]:
    if w[1]<y:
        w[1]+=i
    else:
        w[1]-=i
print("Yes" if w[0]==x and w[1]==y else "No")

Solution (Python):
s=input()
x,y=map(int,input().split())
xx,yy=[],[]
w=[0,0]
ans=chk=0
f=0
for i in s:
    if f==0 and i=='F':
        w[0]+=1
    elif f==0 and i=='T':
        f=1
    elif f==1 and i=='F':
        chk+=1
    elif f==1 and i=='T':
        yy.append(chk)
        chk=0
        f=2
    elif f==2 and i=='F':
        chk+=1
    elif f==2 and i=='T':
        xx.append(chk)
        chk=0
        f=1
if chk and f==1:
    yy.append(chk)
elif chk and f==2:
    xx.append(chk)
xx.sort()
yy.sort()
for i in xx[::-1]:
    if w[0]<x:
        w[0]+=i
    else:
        w[0]-=i
for i in yy[::-1]:
    if w[1]<y:
        w[1]+=i
    else:
        w[1]-=i
print("Yes" if w[0]==x and w[1]==y else "No")

Solution (Python):
s=input()
x,y=map(int,input().split())
xx,yy=[],[]
w=[0,0]
ans=chk=0
f=0
for i in s:
    if f==0 and i=='F':
        w[0]+=1
    elif f==0 and i=='T':
        f=1
    elif f==1 and i=='F':
        chk+=1
    elif f==1 and i=='T':
        yy.append(chk)
        chk=0
        f=2
    elif f==2 and i=='F':
        chk+=1
    elif f==2 and i=='T':
        xx.append(chk)
        chk=0
        f=1
if chk and f==1:
    yy.append(chk)
elif chk and f==2:
    xx.append(chk)
xx.sort()
yy.sort()
for i in xx[::-1]:
    if w[0]<=x:
        w[0]+=i
    else:
        w[0]-=i
for i in yy[::-1]:
    if w[1]<=y:
        w[1]+=i
    else:
        w[1]-=i
print("Yes" if w[0]==x and w[1]==y else "No")

Solution (Python):
s = input().split("T")
x,y = map(int,input().split())
x -= len(s[0])
XY = [[],[]]
for i in range(1,len(s)):
  XY[i%2].append(len(s[i]))
X,Y = XY
X.sort(reverse = True)
Y.sort(reverse = True)
for i in X:
  if x > 0:
    x -= i
  else:
    x += i
for i in Y:
  if y > 0:
    y -= i
  else:
    y += i
if (x,y) == (0,0):
  print("Yes")
else:
  print("No")

Solution (Python):
s = input()
dest_x, dest_y = map(int, input().split())

x_list = []
y_list = []
cnt = 0
flg = 1
init = 1 if s[0] == 'F' else 0
for si in s:
    if si == 'F':
        cnt += 1
    else:
        if flg == 1:
            x_list.append(cnt)
        else:
            y_list.append(cnt)
        cnt = 0
        flg *= -1
if flg == 1:
    x_list.append(cnt)
else:
    y_list.append(cnt)

if init:
    dest_x -= x_list[0]
    x_list[0] = 0

dest_x, dest_y = abs(dest_x), abs(dest_y)

x_max = sum(x_list)
if x_max < dest_x:
    print('No')
    exit()
dpx = [[0 for _ in range(x_max+1)] for _ in range(len(x_list)+1)]
dpx[0][-1] = 1
for i in range(len(x_list)):
    for j in range(x_max+1):
        x = x_list[i]
        if dpx[i][j] == 1:
            if j - 2 * x >= 0:
                dpx[i+1][j-2*x] = 1
            dpx[i+1][j] = 1
if dpx[-1][dest_x] == 0:
    print('No')
    exit()

y_max = sum(y_list)
if y_max < dest_y:
    print('No')
    exit()
dpy = [[0 for _ in range(y_max+1)] for _ in range(len(y_list)+1)]
dpy[0][-1] = 1
for i in range(len(y_list)):
    for j in range(y_max+1):
        y = y_list[i]
        if dpy[i][j] == 1:
            if j - 2 * y >= 0:
                dpy[i+1][j-2*y] = 1
            dpy[i+1][j] = 1
if dpy[-1][dest_y] == 0:
    print('No')
    exit()

print('Yes')

Solution (Python):

def check(z, zz, z_list):
  if len(z_list) == 0:
    if z != zz:
      return 0
    else:
      return 1
  else:
    z_n = len(z_list)
    z_hash = {}
    z_hash[0] = {}
    z_hash[0][zz] = 1
    z_hash[z_n] = {}
    z_hash[z_n][z] = 1
    for i in range(0,z_n):
      if i+1 not in z_hash:
        z_hash[i+1] = {}
        for b_z in z_hash[i]:
          a_z = b_z + z_list[i]
          z_hash[i+1][a_z] = 1
          a_z = b_z - z_list[i]
          z_hash[i+1][a_z] = 1
      else:
        for b_z in z_hash[i]:
          a_z = b_z + z_list[i]
          if a_z in z_hash[i+1]:
            return 1
          a_z = b_z - z_list[i]
          if a_z in z_hash[i+1]:
            return 1
        return 0
      if z_n-i-1 not in z_hash:
        z_hash[z_n-i-1] = {}
        for a_z in z_hash[z_n-i]:
          b_z = a_z + z_list[z_n-i-1]
          z_hash[z_n-i-1][b_z] = 1
          b_z = a_z - z_list[z_n-i-1]
          z_hash[z_n-i-1][b_z] = 1
      else:
        for a_z in z_hash[z_n-i]:
          b_z = a_z + z_list[z_n-i-1]
          if b_z in z_hash[z_n-i-1]:
            return 1
          b_z = a_z - z_list[z_n-i-1]
          if b_z in z_hash[z_n-i-1]:
            return 1
        return 0



s = input()
x, y = map(int, input().split())
data = s.split("T")
xx, yy = 0, 0
i = 0
x_list = []
y_list = []
for Fs in data:
  if len(Fs) == 0:
    i += 1
  else:
    if i == 0:
      xx = len(Fs)
    elif i % 2 == 0:
      x_list.append(len(Fs))
    else:
      y_list.append(len(Fs))
    i += 1
if check(x, xx, x_list) == 1 and check(y, yy, y_list) == 1:
  print("Yes")
else:
  print("No")

Solution (Python):
import sys
import itertools
from itertools import accumulate
def main():
    import itertools
    def eratosthenes_sieve(n):
        table = [0] * (n + 1)
        prime_list = []
        for i in range(2, n + 1):
            if table[i] == 0:
                prime_list.append(i)
                for j in range(i + i, n + 1, i):
                    table[j] = 1
        return prime_list
        def inputs():return (int(x) for x in input().split())
    input = sys.stdin.readline
    def inputs():
        return [int(x) for x in input().split()]
    S = input()
    x,y=inputs()
    cnt=0
    s =[]
    if S[0]=="T":
        s.append(0)
        cnt+=1
    else:
        cnt+=1
    for i in range(len(S)-2):
        if S[i]==S[i+1]:
            cnt+=1
        else:
            s.append(cnt)
            cnt=1
    s.append(cnt)
    flag=1
    x1 = []
    y1 = []
    t= 0
    for i in range(len(s)):
        if i%2==0:
            if flag==1:
              if i!=0:
                x1.append(s[i])
            else:
                y1.append(s[i])
        else:
            flag=(flag+s[i])%2
            t+=s[i]
    x2 = sum(x1)+s[0]
    y2= sum(y1)
    if abs(x)>x2 or abs(y)>y2:
      print("No")
      exit()
    m = max(x2,y2,t)
    dpx=[[-1]*(m*2+5) for i in range(len(x1)+1)]
    dpx[0][m+s[0]]=1
    dpy =[[-1]*(m*2+5) for i in range(len(y1)+1)]
    dpy[0][m]=1
    for i in range(len(x1)):
        for j in range(m*2):
          if dpx[i][j]!=-1:
            if j+x1[i]<=(m)*2:
                dpx[i+1][j+x1[i]]=1
            if j-x1[i]>=0:
                dpx[i+1][j-x1[i]]=1      
    for i in range(len(y1)):
        for j in range(m*2):
          if dpy[i][j]!=-1:
            if j+y1[i]<=m*2:
                dpy[i+1][j+y1[i]]=1
                
            if j-y1[i]>=0:
                dpy[i+1][j-y1[i]]=1
                
    if dpx[len(x1)][m+x]==1 and dpy[len(y1)][m+y]==1:
        print("Yes")
    else:
        print("No")
main()

Solution (Python):
s = input()
x,y = map(int,input().split(" "))
l = [len(c) for c in s.split("T")]
X = l[::2]
Y = l[1::2]
 
ansX = set([X[0]])
for i in range(1,len(X)):
    ansX = set([c+X[i] for c in ansX]+[c-X[i] for c in ansX])
 
ansY = set([0])
for i in range(0,len(Y)):
    ansY = set([c+Y[i] for c in ansY]+[c-Y[i] for c in ansY])
    
if x in ansX and y in ansY:
    print("Yes")
else:
    print("No")

Solution (Python):

import math
import sys
import itertools
MAX_INT = int(10e12)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

s = S()
x,y = IL()
N = len(s)

xlist = []
ylist = []

f = 0
cnt = 0
for i in range(N):
  if s[i] == "F":
    cnt += 1
  elif s[i] == "T":
    if f == 0:
      xlist.append(cnt)
      f = 1
    else:
      ylist.append(cnt)
      f = 0
    cnt = 0
else:
  if f == 0:
    xlist.append(cnt)
  else:
    ylist.append(cnt)

x -= xlist.pop(0)
Xsum = sum(xlist)
Ysum = sum(ylist)

if (Xsum-x)%2 != 0:
  print("No")
  exit()
else:
  Xnum = (Xsum-abs(x))//2
if (Ysum-y)%2 != 0:
  print("No")
  exit()
else:
  Ynum = (Ysum-abs(y))//2



XDP = [[0]*(abs(Xnum)+1) for _ in range(len(xlist)+1)]
YDP = [[0]*(abs(Ynum)+1) for _ in range(len(ylist)+1)]

XDP[0][0] = 1
YDP[0][0] = 1

for i in range(1,len(xlist)+1):
  for j in range(Xnum+1):
    if XDP[i-1][j] == 1:
      XDP[i][j] = 1
      if j+xlist[i-1] < Xnum+1:
        XDP[i][j+xlist[i-1]] = 1




for i in range(1,len(ylist)+1):
  for j in range(Ynum+1):
    if YDP[i-1][j] == 1:
      YDP[i][j] = 1
      if j+ylist[i-1] < Ynum+1:
        YDP[i][j+ylist[i-1]] = 1




if XDP[-1][-1] == 1 and YDP[-1][-1] == 1:
  print("Yes")
else:
  print("No")