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())
y = abs(y)

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

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

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

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


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

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


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

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

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

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

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

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


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

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


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

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

dpx = [0 for i in range(17000)]
dpy = [0 for i in range(17000)]

dpx[8500] = 1
dpy[8500] = 1

f = s.split("T")

fx = []
fy = []

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

for i, fxi in enumerate(fx):
    nex = [0] * 17000
    for j in range(17000):
        if dpx[j]:
            if fxi + j < 17000:
                nex[fxi + j] = 1
            if i > 0 and j - fxi >= 0:
                nex[j - fxi] = 1
    dpx = nex
        
for fyi in fy:
    nex = [0] * 17000
    for j in range(17000):
        if dpy[j]:
            if fyi + j < 17000:
                nex[fyi + j] = 1
            if j - fyi >= 0:
                nex[j - fyi] = 1
    dpy = nex

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

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

dpx = {0}
dpy = {0}

f = s.split("T")

fx = []
fy = []

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

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

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

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

dpx = [0 for i in range(17000)]
dpy = [0 for i in range(17000)]

dpx[8500] = 1
dpy[8500] = 1

f = s.split("T")

fx = []
fy = []

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

for i, fxi in enumerate(fx):
    nex = [0] * 17000
    for j in range(17000):
        if dpx[j]:
            if fxi + j < 17000:
                nex[fxi + j] = 1
            if i > 0 and j - fxi >= 0:
                nex[j - fxi] = 1
    dpx = nex[:]
        
for fyi in fy:
    nex = [0] * 17000
    for j in range(17000):
        if dpy[j]:
            if fyi + j < 17000:
                nex[fyi + j] = 1
            if j - fyi >= 0:
                nex[j - fyi] = 1
    dpy = nex[:]

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

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


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

xs = []
ys = []
isx = True
i = 0
n = len(s)
while i<n and s[i]=="F":
    x -= 1
    i += 1

while i<n:
    c = 0
    while i<n and s[i]=="F":
        i += 1
        c += 1
    if isx and c>0:
        xs.append(c)
    elif c>0:
        ys.append(c)
    isx = not isx
    i += 1
    
def sub(l, target):
    
    val = sum(l)
    prev = [False] * (2*(val+1))
    prev[val] = True
    current = None
    for num in l:
        current = [False] * (2*(val+1))
        for i in range(2*(val+1)):
            if i-num>=0 and prev[i-num]:
                current[i] = True
            if i+num<(2*val+2) and prev[i+num]:
                current[i] = True
        prev = current
    if current is None:
        current = prev
    return current[target+val]

if sum(abs(item) for item in xs)>=abs(x) and sum(abs(item) for item in ys)>=abs(y) and sub(xs, x) and sub(ys, y):
    print("Yes")
else:
    print("No")

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


if 'T' not in s:
  if len(s)==x and y==0:
    print('Yes')
  else:
    print('No')
else:
  move=s.split('T')
  xmove=[len(move[2*i+2]) for i in range((len(move)-1)//2)]
  ymove=[len(move[2*i+1]) for i in range(len(move)//2)]
  import numpy as np
  
  ablex=np.full(16001,False)
  abley=np.full(16001,False)
  ablex[8000+len(move[0])]=True
  abley[8000]=True
  for xi in xmove:
    if xi==0:continue
    ax=np.full(16001,False)
    ax[xi:]+=ablex[:-xi]
    ax[:-xi]+=ablex[xi:]
    ablex=ax
  for yi in ymove:
    if yi==0:continue
    ay=np.full(16001,False)
    ay[yi:]+=abley[:-yi]
    ay[:-yi]+=abley[yi:]
    abley=ay
  if ablex[8000+x] and abley[8000+y]:
    print('Yes')
  else:
    print('No')

Solution (Python):
def resolve():
    S = input()
    X, Y = list(map(int, input().split()))
    insts = []
    cnt = 1
    series = S[0]

    for i in range(1, len(S)):
        if series == S[i]:
            cnt += 1
        else:
            insts.append((series, cnt))
            cnt = 1
            series = S[i]
    insts.append((series, cnt))
    
    xmoves = []
    ymoves = []
    to_x = True
    for inst in insts:
        if inst[0] == "F":
            if to_x:
                xmoves.append(inst[1])
            else:
                ymoves.append(inst[1])
        else:
            if inst[1]%2 == 1:
                to_x = not to_x
    
    
    cands = set()
    cands.add(0)
    for i, x in enumerate(xmoves):
        _cands = set()
        while cands:
            xpos = cands.pop()
            _cands.add(xpos+x)
            if not (i == 0 and insts[0][0] == "F"):
                _cands.add(xpos-x)
        cands = _cands
    if X not in cands:
        print("No")
        return

    cands = set()
    cands.add(0)
    for y in ymoves:
        _cands = set()
        while cands:
            ypos = cands.pop()
            _cands.add(ypos+y)
            _cands.add(ypos-y)
        cands = _cands
    if Y not in cands:
        print("No")
        return
    
    print("Yes")

if '__main__' == __name__:
    resolve()

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    s = input()
    x, y = map(int, readline().split())
    sx, sy = 0, 0

    for i in range(len(s)):
        if s[i] == "F":
            sx += 1
        else:
            break

    is_x = False
    cur = 0

    x_list = []
    y_list = []

    for i in range(sx + 1, len(s)):
        if s[i] == "F":
            cur += 1
        if s[i] == "T" or i == len(s) - 1:
            if is_x:
                x_list.append(cur)
            else:
                y_list.append(cur)
            cur = 0
            is_x = not is_x

    x_possible = set()
    x_possible.add(0)

    while x_list:
        x_possible_new = set()
        cx = x_list.pop()
        for px in x_possible:
            x_possible_new.add(px + cx)
            x_possible_new.add(px - cx)
        x_possible = x_possible_new

    y_possible = set()
    y_possible.add(0)

    while y_list:
        y_possible_new = set()
        cy = y_list.pop()
        for py in y_possible:
            y_possible_new.add(py + cy)
            y_possible_new.add(py - cy)
        y_possible = y_possible_new

    nx = x - sx
    ny = y

    if (nx in x_possible) and (ny in y_possible):
        print("Yes")
    else:
        print("No")



if __name__ == '__main__':
    main()

Solution (Python):
import math



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

    
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ZipArray(a):
    aa = [[a[i],i]for i in range(n)]

    aa.sort(key = lambda x : x[0])
    for i in range(n):
        aa[i][0]=i+1
    aa.sort(key = lambda x : x[1])
    b=[aa[i][0] for i in range(len(a))]
    return b

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans

def Zeros(a,b):
    if(b<=-1):
        return [0 for i in range(a)]
    else:
        return [[0 for i in range(b)] for i in range(a)]

def AddV2(v,w):
    return [v[0]+w[0],v[1]+w[1]]

dir4 = [[1,0],[0,1],[-1,0],[0,-1]]


def clamp(x,y,z):
    return max(y,min(z,x))

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
            

def Zaatsu(a):
    a.sort()
    now = a[0][0]
    od = 0
    for i in range(n):
        if(now==a[i][0]):
            a[i][0]=od
        else:
            now = a[i][0]
            od+=1
            a[i][0] = od
    
    a.sort(key = lambda x : x[1])
    return a

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


'''
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 2
N = 10 ** 6 + 2
fact = [1, 1]  
factinv = [1, 1]  
inv = [0, 1]  

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

'''

def rl(x):
    return range(len(x))










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

now = 0
right = 0
while(True):
    if(len(s)==now):
        break
    if(s[now]=='T'):
        break
    right+=1
    now+=1

x -= right
chain = 0
xy = 0
seg = [[],[]]
while(True):
    if(len(s)==now):
        break
    if(s[now]=='T'):
        
        if(chain>0):
            seg[xy].append(chain)
            chain=0
        xy = 1-xy
    else:
        chain+=1
    now+=1

if(chain>0):
    seg[xy].append(chain)




n = len(seg[0])
m = sum(seg[0])

if(abs(x)>m):
    print("No")
    exit()

dp=[[0 for i in range(m+1)] for i in range(n+1)]
dp[0][0]=1

for i in range(n):
    nowlen = seg[0][i]
    for j in range(m+1):
        if(dp[i][j]==1):
            if(j+nowlen<=m):
                dp[i+1][j+nowlen]=1
            if(j-nowlen>=0):
                dp[i+1][j-nowlen]=1
                

if(dp[-1][abs(x)]==0):
    print("No")
    exit()

n = len(seg[1])
m = sum(seg[1])

if(abs(y)>m):
    print("No")
    exit()

dp=[[0 for i in range(m+1)] for i in range(n+1)]
dp[0][0]=1

for i in range(n):
    nowlen = seg[1][i]
    for j in range(m+1):
        if(dp[i][j]==1):
            if(j+nowlen<=m):
                dp[i+1][j+nowlen]=1
            if(j-nowlen>=0):
                dp[i+1][j-nowlen]=1
                

if(dp[-1][abs(y)]==0):
    print("No")
    exit()

print("Yes")

Solution (Python):
S = input()
gx, gy = map(int, input().split())
SL = S.split('T')
D = [len(sli) for sli in SL]
DX = D[2::2]
DY = D[1::2]

def reachable(D, s, g):
    dp = [{} for _ in range(len(D)+1)]
    dp[0][s] = 1
    for i in range(len(D)):
        d = D[i]
        for j in dp[i]:
            dp[i+1][j+d] = 1
            dp[i+1][j-d] = 1
    return g in dp[-1]

if reachable(DX, D[0], gx) and reachable(DY, 0, gy):
    print('Yes')
else:
    print('No')

Solution (Python):
k = input()
s = list(map(len, k.split("T")))
x, y = map(int, input().split())
x_list = []
y_list = []
check = 1
for i in range(len(s)):
    if check == 1:
        x_list.append(s[i])
        check = -1
    else:
        y_list.append(s[i])
        check = 1

now_x = [x_list[0]]
for move in x_list[1:]:
    nxt = set()
    for i in now_x:
        nxt.add(i + move)
        nxt.add(i - move)
    now_x = nxt
    
now_y = [0]
for move in y_list:
    nxt = set()
    for i in now_y:
        nxt.add(i + move)
        nxt.add(i - move)
    now_y = nxt

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

Solution (Python):
s=input()
x,y=map(int,input().split())
xpossible=[0]
ypossible=[0]
j=0
while s[j]=='F':
  j+=1
  xpossible[0]+=1
  if j==len(s):
    break
counter=0
i=j
while i<len(s):
  current=0
  while s[i]=='F':
    i+=1
    current+=1
    if i==len(s):
      break
  r=set()
  if counter==0:
    for j in xpossible:
      r.add(j+current)
      r.add(j-current)
    xpossible=[]
    for j in r:
      xpossible.append(j)
  else:
    for j in ypossible:
      r.add(j+current)
      r.add(j-current)
    ypossible=[]
    for j in r:
      ypossible.append(j)
  if i==len(s):
    break
  current=0
  while s[i]=='T':
    i+=1
    current+=1
    if i==len(s):
      break
  counter=(counter+current)%2
if x in xpossible and y in ypossible:
  print('Yes')
else:
  print('No')

Solution (Python):
s=input()
x,y=map(int,input().split())
ss=[len(i) for i in s.split("T")]
x-=ss[0]
a=[]
b=[]
for i in range(1,len(ss)):
  if ss[i]==0:continue
  if i%2==1:b.append(ss[i])
  else:a.append(ss[i])
x=abs(x)
y=abs(y)
if max(x,y)>8000:exit(print("No"))
def check(n,a):
  dp=[[0]*20001 for _ in range(len(a)+1)]
  n+=10000
  dp[0][10000]=1
  for i in range(1,len(a)+1):
    for j in range(20001):
      if j+a[i-1]<=20000:dp[i][j+a[i-1]]|=dp[i-1][j]
      if j-a[i-1]>=0:dp[i][j-a[i-1]]|=dp[i-1][j]
  if dp[len(a)][n]==1:return True
  return False
if check(x,a) and check(y,b):print("Yes")
else:print("No")

Solution (Python):
s = input()
gx,gy = map(int, input().split())

x_lis=[]
y_lis=[]

cnt=0
now="x"
for i in range(len(s)):
    if s[i]=="F":
        cnt+=1
    else:
        if now=="x":
            x_lis.append(cnt)
            now="y"
        else:
            y_lis.append(cnt)
            now="x"
        cnt=0

if now=="x":
    x_lis.append(cnt)
    now="y"
else:
    y_lis.append(cnt)
    now="x"

now=set([0])
for i in range(len(x_lis)):
    nxt=set()
    for itm in now:
        nxt.add(itm + x_lis[i])
        if i!=0:
            nxt.add(itm - x_lis[i])
    now=nxt

if not gx in now:
    print("No")
    exit()

now=set([0])
for i in range(len(y_lis)):
    nxt=set()
    for itm in now:
        nxt.add(itm + y_lis[i])
        nxt.add(itm - y_lis[i])
    now=nxt

if not gy in now:
    print("No")
else:
    print("Yes")

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

    x1 = []
    y1 = []
    cnt = 0
    cnt2 = 0

    for i in range(len(s)):
        s1 = s[i]
        if s1 == 'F':
            cnt += 1
        else:
            if cnt2 == 0:
                x -= cnt
            elif cnt2 % 2 == 0:
                x1.append(cnt)
            else:
                y1.append(cnt)
            cnt = 0
            cnt2 += 1

    if cnt2 == 0:
        x -= cnt

    if cnt > 0 and cnt2 > 0:
        if cnt2 % 2 == 0:
            x1.append(cnt)
        else:
            y1.append(cnt)

    if len(x1) == 0 and len(y1) == 0 and x == 0 and y == 0:
        print('Yes')
        exit()

    if sum(x1) < abs(x) or sum(y1) < abs(y):
        print('No')
        exit()

    dp1 = [[False] * (sum(x1)+1)*2 for _ in range((len(x1)+1))]
    dp2 = [[False] * (sum(y1)+1)*2 for _ in range((len(y1)+1))]
    dp1[0][sum(x1)+1] = True
    dp2[0][sum(y1)+1] = True

    for i in range(len(x1)):
        for j in range((sum(x1)+1)*2):
            if dp1[i][j]:
                dp1[i+1][j+x1[i]] = True
                dp1[i+1][j-x1[i]] = True

    for i in range(len(y1)):
        for j in range((sum(y1)+1)*2):
            if dp2[i][j]:
                dp2[i+1][j+y1[i]] = True
                dp2[i+1][j-y1[i]] = True

    if dp1[-1][sum(x1)+1+x] and dp2[-1][sum(y1)+1+y]:
        print('Yes')
    else:
        print('No')

if __name__ == '__main__':
    main()

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

ver = []
hor = []
ini = 0
flag = 'h1'
tmp = 0

for i in range(len(s)):
    if s[i] == 'T':
        if flag == 'h':
            if tmp > 0:
                hor.append(tmp)
            tmp = 0
            flag = 'v'
        elif flag == 'v':
            if tmp > 0:
                ver.append(tmp)
            flag = 'h'
            tmp = 0
        elif flag == 'h1':
            if tmp > 0:
                ini = tmp
            flag = 'v'
            tmp = 0
    else:
        tmp += 1

if tmp > 0:
    if flag == 'h1':
        ini = tmp
    elif flag == 'h':
        hor.append(tmp)
    elif flag == 'v':
        ver.append(tmp)

num = 8000
        
ver_li = [[0 for i in range(2)] for j in range(num*2+1)]
hor_li = [[0 for i in range(2)] for j in range(num*2+1)]

hor_li[num+ini][0] = 1



for i in range(len(hor)):
    
    for j in range(num*2+1):
        if hor_li[j][0] == 1:
            
            hor_li[j][1] = 0
            hor_li[j+hor[i]][1] = 1
            hor_li[j-hor[i]][1] = 1
    for j in range(num*2+1):
        hor_li[j][0] = hor_li[j][1]

ver_li[num][0] = 1
for i in range(len(ver)):
    
    for j in range(num*2+1):
        if ver_li[j][0] == 1:
            ver_li[j][1] = 0
            
            ver_li[j+ver[i]][1] = 1
            ver_li[j-ver[i]][1] = 1
    for j in range(num*2+1):
        ver_li[j][0] = ver_li[j][1]
    
if hor_li[num+x][0] == 1 and ver_li[num+y][0] == 1:
    print('Yes')
else:
    print('No')

Solution (Python):
order = list(input())
X, Y = map(int, input().split())
 
Xs = []
Ys = []
cnt = 0
now_lis = Xs
x_dir = 1
for s in order:
    if s=='T':
        if not cnt==0:
            now_lis.append(cnt)
        now_lis = Ys if x_dir else Xs
        x_dir = not x_dir
        cnt = 0
    else:
        cnt += 1
if not cnt==0:
    now_lis.append(cnt)

if order[0]=='T':
    x_start = 0
else:
    x_start = Xs[0]
    del Xs[0]

def dp(start, lis, goal):
    nodes = set([start])
    for x in lis:
        nx = set()
        L = list(nodes)
        for l in L:
            nx.add(l+x)
            nx.add(l-x)
        nodes = nx
    return goal in nodes
 
if dp(x_start, Xs, X) and dp(0, Ys, Y):
    print('Yes')
else:
    print('No')

Solution (Python):
def main():
    *CMDs, = map(len, input().split('T'))
    X, Y = map(int, input().split())

    X -= CMDs[0]  

    def is_reachable(cmds, goal):
        SFT = 8000
        
        dp = 1 << SFT
        for d in cmds:
            dp = (dp >> d) | (dp << d)
        return SFT + goal >= 0 and (dp >> (SFT + goal)) & 1  

    cond = is_reachable(CMDs[2::2], X) and is_reachable(CMDs[1::2], Y)
    print('Yes' if cond else 'No')


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    SFT = 8000

    CMDs = map(len, input().split('T'))
    X, Y = map(int, input().split())

    
    axis = 0
    curr = [1 << (next(CMDs) + SFT), 1 << SFT]
    for d in CMDs:
        axis ^= 1
        curr[axis] = (curr[axis] >> d) | (curr[axis] << d)

    cond = (curr[0] >> (SFT + X)) & 1 and (curr[1] >> (SFT + Y)) & 1
    print('Yes' if cond else 'No')


if __name__ == '__main__':
    main()

Solution (Python):



def main():
    S = input()
    X, Y = map(int, input().split())
    Dxy = [[] for _ in range(2)]
    cnt = 0
    direc = 0
    for x in S:
        if x == 'F':
            cnt += 1
        else:
            Dxy[direc].append(cnt)
            cnt=0
            direc = 1-direc
    Dxy[direc].append(cnt)

    Dx = Dxy[0][1:]
    Dy = Dxy[1]
    dpx=[set([Dxy[0][0]]),set([])]
    dpy=[set([0]),set([])]
    i=1
    for dx in Dx:
        dpx[i]=set([])
        for x in dpx[1-i]:
            dpx[i].add(x+dx)
            dpx[i].add(x-dx)
        i = 1-i
    j=1
    for dy in Dy:
        dpy[j]=set([])
        for y in dpy[1-j]:
            dpy[j].add(y+dy)
            dpy[j].add(y-dy)
        j = 1-j

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

if __name__ == '__main__':
    main()

Solution (Python):


import sys


def main():
    def input(): return sys.stdin.readline().rstrip()
    def mi():    return map(int, input().split())


    def preprocess(s):
        x_command, y_command = [], []
        L = s.split('T')
        n = len(L)
        for i in range(n):
            if i % 2 == 0 and L[i]:
                x_command.append(L[i].count("F"))
            elif i % 2 == 1 and L[i]:
                y_command.append(L[i].count("F"))
        return x_command, y_command
    

    def valid_command(command, delta):
        
        if not command:
            return delta == 0
        possible_num_set = set()
        n = len(command)
        for i in range(n):
            if i == 0:
                possible_num_set.add(command[0])
                possible_num_set.add(-command[0])
            else:
                num = command[i]
                new_set = set()
                for candidate in possible_num_set:
                    new_set.add(candidate + num)
                    new_set.add(candidate - num)
                possible_num_set = new_set
        
        return delta in possible_num_set

    s = input()
    s.rstrip('T')
    n = len(s)
    x, y = mi()
    dx, dy = x, y
    tail_of_front_F = -1
    for i in range(n):
        if s[i] == 'F':
            tail_of_front_F = i
        else:
            break
    s = s[tail_of_front_F+1:]
    if tail_of_front_F >= 0:
        dx -= (tail_of_front_F + 1)
    

    x_command, y_command = preprocess(s)
    
    
    if valid_command(x_command, dx) and valid_command(y_command, dy):
        print("Yes")
    else:
        print("No")

    


if __name__ == "__main__":
    main()

Solution (Python):
from itertools import groupby
from sys import setrecursionlimit
setrecursionlimit(10 ** 7)

s = input()

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

d = [len(i) for i in s.split('T')]

x_li = d[2::2]
y_li = d[1::2]

now_x = [0]
now_y = [0]

x -= d[0]

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

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

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

Solution (Python):


import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    s = input()
    gx, gy = map(int, input().split())

    spl = list(map(len, s.split("T")))
    init_x = spl[0]
    is_x = 0
    to_x, to_y = [], []
    for i in range(1, len(spl)):
        if spl[i]:
            to_x.append(spl[i]) if is_x else to_y.append(spl[i])
        is_x ^= 1

    now_x = [init_x]
    for move in to_x:
        nx = set()
        for x in now_x:
            nx.add(x + move)
            nx.add(x - move)
        now_x = nx

    now_y = [0]
    for move in to_y:
        ny = set()
        for y in now_y:
            ny.add(y + move)
            ny.add(y - move)
        now_y = ny

    print("Yes" if gx in now_x and gy in now_y else "No")


if __name__ == '__main__':
    resolve()

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    s = input()
    gx, gy = map(int, input().split())

    spl = list(map(len, s.split("T")))
    init_x = spl[0]
    is_x = 0
    to_x, to_y = [], []
    for i in range(1, len(spl)):
        to_x.append(spl[i]) if is_x else to_y.append(spl[i])
        is_x ^= 1

    now_x = [init_x]
    for move in to_x:
        nx = set()
        for x in now_x:
            nx.add(x + move)
            nx.add(x - move)
        now_x = nx

    now_y = [0]
    for move in to_y:
        ny = set()
        for y in now_y:
            ny.add(y + move)
            ny.add(y - move)
        now_y = ny

    print("Yes" if gx in now_x and gy in now_y else "No")


if __name__ == '__main__':
    resolve()

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    s=readline().strip()
    x,y=map(int,readline().split())
    n=len(s)

    now_x={0}
    now_y={0}
    i=0
    first=True
    flag="x"
    cnt=0
    while i<n:
        if s[i]=="F":
            cnt+=1
            i+=1
            if i==n:
                nex=set()
                if flag=="x":
                    l=len(now_x)
                else:
                    l=len(now_y)
                for _ in range(l):
                    if flag=="x":
                        now=now_x.pop()
                    else:
                        now=now_y.pop()
                    nex.add(now+cnt)
                    if first==False:
                        nex.add(now-cnt)
                if flag=="x":
                    now_x=nex
                    flag="y"
                else:
                    now_y=nex
                    flag="x"
        else:
            nex=set()
            if flag=="x":
                l=len(now_x)
            else:
                l=len(now_y)
            for _ in range(l):
                if flag=="x":
                    now=now_x.pop()
                else:
                    now=now_y.pop()
                nex.add(now+cnt)
                if first==False:
                    nex.add(now-cnt)
            if flag=="x":
                now_x=nex
                flag="y"
            else:
                now_y=nex
                flag="x"
            if first:
                first=False
            i+=1
            cnt=0

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

if __name__=="__main__":
    main()

Solution (Python):
S = input().split('T')
S = [len(s) for s in S]
X = S[::2]
Y = S[1::2]

from collections import defaultdict,deque

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

dpx[sum(X)] = 1
dpy[sum(Y)] = 1
que = deque()
for i,x in enumerate(X):
    if i == 0:
        continue
    for k,v in dpx.items():
        if v:
            que.append(k-2*x)
    while que:
        q = que.popleft()
        dpx[q] = 1

for i,y in enumerate(Y):
    for k,v in dpy.items():
        if v:
            que.append(k-2*y)
    while que:
        q = que.popleft()
        dpy[q] = 1


x,y = map(int,input().split())
print('Yes' if dpx[x] and dpy[y] else '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,deque

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

dpx[Xsum] = 1
dpy[Ysum] = 1
que = deque()
for i,x in enumerate(X):
    if i == 0:
        continue
    for k,v in dpx.items():
        if v:
            que.append(k-2*x)
    while que:
        q = que.popleft()
        dpx[q] = 1

for i,y in enumerate(Y):
    for k,v in dpy.items():
        if v:
            que.append(k-2*y)
    while que:
        q = que.popleft()
        dpy[q] = 1


x,y = map(int,input().split())
print('Yes' if dpx[x] and dpy[y] else '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,deque

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

dpx[Xsum] = 1
dpy[Ysum] = 1
que = deque()
for i,x in enumerate(X):
    if i == 0:
        continue
    for k,v in dpx.items():
        if v:
            que.append(k-2*x)
    while que:
        q = que.popleft()
        dpx[q] = 1

for i,y in enumerate(Y):
    for k,v in dpy.items():
        if v:
            que.append(k-2*y)
    while que:
        q = que.popleft()
        dpy[q] = 1


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