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()
x, y = map(int, input().split())
command = s.split("T")

command_length = len(command)

x_command = []
y_command = []
for i in range(command_length):
    c = len(command[i])
    if i % 2 == 0:
        x_command.append(c)

    else:
        y_command.append(c)
x -= x_command.pop(0)
x_command.sort(reverse=True)
y_command.sort(reverse=True)

c_x = 0
c_y = 0
for c in x_command:
    if x - c_x >= 0:
        c_x += c
    else:
        c_x -= c

for c in y_command:
    if y - c_y > 0:
        c_y += c
    else:
        c_y -= c
if c_x == x and c_y == y:
    print("Yes")
else:
    print("No")

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

S = S+"T"
d,cnt = 0,0
pos = [set([0]),set([0])]
init = False
for s in S:
    
    if s=="F":
        cnt += 1
    
    else:
        
        if cnt>0:
            temp = set([])
            for p in pos[d]:
                temp.add(p+cnt)
                if init:
                    temp.add(p-cnt)
            pos[d] = temp
        
        d = (d+1)%2
        cnt = 0
        
        init = True

print("Yes" if X in pos[0] and Y in pos[1] else "No")

Solution (Python):


"""
参考：https://img.atcoder.jp/abc082/editorial.pdf
　　　https://atcoder.jp/contests/abc082/submissions/3930644
・メモリ節約のため、dictをsetにした。
・さらに使い終わった1つ前はもういらないので消すことにした。
・あとちょっと初期化とか色々調整
"""

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

S2 = []
tmp = 0

for i in range(len(S)):
    if S[i] == 'F':
        tmp += 1
        if i == len(S)-1:
            S2.append(tmp)
    
    else:
        S2.append(tmp)
        tmp = 0


dpx = [set() for _ in range(len(S2))]
dpy = [set() for _ in range(len(S2))]

dpx[0].add(S2[0])

dpy[0].add(0)
for i in range(1, len(S2)):
    
    for v in dpx[i-1]:
        if i % 2 == 0:
            
            dpx[i].add(v-S2[i])
            dpx[i].add(v+S2[i])
        else:
            
            dpx[i].add(v)
    
    dpx[i-1] = set()
    
    for v in dpy[i-1]:
        if i % 2 == 1:
            dpy[i].add(v-S2[i])
            dpy[i].add(v+S2[i])
        else:
            dpy[i].add(v)
    dpy[i-1] = set()


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

Solution (Python):


"""
参考：https://img.atcoder.jp/abc082/editorial.pdf
　　　https://atcoder.jp/contests/abc082/submissions/3930644
・メモリ節約のため、dictをsetにした。
・さらに使い終わった1つ前はもういらないので消すことにした。
・あとちょっと初期化調整
"""

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

S2 = []
tmp = 0

for i in range(len(S)):
    if S[i] == 'F':
        tmp += 1
    
    else:
        S2.append(tmp)
        tmp = 0
S2.append(tmp)


dpx = [set() for _ in range(len(S2))]
dpy = [set() for _ in range(len(S2))]

dpx[0].add(S2[0])

dpy[0].add(0)
for i in range(1, len(S2)):
    
    for v in dpx[i-1]:
        if i % 2 == 0:
            
            dpx[i].add(v-S2[i])
            dpx[i].add(v+S2[i])
        else:
            
            dpx[i].add(v)
    
    dpx[i-1] = set()
    
    for v in dpy[i-1]:
        if i % 2 == 1:
            dpy[i].add(v-S2[i])
            dpy[i].add(v+S2[i])
        else:
            dpy[i].add(v)
    dpy[i-1] = set()


if X in dpx[len(S2)-1] and Y in dpy[len(S2)-1]:
    print('Yes')
else:
    print('No')

Solution (Python):


"""
参考：https://img.atcoder.jp/abc082/editorial.pdf
　　　https://atcoder.jp/contests/abc082/submissions/3930644
・メモリ節約のため、dictをsetにした。
・さらに使い終わった1つ前はもういらないので消すことにした。
"""

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

S2 = []
tmp = 0

for i in range(len(S)):
    if S[i] == 'F':
        tmp += 1
    
    else:
        S2.append(tmp)
        tmp = 0
S2.append(tmp)


if len(S2) == 1:
    if X == S2[0] and Y == 0:
        print('Yes')
    else:
        print('No')
    exit()


dpx = [set() for _ in range(len(S2))]
dpy = [set() for _ in range(len(S2))]

dpx[0].add(S2[0])
dpx[1].add(S2[0])

dpy[0].add(0)
dpy[1].add(S2[1])
dpy[1].add(-S2[1])
for i in range(2, len(S2)):
    
    for v in dpx[i-1]:
        if i % 2 == 0:
            
            dpx[i].add(v-S2[i])
            dpx[i].add(v+S2[i])
        else:
            
            dpx[i].add(v)
    
    dpx[i-1] = set()
    
    for v in dpy[i-1]:
        if i % 2 == 1:
            dpy[i].add(v-S2[i])
            dpy[i].add(v+S2[i])
        else:
            dpy[i].add(v)
    dpy[i-1] = set()

if X in dpx[len(S2)-1] and Y in dpy[len(S2)-1]:
    print('Yes')
else:
    print('No')

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


X_Commands = [len(s) for s in S.split('T')[::2]]
Y_Commands = [len(s) for s in S.split('T')[1::2]]


x_set = set((X_Commands[0], ))
for xc in X_Commands[1:]:
    tmp = set()
    for xs in x_set:
        tmp.add(xs + xc)
        tmp.add(xs - xc)
    x_set = tmp


y_set = set((0, ))
for yc in Y_Commands:
    tmp = set()
    for ys in y_set:
        tmp.add(ys + yc)
        tmp.add(ys - yc)
    y_set = tmp

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

Solution (Python):
s = list(input())

a = []
k = 0
for z in s:
  if z == "T":
    a += [k]
    k = 0
  else:
    k += 1
a += [k]


l = [a[0]]
for k in a[2::2]:
  if k == 0:
    continue
  l = [t+k for t in l] + [t-k for t in l]
  l = list(set(l))

m = [0]
for k in a[1::2]:
  if k == 0:
    continue
  m = [t+k for t in m] + [t-k for t in m]
  m = list(set(m))

x,y = map(int,input().split())
print("Yes" if x in l and y in m else "No")

Solution (Python):



def reachable(start, target, lst):

    current = set()
    current.add(start)

    for moves in lst:
        candidates = set()
        for x in current:
            candidates.add(x + moves)
            candidates.add(x - moves)
        current = candidates

    return target in current


def main():
    s = input()
    sa = input().split(' ')
    x = int(sa[0])
    y = int(sa[1])

    x_list = []
    y_list = []
    moves = 0
    move_x = True
    lst = x_list
    for cmd in s:
        if cmd == 'F':
            moves += 1
        else:
            
            if moves != 0:
                lst.append(moves)
            moves = 0
            move_x = not move_x
            lst = x_list if move_x else y_list
    if moves != 0:
        lst.append(moves)

    if s[0] == 'F':
        x -= x_list[0]
        x_list = x_list[1:]

    if reachable(0, x, x_list) and reachable(0, y, y_list):
        print('Yes')
    else:
        print('No')


if __name__ == '__main__':
    main()

Solution (Python):
s = input()
x, y = map(int, input().split())
dpx = set()
dpy = set()
xli = []
yli = []
tmp = 0
d = 0
tcnt = 0
xc = 0
yc = 0

for i in range(len(s)):
    if s[i] == "F":
        if tcnt % 2 == 0:
            xc += 1
        else:
            yc += 1
    else:
        if tcnt % 2 == 0:
            if xc != 0:
                xli.append(xc)
                xc = 0
        else:
            if yc != 0:
                yli.append(yc)
                yc = 0
        tcnt += 1
if tcnt % 2 == 0:
    xli.append(xc)
    xc = 0
else:
    yli.append(yc)
    yc = 0
    
    

if s[0] == 'F':
    dpx.add(xli[0])
    xli = xli[1:]
else:
    dpx.add(0)
dpy.add(0)
for a in xli:
    tmp = set()
    for dx in dpx:
        tmp.add(dx + a)
        tmp.add(dx - a)
    dpx = tmp
for a in yli:
    tmp = set()
    for dy in dpy:
        tmp.add(dy + a)
        tmp.add(dy - a)
    dpy = tmp


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

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):
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 = [X[0]]
for i in range(1,len(X)):
    if not X[i] == 0:
        ansX = list(set([c+X[i] for c in ansX]+[c-X[i] for c in ansX]))

ansY = [0]
for i in range(0,len(Y)):
    if not Y[i] == 0:
        ansY = list(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):
S = input()
x, y = map( int, input().split())
c = 0
ver = []
row = []
l = 0
for s in S:
    if s == "F":
        c += 1
    else:
        if l%2 == 0:
            row.append(c)
        else:
            ver.append(c)
        l += 1
        c = 0
if l%2 == 0:
    row.append(c)
else:
    ver.append(c)

x -= row.pop(0)
row.sort( key = None, reverse = True)
ver.sort( key = None, reverse = True)
nx, ny = 0, 0
for a in row:
    if a >= 0:
        if nx <= x:
            nx += a
        else:
            nx -= a
    else:
        if nx > x:
            nx += a
        else:
            nx -= a
for a in ver:
    if a >= 0:
        if ny <= y:
            ny += a
        else:
            ny -= a
    else:
        if ny > y:
            ny += a
        else:
            ny -= a

if x == nx and y == ny:
    print("Yes")
else:
    print("No")

Solution (Python):
def is_reachable(axis,goal):
    res=sum(axis)-goal
    if(res%2==1 or res <0):
        return False
    res//=2
    tmp=1
    for x in axis:
        tmp|=tmp << x
    if( tmp & (1 << res)):
        return True
    else:
        return False


s=input()
x,y=map(int,input().split())
x_axis=[]
y_axis=[]

i=0
while(s[i]=="F"):
    x-=1
    i+=1
    if(i>=len(s)):
        break

flag=True
tmp_count=0

for j in range(i+1,len(s)):
    if(s[j]=="F"):
        tmp_count+=1
    else:
        if(flag):
            y_axis.append(tmp_count)
        else:
            x_axis.append(tmp_count)
        tmp_count=0
        flag= not flag
    
if(tmp_count!=0):
    if(flag):
        y_axis.append(tmp_count)
    else:
        x_axis.append(tmp_count)

if( is_reachable(x_axis,x) and is_reachable(y_axis,y)):
    print("Yes")
else:
    print("No")

Solution (Python):
def main():
    s = input()
    x, y = map(int, input().split())
    if solve(s, x, y):
        print('Yes')
    else:
        print('No')

def solve(s, x, y):
    moves = split_moves(s)
    if not match(x - moves[0][0], moves[0][1:]):
        return False
    if not match(y, moves[1]):
        return False

    return True

def match(x, moves):
    s = sum(moves)
    if x > s:
        return False

    if (s - x) % 2 != 0:
        return False

    return knapsack((s - x) // 2, moves)

def knapsack(x, moves):
    if x == 0:
        return True

    prev = set([0])
    for m in moves:
        curr = set(i + m for i in prev if i + m <= x)
        if x in curr:
            return True
        curr |= prev
        prev = curr
    return False

def split_moves(s):
    moves = [[0], []]
    i = 0

    for c in s:
        if c == 'F':
            moves[i][-1] += 1
        else:
            i = 1 - i
            moves[i].append(0)

    return moves

main()

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

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

X_reached = {X_move.pop(0)}
Y_reached = {0}

for x in X_move:
    tmp = set()
    for nx in X_reached:
        tmp.add(nx + x)
        tmp.add(nx - x)
    X_reached = tmp

for y in Y_move:
    tmp = set()
    for ny in Y_reached:
        tmp.add(ny + y)
        tmp.add(ny - y)
    Y_reached = tmp

print("Yes" if (X in X_reached) and (Y in Y_reached) else "No")

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

if len(d) == 0:
    d.append(0)
if len(d) == 1:
    d.append(0)
dpx = [d[0]]
dpy = [d[1],-d[1]]
for i in range(2,len(d)):
    di = d[i]
    trans = set()
    if i%2==0:
        for xj in dpx:
            trans.add(xj+di)
            trans.add(xj-di)
        dpx = trans
    else:
        for yj in dpy:
            trans.add(yj+di)
            trans.add(yj-di)
        dpy = trans
ans = (x in dpx)&(y in dpy)

if ans:
    print('Yes')
else:
    print('No')

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

if len(d) == 0:
    d.append(0)
if len(d) == 1:
    d.append(0)
dpx = [d[0]]
dpy = [d[1],-d[1]]
for i in range(2,len(d)):
    di = d[i]
    trans = set()
    if i%2==0:
        for xj in dpx:
            trans.add(xj+di)
            trans.add(xj-di)
        dpx = list(trans)
    else:
        for yj in dpy:
            trans.add(yj+di)
            trans.add(yj-di)
        dpy = list(trans)
ans = (x in dpx)&(y in dpy)

if ans:
    print('Yes')
else:
    print('No')

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

T = S.split("T")
u, v = [len(T[0])], [0]
dp = [u, v]
for i in range(1, len(T)):
    s = set()
    k = len(T[i])
    if i % 2 != 0:
        Q = dp[1]
        for j in Q:
            s.add(j - k)
            s.add(j + k)
        dp = [dp[0], list(s)]
    else:
        Q = dp[0]
        for j in Q:
            s.add(j - k)
            s.add(j + k)
        dp = [list(s), dp[1]]
if x in dp[0] and y in dp[1]:
    print("Yes")
else:
    print("No")

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

ini = s.find('T')
s=s[ini+1:]

is_y=1
point=[set([ini]),set([0])]

while len(s)>1:
    tmp=s.find('T')
    if tmp != 0:
        set_=set()
        for z in point[is_y]:
            set_.add(z+tmp)
            set_.add(z-tmp)
        point[is_y]=set_
    is_y+=1
    is_y%=2
    s=s[tmp+1:]

if x in point[0] and y in point[1]:
    print('Yes')
else:
    print('No')

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

x_list = []
y_list = []

st = s.split("T")
x -= len(st[0])

for i, ls in enumerate(st[1:]):
    if i % 2:
        x_list.append(len(ls))
    else:
        y_list.append(len(ls))

def judge(xy, ls):
    dp = set([0])
    for l in ls:
        tmp = []
        for v in dp:
            tmp.append(v+l)
            tmp.append(v-l)
        dp = set(tmp)
    return xy in dp

print("Yes" if judge(x, x_list) and judge(y, y_list) else "No")

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

x_move = [0]
y_move = []
mode = 0
p = 0
q = -1
for c in s:
    if mode == 0:
        if c == 'F':
            x_move[p] += 1
        if c == 'T':
            mode = 1
            y_move.append(0)
            q += 1
    elif mode == 1:
        if c == 'F':
            y_move[q] += 1
        if c == 'T':
            mode = 0
            x_move.append(0)
            p += 1

temp_x = [x_move[0]]
for i in range(1, len(x_move)):
    next_x = []
    for temp in temp_x:
        next_x.append(temp - x_move[i])
        next_x.append(temp + x_move[i])
    temp_x = [] + list(set(next_x))

if x not in temp_x:
    print('No')
    exit()

temp_y = [0]
for i in range(len(y_move)):
    next_y = []
    for temp in temp_y:
        next_y.append(temp - y_move[i])
        next_y.append(temp + y_move[i])
    temp_y = [] + list(set(next_y))

if y not in temp_y:
    print('No')
    exit()

print('Yes')

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

a = []
t = 0
for s in S:
    if s == "F":
        t += 1
    else:
        a.append(t)
        t = 0
a.append(t)

if a == []:
    if x == y == 0:
        print("Yes")
        exit(0)
    else:
        print("No")
        exit(0)

X = [a[0]]
Y = [0]

for k in range(1,len(a)):
    if k%2 == 0:
        nx = []
        for e in X:
            nx.append(e+a[k])
            nx.append(e-a[k])
        X = list(set(nx))
    else:
        ny = []
        for e in Y:
            ny.append(e+a[k])
            ny.append(e-a[k])
        Y = list(set(ny))

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

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

sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

s = ns()
x,y = li()

command = s.split("T")

lr = len(command[0])
ud = 0

lr_cm = [len(cm) for cm in command[2::2]]
ud_cm = [len(cm) for cm in command[1::2]]

lr_cm.sort(reverse=True)
ud_cm.sort(reverse=True)

for cm in lr_cm:
    if lr > x:
        lr -= cm
    else:
        lr += cm
        
for cm in ud_cm:
    if ud > y:
        ud -= cm
    else:
        ud += cm
        
if lr == x and ud == y:
    print("Yes")
else:
    print("No")

Solution (Python):
s=input()
rx,ry=map(int,input().split())


if "T" in s:
    x=s.index("T")
else:
    x=len(s)

y=0


runx=rx-x
runy=ry-y
s=s[x:]+"T"




disx=[]
disy=[]
now=0
count=0
for j in range(len(s)):
    if s[j]=="T":
        if now==0:
            disx.append(count)
        else:
            disy.append(count)
        now=(now+1)%2
        count=0
    else:
        count+=1
    
disetx={0}
disety={0}

for i in disx:
    nextsetx=set()
    for j in disetx:
        nextsetx.add(j+i)
        nextsetx.add(j-i)
    disetx=nextsetx

for i in disy:
    nextsety=set()
    for j in disety:
        nextsety.add(j+i)
        nextsety.add(j-i)
    disety=nextsety



if runx in disetx and runy in disety:
    print("Yes")
else:
    print("No")

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

X -= len(st[0])

xds = []
yds = []
for i,s in enumerate(st[1:]):
    if i%2:
        xds.append(len(s))
    else:
        yds.append(len(s))

def ok(to, ds):
    dp = set([0])
    for d in ds:
        tmp = []
        for v in dp:
            tmp.append(v+d)
            tmp.append(v-d)
        dp = set(tmp)
    return to in dp

print('Yes' if ok(X,xds) and ok(Y,yds) else 'No')

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

sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

s = ns()
x,y = li()

command = s.split("T")

ud = set()
lr = set()

lr.add(len(command[0]))
ud.add(0)

for i, cm in enumerate(command[1:]):
    if i%2 == 1:
        new_lr = set()
        for element in lr:
            new_lr.add(element + len(cm))
            new_lr.add(element - len(cm))
            
        lr = new_lr
        
    else:
        new_ud = set()
        for element in ud:
            new_ud.add(element + len(cm))
            new_ud.add(element - len(cm))
            
        ud = new_ud

if (x in lr) and (y in ud):
    print("Yes")
else:
    print("No")

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

sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

s = ns()
x,y = li()

command = s.split("T")

ud = set()
lr = set()

lr.add(len(command[0]))
ud.add(0)

for i, cm in enumerate(command[1:]):
    if i%2 == 1:
        new_lr = set()
        for element in lr:
            new_lr.add(element + len(cm))
            new_lr.add(element - len(cm))
            
        lr = new_lr
        
    else:
        new_ud = set()
        for element in ud:
            new_ud.add(element + len(cm))
            new_ud.add(element - len(cm))
            
        ud = new_ud

if (x in lr) and (y in ud):
    print("Yes")
else:
    print("No")

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

sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

s = ns()
x,y = li()

command = s.split("T")

ud = set()
lr = set()

lr.add(len(command[0]))
ud.add(0)

for i, cm in enumerate(command[1:]):
    if i%2 == 1:
        new_lr = set()
        for element in lr:
            new_lr.add(element + len(cm))
            new_lr.add(element - len(cm))
            
        lr = new_lr
        
    else:
        new_ud = set()
        for element in ud:
            new_ud.add(element + len(cm))
            new_ud.add(element - len(cm))
            
        ud = new_ud

if (x in lr) and (y in ud):
    print("Yes")
else:
    print("No")

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

sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

s = ns()
x,y = li()

command = s.split("T")

ud = set()
lr = set()

lr.add(len(command[0]))
ud.add(0)

for i, cm in enumerate(command[1:]):
    if i%2 == 1:
        new_lr = set()
        for element in lr:
            new_lr.add(element + len(cm))
            new_lr.add(element - len(cm))
            
        lr = new_lr
        
    else:
        new_ud = set()
        for element in ud:
            new_ud.add(element + len(cm))
            new_ud.add(element - len(cm))
            
        ud = new_ud

if (x in lr) and (y in ud):
    print("Yes")
else:
    print("No")

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

sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

s = ns()
x,y = li()

command = s.split("T")

ud = set()
lr = set()

lr.add(len(command[0]))
ud.add(0)

for i, cm in enumerate(command[1:]):
    if i%2 == 1:
        new_lr = set()
        for element in lr:
            new_lr.add(element + len(cm))
            new_lr.add(element - len(cm))
            
        lr = new_lr
        
    else:
        new_ud = set()
        for element in ud:
            new_ud.add(element + len(cm))
            new_ud.add(element - len(cm))
            
        ud = new_ud

if (x in lr) and (y in ud):
    print("Yes")
else:
    print("No")

Solution (Python):
S = input()
x, y = map(int, input().split())
XY = [len(x) for x in S.split('T')]
moveX = XY[0::2] + [0]
moveY = XY[1::2] + [0]
def moves(t, move):
  if sum(move) >= t:
    pos = [0] * (sum(move) - t + 1)
    P = len(pos)
    pos[0] = 1
    for m in move:
      _pos = [x for x in pos]
      for i, p in enumerate(pos):
        if p and i + 2 * m < P:
          _pos[i + 2 * m] = 1
      pos = _pos
    return pos[-1]
  else:
    return False
print('Yes' if moves(x - moveX[0], moveX[1:]) and moves(y, moveY) else 'No')

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

dp = [set([s[0]]), set([0])]

for i in range(1, len(s)):
    nxt = set()
    for j in dp[i % 2]:
        nxt.add(j + s[i])
        nxt.add(j - s[i])
    dp[i % 2] = nxt

if x in dp[0] and y in dp[1]:
    print("Yes")
else:
    print("No")

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

dp = [set([s[0]]), set([0])]

for i in range(1, len(s)):
    nxt = set()
    for j in dp[i % 2]:
        nxt.add(j + s[i])
        nxt.add(j - s[i])
    dp[i % 2] = nxt

if x in dp[0] and y in dp[1]:
    print("Yes")
else:
    print("No")

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

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

s = lc()
x,y = li()



command = []
last = ""
com = ""
cnt = 0
for i, si in enumerate(s):
    if si != last:
        if i != 0:
            command.append((com, cnt))
        cnt = 1
        com = si
        last = si
    
    else:
        cnt += 1
        last = si
        
    if i == len(s)-1:
        command.append((com, cnt))
        
        

isx = 1
if command[0][0] == "F":
    x_cand = set([command[0][1]])
    y_cand = set([0])
    command = command[1:]
else:
    x_cand = set([0])
    y_cand = set([0]) 

for cmd, cnt in command:
    if cmd == 'F':
        if isx == 1:
            x_cand = set([xi+cnt for xi in x_cand]) | set([xi-cnt for xi in x_cand])
        elif isx == -1:
            y_cand = set([yi+cnt for yi in y_cand]) | set([yi-cnt for yi in y_cand])
        
    elif cmd == 'T':
        if cnt%2 == 1:
            isx *= -1
    
if x in x_cand and y in y_cand:
    print('Yes')
else:
    print('No')

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

s = s.split('T')
move_x = s[::2]
move_y = s[1::2]

x_cur = len(move_x[0])
for nx in sorted(move_x[1:], reverse = True):
    if x_cur <= x:
        x_cur += len(nx)
    else:
        x_cur -= len(nx)

y_cur = 0
for ny in sorted(move_y, reverse = True):
    if y_cur <= y:
        y_cur += len(ny)
    else:
        y_cur -= len(ny)

if x_cur == x and y_cur == y:
    print('Yes')
else:
    print('No')

Solution (Python):
root = [len(i) for i in input().split("T")]
xi_set, yi_set = [root[0]], [0]
xg, yg = map(int, input().split())
x_list, y_list = root[2::2], root[1::2]

for delta_x in x_list:
    xi_set = set([xi+delta_x for xi in xi_set] + [xi-delta_x for xi in xi_set])
if not xg in xi_set:
    print("No")
    exit()
    
for delta_y in y_list:
    yi_set = set([yi+delta_y for yi in yi_set] + [yi-delta_y for yi in yi_set])
if not yg in yi_set:
    print("No")
    exit()

print("Yes")

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

lisx = s[2::2]
lisy = s[1::2]

nowx = {s[0]}
nowy = {0}

for x1 in lisx:
  gone = set()
  for x2 in nowx:
    gone.add(x2 + x1)
    gone.add(x2 - x1)
  nowx = gone

for y1 in lisy:
  gone = set()
  for y2 in nowy:
    gone.add(y2 + y1)
    gone.add(y2 - y1)
  nowy = gone


if x in nowx and y in nowy:print("Yes")
else:print("No")

Solution (Python):
S = [len(s) for s in input().split("T")]
X, Y = map(int, input().split())


X_move = S[2::2]
Y_move = S[1::2]


X_reached = {S[0]}
Y_reached = {0}


for xm in X_move:
    tmp = set()
    for xr in X_reached:
        tmp.add(xr + xm)
        tmp.add(xr - xm)
    X_reached = tmp

for ym in Y_move:
    tmp = set()
    for yr in Y_reached:
        tmp.add(yr + ym)
        tmp.add(yr - ym)
    Y_reached = tmp

print("Yes" if (X in X_reached and Y in Y_reached) else "No")

Solution (Python):



import sys
from itertools import islice

def reachable(steps, x):
    m = len(steps)
    if m == 0:
        return x == 0
    n = sum(steps) + 1
    if n <= x:
        return False
    dp = [[False] * n for _ in range(m)]
    dp[0][steps[0]] = True
    for i, step in enumerate(islice(steps, 1, None)):
        for j in range(n):
            if dp[i][j] == True:
                dp[i + 1][j + step] = True
                if j - step >= 0:
                    dp[i + 1][j - step] = True
    return dp[-1][x]


def main():
    s = sys.stdin.readline().strip()
    x, y = map(int, sys.stdin.readline().split())

    i = s.find('T')
    if i != -1:
        x -= i
        i += 1
    else:
        i = len(s)
        x -= i

    xsteps = []
    ysteps = []
    cnt = 0
    nturns = 0
    for c in islice(s + 'T', i, None):
        if c == 'F':
            cnt += 1
            continue
        if cnt > 0:
            if nturns % 2 != 0:
                xsteps.append(cnt)
            else:
                ysteps.append(cnt)
            cnt = 0
        nturns += 1

    if reachable(xsteps, abs(x)) == True and reachable(ysteps, abs(y)) == True:
        print('Yes')
    else:
        print('No')

if __name__ == '__main__':
    main()

Solution (Python):
s=input().split("T")
x,y=map(int,input().split())
A=[]
B=[]
for i,j in enumerate(s):
    if i%2==0:
        A.append(len(j))
    else:
        B.append(len(j))
T=max([sum(A),sum(B),abs(x),abs(y)])
X=len(A)
Y=len(B)
if abs(x)+abs(y)>sum(A)+sum(B):
    print("No")
    exit()
dpx=[[False for i in range(2*T+1)] for _ in range(X+1)]
dpy=[[False for i in range(2*T+1)] for _ in range(Y+1)]
dpx[0][T]=True
dpy[0][T]=True
for l in range(X):
    for i in range(2*T+1):
        if dpx[l][i]:
            dpx[l+1][i+A[l]]=True
            if l==0:
                continue
            dpx[l+1][i-A[l]]=True
for r in range(Y):
    for i in range(2*T+1):
        if dpy[r][i]:
            dpy[r+1][i-B[r]]=True
            dpy[r+1][i+B[r]]=True
if dpx[X][x+T] and dpy[Y][y+T]:
    print("Yes")
else:
    print("No")

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

h, v = [], []
for i, si in enumerate(s):
    if i % 2 == 0:
        h.append(len(si))
    else:
        v.append(len(si))

def search(move, goal, horisontal):
    if horisontal:
        goal -= move.pop(0)

    move.sort(reverse=True)
    for m in move:
        if goal > 0:
            goal -= m
        else:
            goal += m
    return goal == 0

if search(h, x, True) and search(v, y, False):
    print('Yes')
else:
    print('No')

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

t = [len(x) for x in s.split('T')]

tx = t[2::2]
M = len(tx)
dpx = {t[0]}
for i in range(M):
    tmp = set()
    for x in dpx:
        tmp.add(x + tx[i])
        tmp.add(x - tx[i])
    dpx = tmp

ty = t[1::2]
M = len(ty)
dpy = {0}
for i in range(M):
    tmp = set()
    for y in dpy:
        tmp.add(y + ty[i])
        tmp.add(y - ty[i])
    dpy = tmp

if X in dpx and Y in dpy:
    print('Yes')
else:
    print('No')