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())
a = []
b = []

p = 0 
q = 1 

for i in range(len(s)):
    if s[i] == 'T':
        if q == 1:
            a.append(p)
        else:
            b.append(p)
        q *= -1
        p = 0
    if s[i] == 'F':
        p += 1
        if i == len(s) - 1:
            if q == 1:
                a.append(p)
            else:
                b.append(p)

if a:
    x -= a.pop(0)


def knapsack(L, n):
    
    if n < 0 or n > sum(L):
        return False
    else:
        dp = [True] + [False] * (n)
        for l in L:
            for i in range(n, -1, -1):
                if i >= l:
                    dp[i] = dp[i - l] or dp[i]
        return dp[n]

def reachable(L, n):
    
    S = sum(L)
    return (S - n) % 2 == 0 and knapsack(L, (S - n) // 2)


if reachable(a, x) and reachable(b, y):
    print('Yes')
else:
    print('No')

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

def f(pos,start):
  dp={0}
  for i in range(start,n,2):
    tmp=set()
    for j in dp:
      if i==0:
        tmp.add(j+l[i])
      else:
        tmp.add(j+l[i])
        tmp.add(j-l[i])
    dp=tmp
  return pos in dp

if f(x,0) and f(y,1):
  print("Yes")
else:
  print("No")

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())

S = input()
Xans,Yans = inpl()

Xmove = []
Ymove = []

First = True
flag = True
tmp = 0
for s in S+'T':
	if s == 'F':
		tmp += 1
	else:
		if First:
			Xans -= tmp
			First = False
		else:
			if flag:
				Xmove.append(tmp)
			else:
				Ymove.append(tmp)
		tmp = 0
		flag = not flag


Xsum = sum(Xmove)
if (Xsum+Xans)%2 == 1:
	print('No')
	sys.exit()
elif abs(Xans) > Xsum:
	print('No')
	sys.exit()
elif len(Xmove) == 0:
	if Xans != 0:
		print('No')
		sys.exit()
else:
	Xpos = (Xsum+Xans)//2
	dp = [0]*(Xpos+1)
	dp[0] = 1
	for x in Xmove:
		for i in range(Xpos,x-1,-1):
			dp[i] = dp[i-x]
		if dp[-1] == 1:
			break
	else:
		print('No')
		sys.exit()

Ysum = sum(Ymove)
if (Ysum+Yans)%2 == 1:
	print('No')
	sys.exit()
elif abs(Yans) > Ysum:
	print('No')
	sys.exit()
elif len(Ymove) == 0:
	if Yans != 0:
		print('No')
		sys.exit()
else:
	Ypos = (Ysum+Yans)//2
	dp = [0]*(Ypos+1)
	dp[0] = 1
	for y in Ymove:
		for i in range(Ypos,y-1,-1):
			dp[i] = dp[i-y]
		if dp[-1] == 1:
			break
	else:
		print('No')
		sys.exit()

print('Yes')

Solution (Python):
from collections import defaultdict

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

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

dx = d[2::2]
n_dx = len(dx)
dp = {d[0]}
for i in range(n_dx):
    tmp = set()
    for j in dp:
        tmp.add(j - dx[i])
        tmp.add(j + dx[i])
    dp = tmp
x_flag = x in dp

dy = d[1::2]
n_dy = len(dy)
dp = {0}
for i in range(n_dy):
    tmp = set()
    for j in dp:
        tmp.add(j - dy[i])
        tmp.add(j + dy[i])
    dp = tmp
y_flag = y in dp

if x_flag and y_flag:
    print('Yes')
else:
    print('No')

Solution (Python):
from collections import defaultdict

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

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

dx = d[0::2]
n_dx = len(dx)
dp = {0}
for i in range(n_dx):
    tmp = set()
    for j in dp:
        if i == 0:
            tmp.add(j + dx[i])
        else:
            tmp.add(j - dx[i])
            tmp.add(j + dx[i])
    dp = tmp
x_flag = x in dp

dy = d[1::2]
n_dy = len(dy)
dp = {0}
for i in range(n_dy):
    tmp = set()
    for j in dp:
        tmp.add(j - dy[i])
        tmp.add(j + dy[i])
    dp = tmp
y_flag = y in dp

if x_flag and y_flag:
    print('Yes')
else:
    print('No')

Solution (Python):
s = [len(i) for i in input().split("T")]
x,y  = (int(i) for i in input().split())
xl,yl,flag = [],[],False
for i in range(len(s)):
	if i%2==0: xl.append(s[i])
	else: yl.append(s[i])
x,y,xl = abs(x-xl[0]),abs(y),xl[1:]
xs,ys,lenx,leny,xl2,yl2 = sum(xl),sum(yl),len(xl),len(yl),[0],[0]
a,b = [[0 for i in range(xs+1)] for i in range(lenx+1)],[[0 for i in range(ys+1)] for i in range(leny+1)]
a[0][0],b[0][0] = 1,1
if xs>=x and (xs-x)%2==0 and ys>=y and (ys-y)%2==0: x2,y2,flag = (xs-x)//2,(ys-y)//2,True
else: print("No")
if flag:
	for i in range(lenx):
		for j in range(x2+1):
			if xl[i]<=j: a[i+1][j] = a[i][j-xl[i]] | a[i][j]
			else: a[i+1][j] = a[i][j]
	for i in range(leny):
		for j in range(y2+1):
			if yl[i]<=j: b[i+1][j] = b[i][j-yl[i]] | b[i][j]
			else: b[i+1][j] = b[i][j]
	if a[-1][x2] and b[-1][y2]: print("Yes")
	else: print("No")

Solution (Python):
s = input()
x,y = map(int,input().split())
x1 = 0
y1 = []
muki = 0
t = len(s)
x2 = []
for i in range(len(s)):
  a = s[i]
  if a == "F":
    x1 += 1
  else:
    t = i+0
    break
x3 = 0
y2 = 0
for i in range(t,len(s)):
  a = s[i]
  if a == "F":
    if muki%2 == 0:
      x3+= 1
    else:
      y2 += 1
  else:
    if muki%2 == 0:
      if x3 != 0:
        x2.append(x3)
      x3 = 0
      muki +=1
    else:
      if y2 != 0:
        y1.append(y2)
      y2 = 0
      muki += 1
if x3 != 0:
   x2.append(x3)
if y2 != 0:
   y1.append(y2)
ansx = set([x1])
for i in x2:
  g = len(ansx)
  ansx2 = list(ansx)
  ansx = set([])
  for j in range(g):
    ansx.add(ansx2[j]+i)
    ansx.add(ansx2[j]-i)
ansy = set([0])
for i in y1:
  g = len(ansy)
  ansy2 = list(ansy)
  ansy = set([])
  for j in range(g):
    ansy.add(ansy2[j]+i)
    ansy.add(ansy2[j]-i)
if (x in ansx and y in ansy):
  print("Yes")
else:
  print("No")

Solution (Python):

s=list(input())
x,y = map(int,input().split())
s.append('T')
n = len(s)
d = 0
c = 0
t = 0
k = 1
xa = {0}
ya = {0}

while d <= n-1:
    if s[d] == 'F':
        c += 1
        d += 1
    else:
        if k == 1:
            if t == 0:
                xa = {c}
                t = 1
            else:    
                xb = set()
                for xx in xa:
                    xb.add(xx + c)
                    xb.add(xx-c)
                    xa = xb
        else:
            yb = set()
            for yy in ya:
                yb.add(yy+c)
                yb.add(yy-c)
                ya = yb
        c = 0
        d += 1
        k = k * (-1)
if x in xa and y in ya:
    print('Yes')
else:
    print('No')

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


move = None
tx = None
ty = None


@mt
def slv(S, x, y):
    tx = x
    ty = y
    move = list(map(lambda x: len(x), S.split('T')))
    xm = [move[0]]
    for i in range(2, len(move), 2):
        xm_ = []
        for x in xm:
            xm_.append(x + move[i])
            xm_.append(x - move[i])
        xm = set(xm_)

    ym = [0]
    for i in range(1, len(move), 2):
        ym_ = []
        for y in ym:
            ym_.append(y + move[i])
            ym_.append(y - move[i])
        ym = set(ym_)

    return 'Yes' if tx in xm and ty in ym else 'No'


def main():
    S = read_str().strip()
    x, y = read_int_n()
    print(slv(S, x, y))


if __name__ == '__main__':
    main()

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


move = None
tx = None
ty = None


@mt
def slv(S, x, y):
    tx = x
    ty = y
    move = list(map(lambda x: len(x), S.split('T')))
    xm = [move[0]]
    for i in range(2, len(move), 2):
        xm_ = []
        for x in xm:
            xm_.append(x + move[i])
            xm_.append(x - move[i])
        xm = set(xm_)

    ym = [0]
    for i in range(1, len(move), 2):
        ym_ = []
        for y in ym:
            ym_.append(y + move[i])
            ym_.append(y - move[i])
        ym = set(ym_)

    return 'Yes' if tx in xm and ty in ym else 'No'


def main():
    S = read_str().strip()
    x, y = read_int_n()
    print(slv(S, x, y))


if __name__ == '__main__':
    main()

Solution (Python):
def main():
  s = input()
  x, y = map(int, input().split())
  lens = len(s)
  ind = 0
  if "T" in s:
    ind = s.index("T")
    x -= ind
  else:
    ind = lens
    x -= lens
  x_lst = []
  y_lst = []
  direct = 0
  while ind < lens:
    acc = 0
    if s[ind] == "F":
      while ind < lens and s[ind] == "F":
        ind += 1
        acc += 1
      if direct == 0:
        x_lst.append(acc)
      else:
        y_lst.append(acc)
  
    elif s[ind] == "T":
      while ind < lens and s[ind] == "T":
        ind += 1
        acc += 1
      direct = (direct + acc) % 2
  x = abs(x)
  y = abs(y)  
  sumx = sum(x_lst)
  sumy = sum(y_lst)
  if sumx < x or (sumx - x) % 2 or sumy < y or (sumy - y) % 2:
    print("No")
  else:
    target_x = (sumx - x) // 2
    target_y = (sumy - y) // 2
    canx = [False] * (target_x + 1)
    canx[0] = True
    cany = [False] * (target_y + 1)
    cany[0] = True
  
    break_flag = False
    for v in x_lst:
      for i in range(target_x - v + 1):
        if canx[i]:
          canx[i + v] = True
          if i + v == target_x:
            break_flag = True
            break
      if break_flag:
        break
    
    break_flag = False
    for v in y_lst:
      for i in range(target_y - v + 1):
        if cany[i]:
          cany[i + v] = True
          if i + v == target_y:
            break_flag = True
            break
      if break_flag:
        break
  
    if canx[target_x] and cany[target_y]:
      print("Yes")
    else:
      print("No")

main()

Solution (Python):

"""
Created on Fri Jun 22 10:08:31 2018

@author: Yuya
"""

s=list(input())

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

N = len(s)
count=0
flag = 1

x_s = []
y_s = []

for i in range(N):
    if s[i]=="F":
        count+=1
    else:
        if flag>0:
            x_s.append(count)
        else:
            y_s.append(count)     
        flag=flag*-1
        count=0

if flag>0:
    x_s.append(count)
else:
    y_s.append(count)
    
if s[0]=="F":
    x = x-x_s[0]
    del x_s[0]
    
def t_dfs(move, target):

    move.sort()
    move.reverse()

    for dx in move:
        if target > 0:
            target -= dx
        else:
            target += dx

    return target == 0    

if t_dfs(x_s, x) and t_dfs(y_s, y):
    print("Yes")
else:
    print("No")

Solution (Python):
S = input()
S += "T"
gx, gy = map(int, input().split())
firstT = S.find("T")
X = {firstT}
Y = {0}
h = True
f = 0
for s in S[firstT:]:
    if s == "T":
        if h:
            newX = set([])
            for x in X:
                newX.add(x + f)
                newX.add(x - f)
            X = newX
        else:
            newY = set([])
            for y in Y:
                newY.add(y + f)
                newY.add(y - f)
            Y = newY
        h = not h
        f = 0
    else:
        f += 1
if gx in X and gy in Y:
    print("Yes")
else:
    print("No")

Solution (Python):
import sys
sys.setrecursionlimit(100000)
s=input().strip()
x,y=map(int,input().split())
sl=[len(i) for i in s.split('T')]
x0,y0=sl[0],0
s_x = sl[::2]
s_y = sl[1::2]
s_y=[0]+s_y
dpx=[set() for i in s_x]
dpy=[set() for i in s_y]
dpx[0].add(s_x[0])
dpy[0].add(0)
for i,l in enumerate(s_x):
    if i==0:continue
    ll=dpx[i]
    for xx in dpx[i-1]:
        ll.add(xx-l)
        ll.add(xx+l)
for i,l in enumerate(s_y):
    if i==0:continue
    ll=dpy[i]
    for yy in dpy[i-1]:
        ll.add(yy-l)
        ll.add(yy+l)
print('Yes' if x in dpx[-1] and y in dpy[-1] else 'No')

Solution (Python):
s = list(map(len, input().split("T")))
*x, = map(int,input().split())
x[0] -= s[0]
s = s[1:]

moveX, moveY = [], []
for i in range(len(s)):
    if i%2 == 0:
        moveY.append(s[i])
    else:
        moveX.append(s[i])
X = [x[0]]
Y = [x[1]]
for x in moveX:
    X_ = []
    for cx in X:
        X_.append(cx+x)
        X_.append(cx-x)
    X = list(set(X_))
for y in moveY:
    Y_ = []
    for cy in Y:
        Y_.append(cy+y)
        Y_.append(cy-y)
    Y = list(set(Y_))
print(["No","Yes"][0 in X and 0 in Y])

Solution (Python):
s = raw_input()
tx, ty = map(int, raw_input().split())
xs, ys = [], []
for i, l in enumerate(map(len, s.split("T"))):
    if i % 2 == 0:
        xs.append(l)
    else:
        ys.append(l)
tx -= xs[0]
xs.pop(0)
xs = sorted(xs)[::-1]
ys = sorted(ys)[::-1]
for x in xs:
    tx += (2*(tx < 0) - 1) * x
for y in ys:
    ty += (2*(ty < 0) - 1) * y
print "Yes" if ((tx == 0) and (ty == 0)) else "No"

Solution (Python):

s = input().split("T")
x, y = ([int(i) for i in input().split()])


movex = []
movey = []

for i, t in enumerate(s):
    if i % 2 == 0:
        movex.append(len(t))
    else:
        movey.append(len(t))




def t_dfs(move, target, is_x):
    if is_x:
        target -= move.pop(0)

    move.sort(reverse=True)

    for dx in move:
        if target > 0:
            target -= dx
        else:
            target += dx

    return target == 0


if t_dfs(movex, x, True) and t_dfs(movey, y, False):
    print("Yes")
else:
    print("No")

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

if 'T' not in S:
    print('Yes' if x==len(S) and y==0 else 'No')
    exit()

sp = S.split('T')
x -= len(sp[0])

v_moves = []
h_moves = []
for i,s in enumerate(sp[1:]):
    if not s: continue
    if i%2==0:
        v_moves.append(len(s))
    else:
        h_moves.append(len(s))

if not h_moves:
    if x != 0:
        print('No')
        exit()
else:
    hs = set([0])
    for h in h_moves:
        tmp = set()
        for h2 in hs:
            tmp.add(h2 + h)
            tmp.add(h2 - h)
        hs = tmp
    if x not in hs:
        print('No')
        exit()

if not v_moves:
    if y != 0:
        print('No')
        exit()
else:
    vs = set([0])
    for v in v_moves:
        tmp = set()
        for v2 in vs:
            tmp.add(v2 + v)
            tmp.add(v2 - v)
        vs = tmp
    if y not in vs:
        print('No')
        exit()

print('Yes')

Solution (Python):
S = input()+"T"
X,Y = map(int,input().split())
dX0,dX,dY,dr,dc = 0,[],[],"x",0
for s in S:
    if s == "F":
        dc += 1
    elif s == "T":
        if dr == "x":
            dr = "y"
            dX.append(dc)
        else:
            dr = "x"
            dY.append(dc)
        dc = 0
dX0,dX = dX[0],dX[1:]

canX,canY = [dX0],[0]
for dx in dX:
    t = []
    for cx in canX:
        t.append(cx+dx)
        t.append(cx-dx)
    canX = list(set(t))

for dy in dY:
    t = []
    for cy in canY:
        t.append(cy+dy)
        t.append(cy-dy)
    canY = list(set(t))

print("Yes" if X in canX and Y in canY else "No")

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

X_act = [len(i) for i in S[::2]]
Y_act = [len(i) for i in S[1::2]]

X_reached = {X_act[0]}
del X_act[0]
for xa in X_act:
    tmp = set()
    for xr in X_reached:
        tmp.add(xr+xa)
        tmp.add(xr-xa)
    X_reached = tmp

Y_reached = {0}
for ya in Y_act:
    tmp = set()
    for yr in Y_reached:
        tmp.add(yr+ya)
        tmp.add(yr-ya)
    Y_reached = tmp

if x in X_reached and y in Y_reached:
    print("Yes")
else:
    print("No")

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

X_command = [len(i) for i in S[0::2]]
Y_command = [len(i) for i in S[1::2]]

x_reachd = {X_command.pop(0)}
y_reachd = {0}

for X in X_command:
    tmp = set()
    for xi in x_reachd:
        tmp.add(xi + X)
        tmp.add(xi - X)
    x_reachd = set(tmp)

for Y in Y_command:
    tmp = set()
    for yi in y_reachd:
        tmp.add(yi + Y)
        tmp.add(yi - Y)
    y_reachd = set(tmp)

if x in x_reachd and y in y_reachd:
    print("Yes")
else:
    print("No")

Solution (Python):
S = input()
N = len(S)
S = list(S.split("T"))
x, y = map(int, input().split())

X_command = sorted([len(i) for i in S[::2]], reverse=True)
Y_command = sorted([len(i) for i in S[1::2]], reverse=True)

nx = X_command.pop(-1)
ny = 0

for xi in X_command:
    if nx > x:
        nx -= xi
    else:
        nx += xi

for yi in Y_command:
    if ny > y:
        ny -= yi
    else:
        ny += yi

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

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

s = s.split("T")
s_x = []
s_y = []
for i in range(0,len(s),2):
    s_x.append(s[i])
for i in range(1,len(s),2):
    s_y.append(s[i])
x_cou = []
y_cou = []
for i in range(len(s_x)):
    x_cou.append(s_x[i].count("F"))
for j in range(len(s_y)):
    y_cou.append(s_y[j].count("F"))

if sum(x_cou) < abs(x) or sum(y_cou) <abs(y):
    print("No")
    exit()

x = x - x_cou[0]
x_list = set([0])
for i in range(len(x_cou)-1,0,-1):
    new_xlist = set()
    for j in range(len(x_list)):
        k = x_list.pop()
        new_xlist.add(k + x_cou[i])
        new_xlist.add(k - x_cou[i])
    x_list = new_xlist

y_list = set([0])
for i in range(len(y_cou)-1,-1,-1):
    new_ylist = set([])
    for j in range(len(y_list)):
        l = y_list.pop()
        new_ylist.add(l + y_cou[i])
        new_ylist.add(l - y_cou[i])
    y_list = new_ylist

if x in x_list and y in y_list:
    print("Yes")
else:
    print("No")

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

dx = []
dy = []
hv = 0
for i in s:
    if hv % 2 == 0:
        dx.append(len(i))
    else:
        dy.append(len(i))
    hv += 1
    
nx = dx.pop(0)
ny = 0

for a in sorted(dx,reverse=True):
    if nx > x:
        nx -= a
    else:
        nx += a

for a in sorted(dy,reverse=True):
    if ny > y:
        ny -= a
    else:
        ny += a

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

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

s = s.split("T")
s_x = []
s_y = []
for i in range(0,len(s),2):
    s_x.append(s[i])
for i in range(1,len(s),2):
    s_y.append(s[i])
x_count = []
y_count = []
for i in range(len(s_x)):
    x_count.append(s_x[i].count("F"))
for j in range(len(s_y)):
    y_count.append(s_y[j].count("F"))

if sum(x_count) < abs(x) or sum(y_count) <abs(y):
    print("No")
    exit()

x = x - x_count[0]
x_list = set([0])
for i in range(len(x_count)-1,0,-1):
    new_xlist = set()
    for j in range(len(x_list)):
        k = x_list.pop()
        new_xlist.add(k + x_count[i])
        new_xlist.add(k - x_count[i])
    x_list = new_xlist

y_list = set([0])
for i in range(len(y_count)-1,-1,-1):
    new_ylist = set([])
    for j in range(len(y_list)):
        l = y_list.pop()
        new_ylist.add(l + y_count[i])
        new_ylist.add(l - y_count[i])
    y_list = new_ylist

if x in x_list and y in y_list:
    print("Yes")
else:
    print("No")

Solution (Python):
import sys

def check_ans(s,x):
  res=[0]
  for i in s:
    res = set([x + i for x in res]) | set([x - i for x in res])
  return x in res

S = sys.stdin.readline().strip()
X,Y=map(int,input().split())
d=1
while S and S[0]=='F':
  S=S[1:]
  X-=1
if len(S)==0:
  if X==0 and Y==0:
    print("Yes")
  else:
    print("No")
  sys.exit()

nx=[]
ny=[]
X=abs(X)
Y=abs(Y)

while S:
  step=0
  while S and S[0]=='T':
    d*=-1
    S=S[1:]
  while S and S[0]=='F':
    step+=1
    S=S[1:]
  if step==0:
    break
  if d==1:
    nx.append(step)
  else:
    ny.append(step)

if check_ans(nx,X) and check_ans(ny,Y):
  print("Yes")
else:
  print("No")

Solution (Python):
s = input()
x, y = map(int, input().split())
hL = len(s)
L = 2 * hL

dp = [[False] * (L + 1) for i in range(2)]
ndp = [False] * (L + 1)

def next(i):
    global s
    while i < hL and s[i] != 'T':
        i += 1
    i += 1
    return i

def c(x):
    global hL
    return x + hL

index = 0
t = 0
js = [1]
dp[0][c(0)] = True
dp[1][c(0)] = True
mini, maxi = 0, 1
while index < hL:
    ni = next(index)
    dx, index = ni - index - 1, ni
    for i in range(mini, maxi):
        for j in js:
            if -hL <= i + j * dx <= hL:
                nx = c(i + j * dx)
                ndp[nx] = ndp[nx] or dp[t][c(i)]
    for i in range(mini, maxi):
        dp[t][c(i)] = False
    dp[t], ndp = ndp, dp[t]
    mini, maxi = mini - dx, maxi + dx
    js = [-1, 1]
    t = (t + 1) % 2
print('Yes' if dp[0][c(x)] and dp[1][c(y)] else 'No')

Solution (Python):
st = input().split("T")
x, y = map(int, input().split())
XL = []; YL = []; t = 0
for i in range(len(st)):
    if i % 2 == 0:
        XL.append(len(st[i]))
    else:
        YL.append(len(st[i]))
    t += len(st[i])
if abs(x) + abs(y) > t:
    print("No")
else:
    T = max([abs(x), abs(y), t])
    xx = [[False for j in range(2*T+1)] for i in range(len(XL)+1)]
    yy = [[False for j in range(2*T+1)] for i in range(len(YL)+1)]
    xx[0][T]=True
    yy[0][T]=True
    xx[1][T+XL[0]]=True
    for i in range(2, len(XL)+1):
        for j in range(2*T+1):
            if j-XL[i-1] < 0:
                xx[i][j]=xx[i-1][j+XL[i-1]]
            elif j+XL[i-1] > 2*T:
                xx[i][j]=xx[i-1][j-XL[i-1]]
            else:
                xx[i][j]=xx[i-1][j-XL[i-1]] or xx[i-1][j+XL[i-1]]
    
    for i in range(1,len(YL)+1):
        for j in range(2*T+1):
            if j-YL[i-1] < 0:
                yy[i][j]=yy[i-1][j+YL[i-1]]
            elif j+YL[i-1] > 2*T:
                yy[i][j]=yy[i-1][j-YL[i-1]]
            else:
                yy[i][j]=yy[i-1][j-YL[i-1]] or yy[i-1][j+YL[i-1]]

    if xx[len(XL)][x+T] and yy[len(YL)][y+T]:
        print("Yes")
    else:
        print("No")

Solution (Python):

"""
https://beta.atcoder.jp/contests/abc082/tasks/arc087_b

"""
import sys
from sys import stdin
input = stdin.readline


def check_x(forwards):
    n = sum(forwards[::2])      
    dpx = [False] * (n * 2 + 1) 
    dpx[forwards[0] + n] = True 

    for f in forwards[2::2]:
        if f == 0:
            continue
        n_move = dpx[f:] + [False]*f
        p_move = [False]*f + dpx[:-f]
        t = [a or b for a, b in zip(n_move, p_move)]
        dpx = t
    return dpx, n


def check_y(forwards):
    n = sum(forwards[1::2])
    dpy = [False] * (n * 2 + 1)
    dpy[n] = True

    for f in forwards[1::2]:
        if f == 0:
            continue
        n_move = dpy[f:] + [False]*f
        p_move = [False]*f + dpy[:-f]
        t = [a or b for a, b in zip(n_move, p_move)]
        dpy = t
    return dpy, n


def solve(s, gx, gy):
    forwards = [len(x) for x in s.split('T')] 
    dpx, zx = check_x(forwards)
    dpy, zy = check_y(forwards)

    if zx < abs(gx):
        return 'No'
    if zy < abs(gy):
        return 'No'
    if dpx[gx + zx] is True and dpy[gy + zy] is True:
        return 'Yes'
    else:
        return 'No'


def main(args):
    s = input().strip()
    gx, gy = map(int, input().split())
    
    
    ans = solve(s, gx, gy)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):
s = list(input())
N = len(s)
gx, gy = map(int, input().split())
sx =sy = 0
mv_lists = [[], []]
def is_reach(mv_list, s, g):
	pos_set = set([s])
	for mv in mv_list:
		tmp_pos_set = set()
		for h in pos_set:
			tmp_pos_set.add(h - mv)
			tmp_pos_set.add(h + mv)
		pos_set = tmp_pos_set
	return g in pos_set
hv = 0
tmp_mv = 0
for o in s:
	if o == "F":
		tmp_mv += 1
	else:
		mv_lists[hv].append(tmp_mv)
		tmp_mv = 0
		hv ^= 1
else:
	mv_lists[hv].append(tmp_mv)
if s[0] == "F":
	sx += mv_lists[0][0]
	mv_lists[0] = mv_lists[0][1:]
if is_reach(mv_lists[0], sx, gx) and is_reach(mv_lists[1], sy, gy):
	print("Yes")
else:
	print("No")

Solution (Python):


command = list(input()) + ["*"]
x, y = map(int, input().split())

d = 0
move = 0
dx = []
dy = [abs(y)]

fst = True

fmove = 0

for c in command:
    if c == 'F':
        move += 1
    else:
        if fst:
            fmove = move
            fst = False
        else:
            if d % 2 == 0:
                dx.append(move)
            else:
                dy.append(move)
        move = 0
        d += 1

temp = -x + fmove
for x in sorted(dx, reverse=True):
    if temp < 0:
        temp += x
    else:
        temp -= x

adx = temp
temp = 0

for y in sorted(dy, reverse=True):
    if temp < 0:
        temp += y
    else:
        temp -= y
ady = temp

print("Yes" if adx == ady == 0 else "No")

Solution (Python):

nF = list(map(str, input().split('T')))
finalx, finaly = list(map(int, input().split()))

x = []
y = []
for i in range(len(nF)):
    if i % 2 == 0:
        x.append(len(nF[i]))
    else:
        y.append(len(nF[i]))

posy = [0]
if len(x) == 0:
    posx = [0]
elif len(x) == 1:
    posx = [x[0]]
else:
    posx = [x[0]]
    for i in range(1, len(x)):
        plus_x = list(map(lambda elem: elem + x[i], posx))
        minus_x = list(map(lambda elem: elem - x[i], posx))
        plus_x.extend(minus_x)
        posx = list(set(plus_x))

if len(y) >= 1:
    for i in range(len(y)):
        plus_y= list(map(lambda elem: elem + y[i], posy))
        minus_y= list(map(lambda elem: elem - y[i], posy))
        plus_y.extend(minus_y)
        posy = list(set(plus_y))

if finalx in posx and finaly in posy:
    print('Yes')
else:
    print('No')

Solution (Python):
import sys

def check(s, x, y):

    while s and s[0] == "F":
        s = s[1:]
        x -= 1
    while s and s[-1] == "T":
        s = s[:-1]

    d = 0
    vxs = []
    vys = []
    while s:
        while s and s[0] == "T":
            s = s[1:]
            d ^= 1
        l = 0
        while s and s[0] == "F":
            s = s[1:]
            l += 1
        if d:
            vys.append(l)
        else:
            vxs.append(l)

    def check1(vs, t):
        xs = [0]
        for v in vs:
            xs = set([x + v for x in xs]) | set([x - v for x in xs])
        return t in xs

    r = check1(vxs, x) and check1(vys, y)
    return r

if __name__ == "__main__":
    s = sys.stdin.readline().strip()
    x, y = [int(n) for n in sys.stdin.readline().strip().split()]
    if check(s, x, y):
        print("Yes")
    else:
        print("No")

Solution (Python):



def check(d, to_d):
  meu = set([0])
  for i in to_d:
    hoge = set([])
    for j in meu: hoge.add(j+i), hoge.add(j-i)
    meu = hoge
  return d in meu

S = map(len, raw_input().split('T'))
x, y = map(int, raw_input().split())
to_x, to_y = S[2::2], S[1::2]

print "Yes" if check(x-S[0],to_x) and check(y,to_y) else "No"

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

X = []
Y = []
n = 0

for i in s:
    if i == 'T':
        (X if len(X) == len(Y) else Y).append(n)
        n = 0
    else:
        n += 1
(X if len(X) == len(Y) else Y).append(n)
s = X[0]
t = 0
X = sorted(X[1:], reverse=True)
Y = sorted(Y, reverse=True)

for i in X:
    s += i * (-1 if s > x else 1)
for i in Y:
    t += i * (-1 if t > y else 1)

if s == x and t == y:
    print ('Yes')
else:
    print ('No')

Solution (Python):
import math

s=list(input().split('T'))
sn=[]

for i in range(len(s)):
    sn.append(len(s[i]))

x, y = map(int,input().split(' '))
x = x-sn[0]

snx=[]
sny=[]

for i in range(2,len(sn),2):
    snx.append(sn[i])

for i in range(1,len(sn),2):
    sny.append(sn[i])


xflg = 0
ansx=[]
prex=[0]
tmpx=[0]
for i in range(len(snx)):
    tmpx=set()
    for j in prex:
        tmpx.add(j+snx[i])
        tmpx.add(j-snx[i])
    prex=tmpx

if x in tmpx:
    xflg = 1


yflg = 0
prey=[0]
tmpy=[0]
for i in range(len(sny)):
    tmpy=set()
    for j in prey:
        tmpy.add(j+sny[i])
        tmpy.add(j-sny[i])
    prey=tmpy

if y in tmpy:
    yflg = 1

if xflg == 1 and yflg == 1:
    print("Yes")
else:
    print("No")

Solution (Python):



import sys

s = sys.stdin.readline().strip().split("T")
x, y = sys.stdin.readline().strip().split(" ")
x = int(x)
y = int(y)
move_all = [len(_s) for _s in s]
pos_x = set([move_all[0]])
pos_y = set([0])
move_x = move_all[2::2] or None
move_y = move_all[1::2] or None
if move_x:
    for move in move_x:
        pos_x = {_x + move for _x in pos_x} | {_x - move for _x in pos_x}
if move_y:
    for move in move_y:
        pos_y = {_y + move for _y in pos_y} | {_y - move for _y in pos_y}
if x in pos_x and y in pos_y:
    print("Yes")
else:
    print("No")

Solution (Python):
def check(a, numlist):
	goal = a+sum(numlist)
	if goal %2 == 1:
		return False
	goal /=2
	okset ={0}
	for x in numlist:
		addset = {ok+x for ok in okset}
		okset |= addset
	return goal in okset

s = [len(x) for x in input().split('T')]
x,y = [int(i) for i in input().split()]
x = x-s[0]
sx = s[2::2]
sy = s[1::2]
if check(x,sx) and check(y,sy):
	print("Yes")
else:
	print("No")