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())
X = 0
num = 0
while num <= len(s) - 1:
    if s[num] == "F":
        X += 1
        num += 1
    else:
        num += 1
        break
flag = False
move_x = []
move_y = []
temp = 0
while num <= len(s) - 1:
    if s[num] == "F":
        temp += 1
        num += 1
    else:
        if flag == False:
            move_y.append(temp)
            flag = True
        else:
            move_x.append(temp)
            flag = False
        num += 1
        temp = 0
else:
    if flag == False:
        move_y.append(temp)
    else:
        move_x.append(temp)
set_x = {X}
set_y = {0}
for i in move_x:
    temp = set()
    for j in set_x:
        temp.add(j + i)
        temp.add(j - i)
    set_x = temp
for i in move_y:
    temp = set()
    for j in set_y:
        temp.add(j + i)
        temp.add(j - i)
    set_y = temp
if x in set_x and y in set_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 = list(input().split("T"))
    gx, gy = map(int, input().split())

    to_X = 0
    init_x = len(S[0])
    S.pop(0)
    X, Y = [], []
    for s in S:
        X.append(len(s)) if to_X else Y.append(len(s))
        to_X ^= 1

    now_x = {init_x}
    for x in X:
        tmp = set()
        for prev_x in now_x:
            tmp.add(prev_x + x)
            tmp.add(prev_x - x)
        now_x = tmp

    now_y = {0}
    for y in Y:
        tmp = set()
        for prev_y in now_y:
            tmp.add(prev_y + y)
            tmp.add(prev_y - y)
        now_y = tmp

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


if __name__ == '__main__':
    resolve()

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    S = in_s() + "T"
    N = len(S)

    X, Y = in_nn()

    x_num = []
    y_num = []

    f = False
    first = True

    cnt = 0
    for i in range(N):
        if S[i] == "T":

            if cnt > 0:
                if f:
                    if first:
                        Y -= cnt
                    else:
                        y_num.append(cnt)
                else:
                    if first:
                        X -= cnt
                    else:
                        x_num.append(cnt)
            cnt = 0
            f = not f
            first = False
        else:
            cnt += 1

    x_ok = False
    y_ok = False

    if len(x_num) == 0:
        if X == 0:
            x_ok = True
    else:
        dp_x = [False] * N
        dp_x[0] = True
        for tx in x_num:
            for i in range(N - 1, -1, -1):
                if i + tx < N and dp_x[i]:
                    dp_x[i + tx] = True

        xsum = sum(x_num)
        for i in range(N):
            if dp_x[i]:
                if abs(i - abs(xsum - i)) == abs(X):
                    x_ok = True

    if len(y_num) == 0:
        if Y == 0:
            y_ok = True
    else:
        dp_y = [False] * N
        dp_y[0] = True
        for ty in y_num:
            for i in range(N - 1, -1, -1):
                if i + ty < N and dp_y[i]:
                    dp_y[i + ty] = True

        ysum = sum(y_num)
        for i in range(N):
            if dp_y[i]:
                if abs(i - abs(ysum - i)) == abs(Y):
                    y_ok = True

    
    
    

    if x_ok and y_ok:
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    main()

Solution (Python):
s=input().split("T")
x,y=map(int,input().split())
a,b=[len(i)for i in s[0::2]],[len(i)for i in s[1::2]]
x-=a[0]
a,b=sorted(a[1:]),sorted(b)
while a:
 if x<0:x+=a.pop()
 else:x-=a.pop()
while b:
 if y<0:y+=b.pop()
 else:y-=b.pop()
print("YNeos"[x!=0 or y!=0::2])

Solution (Python):
import sys
import math
import copy
import random
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter



input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  
    fac = [0] * SIZE  
    finv = [0] * SIZE  
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def renritsu(A, Y):
    
    
    
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    
    return X.flatten().tolist()[0]

class TwoDimGrid:
    
    def __init__(self, h, w, wall="
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return
    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        
        
        
        
        

def solve():
    s = getS()
    x, y = getList()
    ls = len(s)
    hori = True
    h,v = [],[]
    cnt = 0
    for c in s:
        if c == "F":
            cnt += 1
        else:
            if hori:
                h.append(cnt)
            else:
                v.append(cnt)
            cnt = 0
            hori = not hori

    if cnt != 0:
        if hori:
            h.append(cnt)
        else:
            v.append(cnt)

    
    x -= h[0]
    h[0] = 0
    dp = [0] * (ls * 2 + 100)
    dp[ls + 50] = 1
    for num in h:
        tmp = [0] * (len(dp))
        for i, d in enumerate(dp):
            if d == 1:
                tmp[i - num] = 1
                tmp[i + num] = 1
        dp = tmp
    
    if dp[x + ls + 50] == 0:
        print("No")
        return

    dp = [0] * (ls * 2 + 100)
    dp[ls + 50] = 1
    for num in v:
        tmp = [0] * (len(dp))
        for i, d in enumerate(dp):
            if d == 1:
                tmp[i - num] = 1
                tmp[i + num] = 1
        dp = tmp
    
    if dp[y + ls + 50] == 0:
        print("No")
        return

    print("Yes")
    return


def main():
    n = getN()
    for _ in range(n):
        s = "".join([random.choice(["F", "T"]) for i in range(20)])
        print(s)
        solve(s, 1, 0)

    return
if __name__ == "__main__":
    
    solve()

Solution (Python):
'''
研究室PCでの解答
'''
import math

import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7 
dir = [(-1,0),(1,0),(0,-1),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"


def main():
    s = input()

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



    move = [[],[]]
    p = 0
    nm = 0
    for i in s:
        if i == "T":
            move[p].append(nm)
            nm = 0
            p = 1-p
        else:
            nm += 1
    move[p].append(nm)

    st = set([move[0][0]])
    for i in move[0][1::]:
        nst = set()
        for j in st:
            nst.add(j+i)
            nst.add(j-i)
        st = nst.copy()
    if not x in st:
        print("No")
        exit()

    st = set([0])
    for i in move[1]:
        nst = set()
        for j in st:
            nst.add(j+i)
            nst.add(j-i)
        st = nst.copy()
    if not y in st:
        print("No")
        exit()

    print("Yes")

    return None

if __name__ == '__main__':
    main()

Solution (Python):
import sys

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


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

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

    now_x = {init_x}
    for x in to_x:
        tmp = set()
        for now in now_x:
            tmp.add(now - x)
            tmp.add(now + x)
        now_x = tmp

    now_y = {0}
    for y in to_y:
        tmp = set()
        for now in now_y:
            tmp.add(now - y)
            tmp.add(now + y)
        now_y = tmp

    print("Yes" if X in now_x and Y in now_y else "No")


if __name__ == '__main__':
    resolve()

Solution (Python):
import sys
import math
import collections
import bisect
import itertools



sys.setrecursionlimit(10 ** 7)
INF = 10 ** 16
MOD = 10 ** 9 + 7


ni = lambda: int(sys.stdin.readline().rstrip())
ns = lambda: map(int, sys.stdin.readline().rstrip().split())
na = lambda: list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))




def main():
    s = list(input())
    x, y = ns()

    axis = [[], []]

    cnt = 0
    sw = 0
    for si in s:
        if si == "F":
            cnt += 1
        else:
            axis[sw].append(cnt)
            cnt = 0
            sw ^= 1
    axis[sw].append(cnt)
    if len(axis[0]) > 0:
        x -= axis[0][0]
        del axis[0][0]

    x_cand = {0}
    y_cand = {0}

    for xi in axis[0]:
        if xi == 0:
            continue
        nxt = set()
        for xc in x_cand:
            nxt.add(xc + xi)
            nxt.add(xc - xi)
        x_cand = nxt

    for yi in axis[1]:
        if yi == 0:
            continue
        nxt = set()
        for yc in y_cand:
            nxt.add(yc + yi)
            nxt.add(yc - yi)
        y_cand = nxt

    if x in x_cand and y in y_cand:
        print("Yes")
    else:
        print("No")

if __name__ == '__main__':
    main()

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




def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))



def main():
	s=input().rstrip().decode()+"T"
	x,y=MI()
	X=[]
	Y=[]

	i=0

	if s[0]=="F":
		for ss in s:
			if ss=="F":
				x-=1
				i+=1
			else:
				break
	

	while True:
		ii=s.find("T",i+1)
		if ii==-1:
			break
		Y.append(ii-i-1)

		i=s.find("T",ii+1)
		if i==-1:
			break
		X.append(i-ii-1)
	


	s=set([0])
	ss=set()
	for i in X:
		for j in s:
			ss.add(j+i)
			ss.add(j-i)
		s=ss.copy()
		ss=set()
		
	

	if x not in s:
		print("No")
		exit()

	s=set([0])
	ss=set()
	for i in Y:
		for j in s:
			ss.add(j+i)
			ss.add(j-i)
		s=ss.copy()
		ss=set()
	

	if y not in s:
		print("No")
		exit()

	print("Yes")






















if __name__ == "__main__":
	main()

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




def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))



def main():
	s=input().rstrip().decode()+"T"
	x,y=MI()
	X=[]
	Y=[]

	i=0

	if s[0]=="F":
		for ss in s:
			if ss=="F":
				x-=1
				i+=1
			else:
				break
	

	while True:
		ii=s.find("T",i+1)
		if ii==-1:
			break
		Y.append(ii-i-1)

		i=s.find("T",ii+1)
		if i==-1:
			break
		X.append(i-ii-1)
	


	s=set([0])
	ss=set()
	for i in X:
		for j in s:
			ss.add(j+i)
			ss.add(j-i)
		s=ss.copy()
		ss=set()
		
	

	if x not in s:
		print("No")
		exit()

	s=set([0])
	ss=set()
	for i in Y:
		for j in s:
			ss.add(j+i)
			ss.add(j-i)
		s=ss.copy()
		ss=set()
	

	if y not in s:
		print("No")
		exit()

	print("Yes")






















if __name__ == "__main__":
	main()

Solution (Python):
s = list(input())
x, y = map(int, input().split())
a = [[], []]
b = [0, 0]
j = 0
cnt = 0
for i in s:
    if i == "F":
        if j == 0:
            x -= 1
        else:
            cnt += 1
    else:
        if cnt > 0:
            a[j % 2].append(cnt)
            b[j % 2] += cnt
        j += 1
        cnt = 0
if cnt > 0:
    a[j % 2].append(cnt)
    b[j % 2] += cnt
x, y = abs(x), abs(y)
lx, ly = len(a[0]), len(a[1])
if b[0] < x or b[1] < y:
    print("No")
    exit()
if lx > 0:
    dp = [[0] * (2 * b[0] + 1) for _ in range(lx + 1)]
    dp[0][b[0]] = 1
    for i in range(lx):
        for j in range(2 * b[0] + 1):
            if dp[i][j] == 1:
                dp[i + 1][j + a[0][i]] = 1
                dp[i + 1][j - a[0][i]] = 1
    if dp[lx][b[0] + x] == 0:
        print("No")
        exit()
if ly > 0:
    dp = [[0] * (2 * b[1] + 1) for _ in range(ly + 1)]
    dp[0][b[1]] = 1
    for i in range(ly):
        for j in range(2 * b[1] + 1):
            if dp[i][j] == 1:
                dp[i + 1][j + a[1][i]] = 1
                dp[i + 1][j - a[1][i]] = 1
    if dp[ly][b[1] + y] == 0:
        print("No")
        exit()
print("Yes")

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

def main():
  def isinsums(x,lst):
    s=sum(lst)
    t=(s+x)//2
    if s==0 and x==0:
      return True
    if (s-x)%2 or t<0:
      return False
    t=(s+x)//2
    m1=(1<<(t+1))-1
    m2=1<<t
    sums=0
    for e in lst:
      sums=(sums|(1+sums)<<e)&m1
      if (sums+1)&m2>0:
        return True
    return False
  s=readline().strip()
  x,y=map(int,readline().split())
  
  s=s.replace('TF','T F')
  s=s.replace('FT','F T')
  bs=s.split()
  if bs[0][0]=='F':
    x-=len(bs[0])
    del bs[0]
  xy=[[],[]]
  f=0
  for i,e in enumerate(bs):
    if not i%2:
      l=len(e)
    else:
      f^=l%2
      xy[f].append(len(e))
  if isinsums(x,xy[0]) and isinsums(y,xy[1]):
    print('Yes')
  else:
    print('No')

if __name__=='__main__':
  main()

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

def isinsums(x,lst):
  s=sum(lst)
  t=(s+x)//2
  if s==0 and x==0:
    return True
  if (s-x)%2 or t<0:
    return False
  t=(s+x)//2
  m1=(1<<(t+1))-1
  m2=1<<t
  sums=0
  for e in lst:
    sums=(sums|(1+sums)<<e)&m1
  sums|=1
  if (sums)&m2>0:
    return True
  return False

s=readline().strip()
x,y=map(int,readline().split())

s=s.replace('TF','T F')
s=s.replace('FT','F T')
bs=s.split()
if bs[0][0]=='F':
  x-=len(bs[0])
  del bs[0]

xy=[[],[]]
f=0
for i,e in enumerate(bs):
  if not i%2:
    l=len(e)
  else:
    f^=l%2
    xy[f].append(len(e))
if isinsums(x,xy[0]) and isinsums(y,xy[1]):
  print('Yes')
else:
  print('No')

Solution (Python):
import sys

def main():
  s = input()
  n = len(s)
  x, y = map(int, input().split())
  
  move = [0]
  for i in range(n):
    if s[i] == "F":
      move[-1] += 1
    else:
      move.append(0)
  
  move_x = move[2::2]
  move_y = move[1::2]
  move_a = move[0]
  
  for start, lis in zip([x-move_a, y], [move_x, move_y]):
    move_range = sum(lis)
    if abs(start) > move_range:
      print("No")
      sys.exit()
    
    dp = 2**move_range
    for value in lis:
      dp = (dp >> value)|(dp << value)
    if not (dp>>(move_range+start))&1:
      print("No")
      sys.exit()
  
  print("Yes")
  
  
if __name__ == "__main__":
  main()

Solution (Python):
import sys

def main():
  s = input()
  n = len(s)
  x, y = map(int, input().split())
  
  move = [0]
  for i in range(n):
    if s[i] == "F":
      move[-1] += 1
    else:
      move.append(0)
  
  move_x = move[2::2]
  move_y = move[1::2]
  move_a = move[0]
  
  for start, lis in zip([x-move_a, y], [move_x, move_y]):
    move_range = sum(lis)
    if abs(start) > move_range:
      print("No")
      sys.exit()
    
    dp = 2**move_range
    for value in lis:
      dp = (dp >> value)|(dp << value)
    if not (dp>>(move_range+start))&1:
      print("No")
      sys.exit()
  
  print("Yes")
  
  
if __name__ == "__main__":
  main()

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

  dpx = [[False, False] for _ in range(n+2)]
  dpy = [[False, False] for _ in range(n+2)]

  dpx[0][0] = True
  dpy[0][0] = True
  x_turn = False
  y_turn = False
  judge = True
  turn = False
  xcount = 0
  count = 0
  for i in range(n):
    if judge and s[i] == "F":
      xcount += 1
      continue
    if judge:
      judge = False
      turn = False if turn else True
      continue
    if s[i] == "F":
      count += 1
      if i != n-1:
        continue
    if turn:
      y_turn = False if y_turn else True
      if y_turn:
        p, q = 0, 1
      else:
        p, q = 1, 0
      for i in range(n+1):
        
        dpy[i][q] = False
      for i in range(n+1):
        if dpy[i][p]:
          dpy[abs(i-count)][q] = True
          dpy[i+count][q] = True
    else:
      x_turn = False if x_turn else True
      if x_turn:
        p, q = 0, 1
      else:
        p, q = 1, 0
      for i in range(n+1):
        dpx[i][q] = False
      for i in range(n+1):
        if dpx[i][p]:
          dpx[abs(i-count)][q] = True
          dpx[i+count][q] = True
    turn = False if turn else True
    count = 0
  
  p = 1 if x_turn else 0
  q = 1 if y_turn else 0
  x -= xcount
  if abs(x) > n:
    print("No")
  elif dpx[abs(x)][p] and dpy[abs(y)][q]:
    print("Yes")
  else:
    print("No")
  
if __name__ == "__main__":
  main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def check(vec, init_pos, target_pos):
    dp = {init_pos}

    for nv in vec:
        if nv == 0:
            continue
        dp_next = set()
        for v in dp:
            dp_next.add(v - nv)
            dp_next.add(v + nv)
        dp = dp_next

    return target_pos in dp


def main():
    S = readline().strip()
    x, y = map(int, readline().split())

    L = [len(commands) for commands in S.split('T')]

    init_x = L[0]
    x_commands = L[2::2]
    y_commands = L[1::2]

    ok = check(x_commands, init_x, x) and check(y_commands, 0, y)

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

    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def check(vec, init_pos, target_pos):
    dp = {init_pos}

    for nv in vec:
        dp_next = set()
        for v in dp:
            dp_next.add(v - nv)
            dp_next.add(v + nv)
        dp = dp_next

    return target_pos in dp


def main():
    S = readline().strip()
    x, y = map(int, readline().split())

    L = [len(commands) for commands in S.split('T')]

    init_x = L[0]
    x_commands = [c for c in L[2::2] if c != 0]
    y_commands = [c for c in L[1::2] if c != 0]

    ok = check(x_commands, init_x, x) and check(y_commands, 0, y)

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

    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def check(vec, init_pos, target_pos):
    dp = {init_pos}

    for nv in vec:
        dp_next = set()
        for v in dp:
            dp_next.add(v - nv)
            dp_next.add(v + nv)
        dp = dp_next

    return target_pos in dp


def main():
    S = readline().strip()
    x, y = map(int, readline().split())

    L = [len(commands) for commands in S.split('T')]

    init_x = L[0]
    x_commands = L[2::2]
    y_commands = L[1::2]

    ok = check(x_commands, init_x, x) and check(y_commands, 0, y)

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

    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def check(vec, init_pos, target_pos):
    base = 8000
    dp = [False] * 16001
    dp[init_pos + base] = True

    for i in range(len(vec)):
        dp, dp_prev = [False] * 16001, dp
        for j in range(16001):
            if (
                j - vec[i] >= 0
                and dp_prev[j - vec[i]]
                or j + vec[i] <= 16000
                and dp_prev[j + vec[i]]
            ):
                dp[j] = True

    return dp[target_pos + base]


def main():
    S = readline().strip()
    x, y = map(int, readline().split())

    idx = len(S)
    for i, c in enumerate(S):
        if c == 'T':
            idx = i
            break

    init_x = idx
    step = [[] for _ in range(2)]
    i = 0
    for c in S[idx:]:
        if c == 'T':
            i = 1 - i
            step[i].append(0)
        else:
            step[i][-1] += 1
            
    for row in step:
        row = [v for v in row if v != 0]
        if not row:
            row = [0]

    ok = check(step[0], init_x, x) and check(step[1], 0, y)

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

    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def check(vec, init_pos, target_pos):
    base = 8000
    dp = [False] * 16001
    dp[init_pos + base] = True

    for i in range(len(vec)):
        dp, dp_prev = [False] * 16001, dp
        for j in range(16001):
            if (
                j - vec[i] >= 0
                and dp_prev[j - vec[i]]
                or j + vec[i] <= 16000
                and dp_prev[j + vec[i]]
            ):
                dp[j] = True

    return dp[target_pos + base]


def main():
    S = readline().strip()
    x, y = map(int, readline().split())

    idx = len(S)
    for i, c in enumerate(S):
        if c == 'T':
            idx = i
            break

    init_x = idx
    step = [[0] for _ in range(2)]
    i = 0
    for c in S[idx:]:
        if c == 'T':
            i = 1 - i
            step[i].append(0)
        else:
            step[i][-1] += 1

    ok = check(step[0], init_x, x) and check(step[1], 0, y)

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

    return


if __name__ == '__main__':
    main()

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

d = [len(v) for v in s.split("T")]
x -= d[0]
a = d[2::2]
b = d[1::2]


def c(d, x):
    cur = {0}
    for i in d:
        nxt = set()
        for j in cur:
            nxt.add(j + i)
            nxt.add(j - i)
        cur = copy(nxt)
    return x in cur

print("Yes" if c(a, x) and c(b, y) else "No")

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

xy=[[],[]]

sgn = 1
now = n
for i in range(n):
    if s[i] == "T":
        x -= i
        now = i + 1
        break
for i in range(now, n):
    if s[i] == "T":
        xy[sgn].append(i - now)
        now = i + 1
        sgn ^= 1


cur = {0}
for i in xy[0]:
    nxt = set()
    for j in cur:
        nxt.add(j + i)
        nxt.add(j - i)
    cur = copy(nxt)

flag = x in cur

cur = {0}
for i in xy[1]:
    nxt = set()
    for j in cur:
        nxt.add(j + i)
        nxt.add(j - i)
    cur = copy(nxt)

print("Yes" if flag and y in cur else "No")

Solution (Python):
def turn():
  global drc
  if moves[drc][-1] != 0:
    moves[drc].append(0)
  drc=(drc+1)%2

def forward():
  moves[drc][-1]+=1
  
OP={"T":turn,"F":forward}

ops=input()
target=list(map(int,input().split()))
moves=[[0],[0]]
drc=0
first_F=0


for i,o in enumerate(ops):
  if o=="T" or i+1==len(ops):
    first_F=i
    break

for i in range(first_F,len(ops)):
  OP[ops[i]]()


x_set=set([first_F])
for move in moves[0]:
  tmp_set=set()
  for xx in x_set:
    tmp_set.add(xx+move)
    tmp_set.add(xx-move)
  x_set=tmp_set
  
y_set=set([0])
for move in moves[1]:
  tmp_set=set()
  for yy in y_set:
    tmp_set.add(yy+move)
    tmp_set.add(yy-move)
  y_set=tmp_set

if target[0] in x_set and target[1] in y_set:
  print("Yes")
else:
  print("No")

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

xy = True
x_s = []
y_s = []
cnt = 0
for s in string:
    if s == "F":
        cnt += 1
    else:
        if xy:
            x_s.append(cnt)
            xy = False
        else:
            y_s.append(cnt)
            xy = True
        cnt = 0
if xy:
    x_s.append(cnt)
    xy = False
else:
    y_s.append(cnt)
    xy = True
x_can = [False for _ in range(8001)]
y_can = [False for _ in range(8001)]

x_can[x_s[0]] = True
for xx in x_s[1:]:
    for i in range(8000, xx - 1, -1):
        if x_can[i - xx]:
            x_can[i] = True

y_can[0] = True
for yy in y_s:
    for i in range(8000, yy - 1, -1):
        if y_can[i - yy]:
            y_can[i] = True
            
x_pos = (x + sum(x_s))
y_pos = (y + sum(y_s))
if x_pos % 2 == 1 or y_pos % 2 == 1:
    print("No")
elif x_can[x_pos // 2] and y_can[y_pos // 2]:
    print("Yes")
else:
    print("No")

Solution (Python):
s=input()
x,y=map(int, input().split())
cx=[]
cy=[]
dirx=1
l=0
ini=1
for i in range(len(s)):
  if s[i] == "F":
    l += 1
  else:
    if ini:
      x -= l
      ini = 0
    else:
      if dirx:
        cx.append(l)
      else:
        cy.append(l)
    l = 0
    dirx = not dirx

if ini == 0:
  if dirx:
    cx.append(l)
  else:
    cy.append(l)
else:
  x -= l





ng = 0
dp = {0}
for i in range( len(cx) ):
  dpn = set()
  for d in dp:
    dpn.add(d+cx[i])
    dpn.add(d-cx[i])

  dpn, dp = dp, dpn
  
if not x in dp:
  ng = 1

  
dp = {0}
for i in range( len(cy) ):
  dpn = set()
  for d in dp:
    dpn.add(d+cy[i])
    dpn.add(d-cy[i])

  dpn, dp = dp, dpn
  
if not y in dp:
  ng = 1


if ng==0:
  print("Yes")
else:
  print("No")

Solution (Python):
dic={"x":"y","y":"x"}

a = list(input())
X,Y = map(int,input().split())

direction = "x"
cersol = 0
while cersol < len(a) and a[cersol] == "F":
    cersol+=1
    X -= 1

move = {"x":[],"y":[]}

while cersol < len(a):
    if a[cersol]=="T":
        direction=dic[direction]
        cersol+=1

    count = 0
    while cersol < len(a) and a[cersol] == "F":
        count+=1
        cersol+=1
    
    move[direction].append(count)


able = {0:0}
preable = {0:0}
for i in move["x"]:
    able={}
    for j in preable:
        able[j+i]=0
        able[j-i]=0
    preable=able


ableX = able

able = {0:0}
preable = {0:0}
for i in move["y"]:
    able = {}
    for j in preable:
        able[j+i]=0
        able[j-i]=0
    preable=able

if (X in ableX)and(Y in able):
    print("Yes")
else:
    print("No")

Solution (Python):
import sys

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

goal = {"x": x, "y": y}
dist = {"x": [], "y": []}

if s[-1] != "T":
    s += "T"
d = 0
f = 0
x_init = -1
for i in s:
    if i == "F":
        d += 1
    else:
        if f % 2 == 0:
            if x_init == -1:
                x_init = d
            else:
                dist["x"].append(d)
        else:
            dist["y"].append(d)
        d = 0
        f += 1


for mode in ("x", "y"):
    len_d = len(dist[mode])
    cand = set()
    if mode == "x":
        cand.add(x_init)
    else:
        cand.add(0)
    for i in range(len_d):
        next_cand = set()
        for d in cand:
            next_cand.add(d + dist[mode][i])
            next_cand.add(d - dist[mode][i])
        cand = next_cand

    if goal[mode] not in cand:
        print("No")
        sys.exit()

print("Yes")

Solution (Python):
s=input()
x,y=map(int, input().split())
X,Y=[],[]
t,c=0,0
for i in range(len(s)):
  if s[i]=='F':
    c+=1
  else:
    if t%2==0:
      X.append(c)
    else:
      Y.append(c)
    c=0
    t+=1
if t%2==0:
  X.append(c)
else:
  Y.append(c)
if len(X)>0:
  D=[X[0]]
  E=[]
  for i in range(1,len(X)):
    for j in D:
      E.append(j+X[i])
      E.append(j-X[i])
    D,E=list(set(E)),[]
if x not in D:
  print('No')
  exit()
  
if len(X)>0:
  D=[0]
  E=[]
  for i in range(len(Y)):
    for j in D:
      E.append(j+Y[i])
      E.append(j-Y[i])
    D,E=list(set(E)),[]
if y not in D:
  print('No')
else:
  print('Yes')

Solution (Python):
s=input()
x,y=map(int, input().split())
X,Y=[],[]
t,c=0,0
for i in range(len(s)):
  if s[i]=='F':
    c+=1
  else:
    if t%2==0:
      X.append(c)
    else:
      Y.append(c)
    c=0
    t+=1
if t%2==0:
  X.append(c)
else:
  Y.append(c)
if len(X)>0:
  D=[X[0]]
  E=[]
  for i in range(1,len(X)):
    for j in D:
      E.append(j+X[i])
      E.append(j-X[i])
    D,E=list(set(E)),[]
if x not in D:
  print('No')
  exit()
  
if len(X)>0:
  D=[0]
  E=[]
  for i in range(len(Y)):
    for j in D:
      E.append(j+Y[i])
      E.append(j-Y[i])
    D,E=list(set(E)),[]
if y not in D:
  print('No')
else:
  print('Yes')