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):
def main():
    
    
    
    


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

    N = 8010

    X = [0]
    Y = [0]
    flag = True
    for s in S:
        if s == 'F':
            if flag:
                X[-1] += 1
            else:
                Y[-1] += 1
        else:
            if flag:
                if Y[-1] != 0:
                    Y.append(0)
            else:
                if X[-1] != 0:
                    X.append(0)
            flag = not flag

    dp = [[0] * (2 * N) for _ in range(2)]
    if S[0] == 'F':
        dp[0][X[0] + N] = 1
    else:
        dp[0][X[0] + N] = 1
        dp[0][-X[0] + N] = 1

    for i in range(1, len(X)):
        x_ = X[i]
        for j in range(2 * N):
            dp[i % 2][j] = 0
            if j >= x_:
                if dp[(i - 1) % 2][j - x_]:
                    dp[i % 2][j] = 1
            if j + x_ < 2 * N:
                if dp[(i - 1) % 2][j + x_]:
                    dp[i % 2][j] = 1

    
    if dp[(len(X) - 1) % 2][x + N] == 0:
        print ('No')
        exit()

    dp = [[0] * (2 * N) for _ in range(2)]
    dp[0][Y[0] + N] = 1
    dp[0][-Y[0] + N] = 1

    for i in range(1, len(Y)):
        y_ = Y[i]
        for j in range(2 * N):
            dp[i % 2][j] = 0
            if j >= y_:
                if dp[(i - 1) % 2][j - y_]:
                    dp[i % 2][j] = 1
            if j + y_ < 2 * N:
                if dp[(i - 1) % 2][j + y_]:
                    dp[i % 2][j] = 1

    if dp[(len(Y)-1) % 2][y + N] == 0:
        print ('No')
        exit()

    print ('Yes')

if __name__ == '__main__':
    main()

Solution (Python):
S=input()
x,y=map(int, input().split())
N=len(S)
now=0
xs=[]
ys=[]
count=0
for i in range(N):
    if S[i]=="F":
        now+=1
    else:
        if count%2==0:
            xs.append(now)
        else:
            ys.append(now)
        count+=1
        now=0
    if S[i]=="F" and i==N-1:
        if count%2==0:
            xs.append(now)
        else:
            ys.append(now)

from collections import defaultdict
D=defaultdict(int)

xnow=xs.pop(0)
D[xnow]=1
for i in range(len(xs)):
    dx=xs[i]
    ds=D.keys()
    D=defaultdict(int)
    for v in ds:
        D[v+dx]=1
        D[v-dx]=1
D=dict(D)

xok=x in D


D=defaultdict(int)
D[0]=1
for i in range(len(ys)):
    dy=ys[i]
    ds=D.keys()
    D=defaultdict(int)
    for v in ds:
        D[v+dy]=1
        D[v-dy]=1
D=dict(D)

yok=y in D


if xok and yok:
    print("Yes")
else:
    print("No")

Solution (Python):
S = input().split('T')

N = len(S)
X = []
Y = []

for i in range(N):
    if i%2:
        Y.append(len(S[i]))
    else:
        X.append(len(S[i]))

Xsum = sum(X)
Ysum = sum(Y)

from collections import defaultdict

dpx = defaultdict(int)
dpy = defaultdict(int)

dpx[Xsum] = 1
dpy[Ysum] = 1

for k in range(1,len(X)):
    x = X[k]
    for i in range(-Xsum,Xsum+1):
        if dpx[i]:
            dpx[i-2*x] = 1

for y in Y:
    for i in range(-Ysum,Ysum+1):
        if dpy[i]:
            dpy[i-2*y] = 1

x,y = map(int,input().split())
print('Yes' if dpx[x] and dpy[y] else 'No')

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

def check(dz, z, base=8001, first_fixed=True):
  a = dz[0]
  if first_fixed:
    state = 1 << base + a
  else:
    state = 1 << (base + a) | 1 << (base - a) 
  for a in dz[1:]:
    state = (state<<a | state>>a)
  return (state>>(base+z)) & 1

if check(dx, x) and check(dy, y, first_fixed=False):
  print('Yes')
else:
  print('No')

Solution (Python):
def parse(s):
    first_val = None
    verticals, horizontals = list(), list()
    seq = 0
    is_horizontal = True
    is_first = True
    for c in s:
        if c == 'T':
            if is_first:
                first_val = seq
                is_first = False
            elif is_horizontal:
                horizontals.append(seq)
            else:
                verticals.append(seq)
            is_horizontal = not is_horizontal
            seq = 0
        else:
            seq += 1
    if seq > 0:
        if is_first:
            first_val = seq
        elif is_horizontal:
            horizontals.append(seq)
        else:
            verticals.append(seq)
    return first_val, verticals, horizontals


def check(vals, target):
    val_set = {0}
    for v in vals:
        tmp_set = set()
        for sv in val_set:
            tmp_set.add(sv + v)
            tmp_set.add(sv - v)
        val_set = tmp_set
    return target in val_set


def main():
    S = input()
    X, Y = list(map(int, input().split(' ')))
    fv, vs, hs = parse(S)
    if check(hs, X - fv) and check(vs, Y):
        print('Yes')
    else:
        print('No')


if __name__ == '__main__':
    main()

Solution (Python):
s = input() + 'T'
n = len(s)
x, y = map(int, input().split())
X = [[], [0]]
xy = 0
cnt =0
for i in s:
    if i == 'F':
        cnt += 1
    else:
        X[xy].append(cnt)
        xy = 1 - xy
        cnt = 0

def f(l):
    if len(l) == 0:
        return [0]
    post = [l[0]]
    for i in range(1, len(l)):
        if l[i]:
            pre = sorted(post)
            post = []
            for j in range(len(pre)):
                if j == 0 or pre[j] != pre[j-1]:
                    post.append(pre[j]+l[i])
                    post.append(pre[j]-l[i])
    return post

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

Solution (Python):





S = input()
gx, gy = map(int, input().split())


op_x = []
op_y = []
cnt_turn = 0
cnt_f = 0
sx,sy = 0, 0
for s in S:
    if s == "F":
        cnt_f += 1
    else:
        if cnt_turn  == 0:
            sx = cnt_f
            cnt_turn += 1
            cnt_f = 0
        elif cnt_turn % 2 == 0:
            op_x.append(cnt_f)
            cnt_turn += 1
            cnt_f = 0
        else:
            op_y.append(cnt_f)
            cnt_turn += 1
            cnt_f = 0

if cnt_turn  == 0:
    sx = cnt_f
    cnt_turn += 1
    cnt_f = 0
elif cnt_turn % 2 == 0:
    op_x.append(cnt_f)
    cnt_turn += 1
    cnt_f = 0
else:
    op_y.append(cnt_f)
    cnt_turn += 1
    cnt_f = 0

X = set()
X.add(sx)
for dx in op_x:
    nxt = set()
    for x in X:
        nxt.add(x + dx)
        nxt.add(x - dx)
    X = nxt

Y = set()
Y.add(sy)
for dy in op_y:
    nxt = set()
    for y in Y:
        nxt.add(y + dy)
        nxt.add(y - dy)
    Y = nxt

print("Yes" if gx in X and gy in Y else "No")

Solution (Python):



def run_length_encoding(s):
    '''
    連長圧縮を行う
    s ... iterable object e.g. list, str
    return
    ----------
    s_composed,s_num,s_idx
    それぞれ、圧縮後の文字列、その文字数、その文字が始まるidx
    '''
    s_composed = []
    s_sum = []
    s_idx = [0]
    pre = s[0]
    cnt = 1
    for i, ss in enumerate(s[1:], start=1):
        if pre == ss:
            cnt += 1
        else:
            s_sum.append(cnt)
            s_composed.append(pre)
            s_idx.append(i)
            cnt = 1
            pre = ss
    s_sum.append(cnt)
    s_composed.append(pre)
    
    return s_composed, s_sum, s_idx




s = input()
x, y = map(int, input().split())
s_composed, s_sum, _ = run_length_encoding(s)

if s_composed[0] == 'T':  
    s_composed.insert(0, 'F')
    s_sum.insert(0, 0)

is_x = True
hist_x = []
hist_y = []
for s, n in zip(s_composed, s_sum):
    if s == 'T':
        if n & 1:  
            is_x = not is_x
    else:
        if is_x:
            hist_x.append(n)
        else:
            hist_y.append(n)

'''
dpx[i][j]...x座標がjであることが可能か？ x方向の移動についてi回目の移動のあとに。
dpy[i][j]...y座標がjであることが可能か？ y方向の移動についてi回目の移動のあとに。

更新則
xについての移動,距離d移動するとすると
dpx[i+1][j+d]=dpx[i+1][j+d] or dpx[i][j]
dpx[i+1][j-d]=dpx[i+1][j-d] or dpx[i][j]
(配るDP)で更新すると楽かな？
yも同様

初期条件
dpx[:][:]=False
dpx[0][d_x0]=True 

dpy[:][:] = False
dpy[0][d_y0]=True 簡単のために
dpy[0][-d_y0]=True
'''

try:
    if hist_x:
        x_range = 2 * (sum(hist_x) + 1500)  
        shift = x_range // 2
        dpx = [[False] * x_range for _ in range(len(hist_x))]
        dpx[0][hist_x[0] + shift] = True
        for i in range(1, len(hist_x)):
            d = hist_x[i]
            for j in range(x_range):
                if j + d < x_range:
                    dpx[i][j] = dpx[i][j] or dpx[i - 1][j + d]
                if -1 < j - d:
                    dpx[i][j] = dpx[i][j] or dpx[i - 1][j - d]
        ansx = dpx[-1][x + shift]
    
    

    if hist_y:
        y_range = 2 * (sum(hist_y) + 1500)  
        shift = y_range // 2
        dpy = [[False] * y_range for _ in range(len(hist_y))]
        dpy[0][hist_y[0] + shift] = True
        dpy[0][-hist_y[0] + shift] = True
        for i in range(1, len(hist_y)):
            d = hist_y[i]
            for j in range(y_range):
                if j + d < y_range:
                    dpy[i][j] = dpy[i][j] or dpy[i - 1][j + d]
                if -1 < j - d:
                    dpy[i][j] = dpy[i][j] or dpy[i - 1][j - d]
        ansy = dpy[-1][y + shift]
    else:
        ansy = y == 0

    print('Yes' if ansx and ansy else 'No')
except:
    print('No')

Solution (Python):
import numpy as np

s = input()
x_goal, y_goal = map(int, input().split())

directed_x = None
steps = 0
xs = [0]
ys = [0]
for c in (s+'T'):   
    if c == 'F':
        steps += 1
    else:
        if directed_x is None:
            x_goal -= steps
            directed_x = True
        elif steps != 0:
            if directed_x:
                xs.append(steps)
            else:
                ys.append(steps)
        steps = 0
        directed_x = not directed_x

dx_2 = sum(xs) - x_goal
if dx_2 % 2 == 1 or dx_2 < 0:
    ans = False
else:
    dx = dx_2 // 2
    possible_x = np.full(dx + 1, False)
    possible_x[0] = True
    for x in xs:
        possible_x[x:] = np.logical_or(possible_x[:len(possible_x)-x], possible_x[x:])
        if possible_x[dx]:
            ans = True
            break
    else:
        ans = False

if ans:
    dy_2 = sum(ys) - y_goal
    if dy_2 % 2 == 1 or dy_2 < 0:
        ans = False
    else:
        dy = dy_2 // 2
        possible_y = np.full(dy + 1, False)
        possible_y[0] = True
        for y in ys:
            possible_y[y:] = np.logical_or(possible_y[:len(possible_y)-y], possible_y[y:])
            if possible_y[dy]:
                ans = True
                break
        else:
            ans = False

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

Solution (Python):
s = input()
x,y = map(int, input().split())
start = 0
key = len(s)
way = 0
dist = 0
now = 0
now_x = set([0])
now_y = set([0])
for k,i in enumerate(s):
  if i == "T":
    key = k
    break
  start += 1
x -= start
for i in s[key:]:
  if i == "T":
    if now:
      now = 0
      if way:
        new_y = set()
        for i in now_y:
          new_y.add(i+dist)
          new_y.add(i-dist)
        now_y = new_y
      else:
        new_x = set()
        for i in now_x:
          new_x.add(i+dist)
          new_x.add(i-dist)
        now_x = new_x
      dist = 0
    way ^= 1
  else:
    now = 1
    dist += 1
if way:
  new_y = set()
  for i in now_y:
    new_y.add(i+dist)
    new_y.add(i-dist)
    now_y = new_y
else:
  new_x = set()
  for i in now_x:
    new_x.add(i+dist)
    new_x.add(i-dist)
    now_x = new_x
if (x in now_x) and (y in now_y):
  print("Yes")
else:
  print("No")

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    s=list(input())+['T']
    x,y=map(int,input().split())
    x+=16000
    y+=16000
    ud=[]
    lr=[]
    f=0
    f2=0
    c=0
    for t in s:
        if t=='F':
            if not f:
                x-=1
            else:
                c+=1
        else:
            f=1
            if c:
                if f2:
                    ud.append(c)
                else:
                    lr.append(c)
            c=0
            f2=not f2
    for l,z in [(ud,y),(lr,x)]:
        N=len(l)
        dp=[0]*(N+1)
        dp[0]=1<<16000
        for i in range(N):
            dp[i+1]|=dp[i]<<l[i]|dp[i]>>l[i]
        if not (dp[-1]>>z)&1:
            print('No')
            break
    else:
        print('Yes')
        
    

if __name__ == '__main__':
    main()

Solution (Python):
s = str(input())
x,y = map(int,input().split())
x_list,y_list = [],[]
l = s.split('T')

for i in range(0,len(l),2):
  x_list.append(len(l[i]))
for i in range(1,len(l),2):
  y_list.append(len(l[i]))

ans_x,ans_y = set(),set()

if s[0]=='F':
  ans_x.add(x_list[0])
  x_list=x_list[1:]
else:
  ans_x.add(0)
ans_y.add(0)

for i in x_list:
  ans = set()
  for j in ans_x:
    ans.add(i+j)
    ans.add(j-i)
  ans_x = ans
for i in y_list:
  ans = set()
  for j in ans_y:
    ans.add(i+j)
    ans.add(j-i)
  ans_y = ans
if x in ans_x and y in ans_y:
  print("Yes")
else:
  print("No")

Solution (Python):
from itertools import product

S=input()
x,y=map(int,input().split())
L=[len(i) for i in S.split("T")]

X={L[0]}
Y={0}
for i in L[2::2]:
    X={j+k for j,k in product(X,[-i,i])}
for i in L[1::2]:
    Y={j+k for j,k in product(Y,[-i,i])}

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

Solution (Python):



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

cnt_T = 0
cnt = 0

step_X = []
step_Y = []
sx = 0
sy = 0
for s in S:
    if s == "F":
        cnt += 1
    else:
        if cnt_T == 0:
            sx = cnt
            cnt = 0
            cnt_T += 1
        elif cnt_T % 2 == 0:
            step_X.append(cnt)
            cnt = 0
            cnt_T += 1
        else:
            step_Y.append(cnt)
            cnt = 0
            cnt_T += 1



if cnt_T == 0:
    sx = cnt
    cnt = 0
    cnt_T += 1
elif cnt_T % 2 == 0:
    step_X.append(cnt)
    cnt = 0
    cnt_T += 1
else:
    step_Y.append(cnt)
    cnt = 0
    cnt_T += 1


dp_X = set()
dp_X.add(sx)
for i in step_X:
    tmp = set()
    for x in dp_X:
        tmp.add(x + i)
        tmp.add(x - i)
    dp_X = tmp

dp_Y = set()
dp_Y.add(sy)
for i in step_Y:
    tmp = set()
    for y in dp_Y:
        tmp.add(y + i)
        tmp.add(y - i)
    dp_Y = tmp

if X in dp_X and Y in dp_Y:
    print("Yes")
else:
    print("No")

Solution (Python):
s = list(input())
x,y = map(int,input().split())
i = 0
while i < len(s) and s[i] == "F":
    i += 1
    x -= 1

l = []
r = []
check = 1
count = 0
while i < len(s):
    if s[i] == "T":
        if count >0:
            if check == 1:
                l.append(count)
            else:
                r.append(count)
        check *= -1
        count = 0
    else:
        count += 1
    i += 1

if count >0:
    if check == 1:
        l.append(count)
    else:
        r.append(count)       

    
if abs(x) > sum(l) or abs(y) > sum(r):
    print("No")
    exit() 

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

dpy = [[0]*(sum(r)*2+3) for i in range(len(r)+1)]
rx = sum(r)*2+3
dpy[0][sum(r)] = 1
for i in range(len(r)):
    for j in range(rx):
        if dpy[i][j] == 1:
            dpy[i+1][j+r[i]] = 1
            dpy[i+1][j-r[i]] = 1
if dpx[-1][sum(l)+x] == 1 and dpy[-1][sum(r)+y] == 1:
    print("Yes")
else:
    print("No")

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

dp_x = [[False, False] for i in range(2*n+2)]
dp_y = [[False, False] for i in range(2*n+2)]
dp_x[0][0] = True
dp_y[0][0] = True
dp_y[0][1] = True

flag = 0
for i in range(len(s)):
    if s[i] == "T":
        if flag == 0:
            flag = 2
            for j in range(-i-1, i+2):
                if dp_x[j][0]:
                    dp_x[j][1] = True

        elif flag == 2:
            flag = 1
            for j in range(-i-1, i+2):
                dp_x[j][0] = dp_x[j][1] or dp_x[j][0]
                dp_x[j][1] = dp_x[j][1] or dp_x[j][0]

        else:
            flag = 2
            for j in range(-i-1, i+2):
                dp_y[j][0] = dp_y[j][1] or dp_y[j][0]
                dp_y[j][1] = dp_y[j][1] or dp_y[j][0]


    else:
        if flag == 0:
            for j in range(i+1, -i-2, -1):
                dp_x[j][0] = dp_x[j-1][0]

        elif flag == 1:
            for j in range(i+1, -i-2, -1):
                dp_x[j][0] = dp_x[j-1][0]
                dp_x[-j][1] = dp_x[-j+1][1]
        else:
            for j in range(i+1, -i-2, -1):
                dp_y[j][0] = dp_y[j-1][0]
                dp_y[-j][1] = dp_y[-j+1][1]


if dp_x[x][0] or dp_x[x][1]:
    if dp_y[y][0] or dp_y[y][1]:
        print("Yes")
    else:
        print("No")
else:
    print("No")

Solution (Python):
from itertools import product
s = input()
x, y = map(int,input().split())
L = [len(i) for i in s.split('T')]
X = {L[0]}
Y = {0}
for i in L[2::2]:
    X = {j + k for j, k in product(X, [-i, i])}
for i in L[1::2]:
    Y = {j + k for j, k in product(Y, [-i, i])}
if x in X and y in Y:
    print('Yes')
else:
    print('No')

Solution (Python):
def main():
    s = input()
    x, y = map(int, input().split())
    d = {}
    d[True] = {0}
    d[False] = {0}
    xf = True
    t = 0
    first = True
    for i in s:
        if i == 'F':
            t += 1
        else:
            if first:
                d[xf] = set()
                d[xf].add(t)
                first = False
            else:
                tmp = set()
                for j in d[xf]:
                    tmp.add(j-t)
                    tmp.add(j+t)
                d[xf] = tmp
            xf = not xf
            t = 0
    if t == len(s):
        d[True] = {t}
        d[False] = {0}
    elif t > 0:
        tmp = set()
        for j in d[xf]:
            tmp.add(j-t)
            tmp.add(j+t)
        d[xf] = tmp
    if x in d[True] and y in d[False]:
        return True
    return False
if main():
    print('Yes')
else:
    print('No')

Solution (Python):
def main():
    S = input()
    X, Y = map(int, input().split())
    d = 0
    L = [[0], [0]]
    for s in S:
        if s == 'T':
            d = 1 - d
            if L[d][-1] != 0:
                L[d].append(0)
        else:
            L[d][-1] += 1
    xs, ys = set([L[0][0] if S[0] == 'F' else 0]), set([0])
    for i in L[0][1 if S[0] == 'F' else 0:]:
        xs = set(x + i for x in xs) | set(x - i for x in xs)
    for i in L[1]:
        ys = set(y + i for y in ys) | set(y - i for y in ys)
    return X in xs and Y in ys

print('Yes' if main() else 'No')

Solution (Python):
def main():
    s = input()
    x, y = map(int, input().split())
    d = {}
    d[True] = []
    d[False] = []
    xf = True
    t = 0
    for i in s:
        if i == 'F':
            t += 1
        else:
            d[xf].append(t)
            xf = not xf
            t = 0
    if t > 0:
        d[xf].append(t)
    dpx = set()
    dpx.add(d[True][0])
    for i in d[True][1:]:
        dnew = set()
        for j in dpx:
            dnew.add(-i+j)
            dnew.add(i+j)
        dpx = dnew
    if x not in dpx:
        return False
    dpy = set()
    dpy.add(0)
    for i in d[False]:
        dnew = set()
        for j in dpy:
            dnew.add(-i+j)
            dnew.add(i+j)
        dpy = dnew
    if y not in dpy:
        return False
    return True
if main():
    print('Yes')
else:
    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()
for i in range(len(sumS)):
  if sumS[i][0] == "F":
    steps = sumS[i][1] 
    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 sumS[i][0] == "T":
    turns = sumS[i][1] 
    if turns % 2 == 1:
      
      xdir ^= True
      
if X in dpXcur and Y in dpYcur:
  print("Yes")
else:
  print("No")

Solution (Python):
import sys

def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))

def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]

MOD=10**9+7
def divc(x,y): return -(-x//y)
def divf(x,y): return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers"""
    is_prime = [True] * MAX_NUM
    is_prime[0] = False
    is_prime[1] = False
    primes = []
    for i in range(MAX_NUM):
        if is_prime[i]:
            primes.append(i)
            for j in range(2*i, MAX_NUM, i):
                is_prime[j] = False
    return primes

from itertools import accumulate as acc
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left



def main():
    s = IS()
    x, y = MII()
    ff = s.split('T')
    ffx = [len(f) for f in ff[::2]]
    ffy = [len(f) for f in ff[1::2]]

    nx = ffx.pop(0)
    ny = 0

    ffx.sort(reverse=True)
    ffy.sort(reverse=True)

    for fx in ffx:
        if x >= nx: nx += fx
        else: nx -= fx
    for fy in ffy:
        if y >= ny: ny += fy
        else: ny -= fy

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

if __name__ == '__main__':
    main()

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

if S.find("F") < 0:
    if X != 0 or Y != 0:
        print("No")
    else:
        print("Yes")
    exit()

n = len(S)
d = list(map(len, S.split("T")))
xs = d[::2]
ys = d[1::2]

dpx = [set() for _ in range(len(xs))]
for i in range(len(xs)):
    if i == 0:
        d = (-1) ** S.find("F")
        dpx[i].add(d * xs[i] + n)
        continue
    
    for j in dpx[i - 1]:
        dpx[i].add(j + xs[i])
        dpx[i].add(j - xs[i])

dpy = [set() for _ in range(len(ys) + 1)]
dpy[0].add(n)
for i in range(len(ys)):
    for j in dpy[i]:
        dpy[i + 1].add(j + ys[i])
        dpy[i + 1].add(j - ys[i])

ans = (X + n in dpx[-1]) & (Y + n in dpy[-1])
print("Yes" if ans else "No")

Solution (Python):
s = input()
x,y = map(int,input().split())
cur_x = 0
cur_y = 0
L_x = []
L_y = []
s += 'K'
temp = 0
dirc = 'yoko'
for i in range(len(s)):
    if s[i] == 'F':
        temp += 1
    elif s[i] == 'T':
        if temp == 0:
            if dirc == 'yoko':
                dirc = 'tate'
            else:
                dirc = 'yoko'
        else:
            if dirc == 'yoko':
                L_x.append(temp)
                temp = 0
                dirc = 'tate'
            else:
                L_y.append(temp)
                temp = 0
                dirc = 'yoko'
    else:
        if temp != 0:
            if dirc == 'yoko':
                L_x.append(temp)
                temp = 0
                dirc = 'tate'
            else:
                L_y.append(temp)
                temp = 0
                dirc = 'yoko'
dp_x = [[False]*8001 for i in range(len(L_x)+1)]
dp_y = [[False]*8001 for i in range(len(L_y)+1)]
dp_x[0][0] = True
dp_y[0][0] = True
if s[0] != 'F':
    for i in range(1,len(L_x)+1):
        for j in range(8001):
            if j+L_x[i-1] <= 8000:
                dp_x[i][j] = dp_x[i-1][abs(j-L_x[i-1])] or dp_x[i-1][j+L_x[i-1]]
            else:
                dp_x[i][j] = dp_x[i-1][abs(j-L_x[i-1])]
    for i in range(1,len(L_y)+1):
        for j in range(8001):
            if j+L_y[i-1] <= 8000:
                dp_y[i][j] = dp_y[i-1][abs(j-L_y[i-1])] or dp_y[i-1][j+L_y[i-1]]
            else:
                dp_y[i][j] = dp_y[i-1][abs(j-L_y[i-1])]
    if dp_x[len(L_x)][abs(x)] == True and dp_y[len(L_y)][abs(y)] == True:
        print('Yes')
    else:
        print('No')
else:
    for i in range(2,len(L_x)+1):
        for j in range(8001):
            if j+L_x[i-1] <= 8000 and abs(j-L_x[i-1]) <= 8000:
                dp_x[i-1][j] = dp_x[i-2][abs(j-L_x[i-1])] or dp_x[i-2][j+L_x[i-1]]
            elif abs(j-L_x[i-1]) <= 8000:
                dp_x[i-1][j] = dp_x[i-2][abs(j-L_x[i-1])]
    for i in range(1,len(L_y)+1):
        for j in range(8001):
            if j+L_y[i-1] <= 8000 and abs(j-L_y[i-1]) <= 8000:
                dp_y[i][j] = dp_y[i-1][abs(j-L_y[i-1])] or dp_y[i-1][j+L_y[i-1]]
            elif abs(j-L_y[i-1]) <= 8000:
                dp_y[i][j] = dp_y[i-1][abs(j-L_y[i-1])]
    if abs(x-L_x[0]) <= 8000:
        if dp_x[len(L_x)-1][abs(x-L_x[0])] == True and dp_y[len(L_y)][abs(y)] == True:
            print('Yes')
        else:
            print('No')
    else:
        print('No')

Solution (Python):
import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]
def init_dp1(init, N): return [init for _ in range(N)]
def init_dp2(init, N, M): return [[init for _ in range(M)] for _ in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n) 


def main():
    S = input().strip()
    X, Y = read_values()
    F = list(map(len, S.split("T")))

    S = {F[0]}
    if len(F) >= 2:
        for x in F[2::2]:
            SS = set()
            for s in S:
                SS.add(s + x)
                SS.add(s - x)
            S = SS

    if not X in S:
        print("No")
        return
    
    S = {0}
    if len(F) >= 1:
        for x in F[1::2]:
            SS = set()
            for s in S:
                SS.add(s + x)
                SS.add(s - x)
            S = SS
    
    if not Y in S:
        print("No")
        return 
    
    print("Yes")


if __name__ == "__main__":
    main()

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

direction_x=True
xlist,ylist=[],[]
cnt=0
for s in slist:
  if s=="F":
    cnt+=1
  else:
    if direction_x:
      xlist.append(cnt)
    else:
      ylist.append(cnt)
    cnt=0     
    direction_x=not direction_x
else:
  if direction_x:
    xlist.append(cnt)
  else:
    ylist.append(cnt)

x-=xlist.pop(0)



set_x={0}
for i in range(len(xlist)):
  new_set_x=set()
  for xx in set_x:
    new_set_x.add(xx+xlist[i])
    new_set_x.add(xx-xlist[i])
  set_x=new_set_x
    
set_y={0}
for i in range(len(ylist)):
  new_set_y=set()
  for yy in set_y:
    new_set_y.add(yy+ylist[i])
    new_set_y.add(yy-ylist[i])
  set_y=new_set_y


if x in set_x and y in set_y:
  print("Yes")
else:
  print("No")

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

vec=[(1,0),(0,1),(-1,0),(0,-1)]

direction_x=True
xlist=[]
ylist=[]
cnt=0
for s in slist:
  if s=="F":
    cnt+=1
  else:
    if direction_x:
      xlist.append(cnt)
    else:
      ylist.append(cnt)
    cnt=0     
    direction_x=not direction_x
else:
  if direction_x:
    xlist.append(cnt)
  else:
    ylist.append(cnt)

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



dp_x=[]
for _ in range(len(xlist)+1):
  dp_x.append(set())
dp_x[0]={0}
for i in range(1,len(xlist)+1):
  for j in dp_x[i-1]:
    dp_x[i].add(j+xlist[i-1])
    dp_x[i].add(j-xlist[i-1])


dp_y=[]
for _ in range(len(ylist)+1):
  dp_y.append(set())
dp_y[0]={0}
for i in range(1,len(ylist)+1):
  for j in dp_y[i-1]:
    dp_y[i].add(j+ylist[i-1])
    dp_y[i].add(j-ylist[i-1])



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

Solution (Python):
"""Knapsack"""
import sys

sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
s = input().rstrip()
gx, gy = map(int, input().split())
rx, ry = 0, 0
head, inst = 0, []
while head < len(s) and s[head] == "F":
    head += 1
    rx += 1

xlist, ylist = [], []
heading_x = True
while head < len(s):
    tcnt = 0
    while head < len(s) and s[head] == "T":
        tcnt += 1
        head += 1
    if head == len(s):
        break
    fcnt = 0
    while head < len(s) and s[head] == "F":
        fcnt += 1
        head += 1
    if ((tcnt % 2) == 1) ^ heading_x:
        heading_x = True
        xlist.append(fcnt)
    else:
        heading_x = False
        ylist.append(fcnt)


def fill(fs, goal_offset):
    f_total = sum(fs)
    if f_total < goal_offset:
        return False
    if f_total == goal_offset:
        return True
    f_diff = f_total - goal_offset
    if f_diff % 2 == 1:
        return False
    f_diff //= 2
    diffs = {0}
    for f in fs:
        for d in list(diffs):
            d2 = f + d
            if d2 > f_diff:
                continue
            if d2 == f_diff:
                return True
            diffs.add(d2)
    return False


if fill(xlist, abs(gx - rx)) and fill(ylist, abs(gy - ry)):
    print("Yes")
else:
    print("No")

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

x_mov = []
y_mov = []

s += "T"
flag = 'x'
mov = 0
for c in s:
    if c=='F':
        mov += 1
    elif flag == 'x':
        flag = 'y'
        x_mov.append(mov)
        mov = 0
    else:
        flag = 'x'
        y_mov.append(mov)
        mov = 0

def get_pos(pos,mov_set):
    for i in mov_set:
        new_pos = set()
        for j in pos:
            new_pos.add(j+i)
            new_pos.add(j-i)
        pos = new_pos
    return pos

if s[0] =="T":
    x_pos = get_pos([0],x_mov)
else:
    x_pos = get_pos([x_mov[0]],x_mov[1:])
y_pos = get_pos([0],y_mov)

print("Yes" if x in x_pos and y in y_pos else "No")