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().rstrip()
s += "T"
x, y = map(int, input().split())
cnt = 0
dir = 0
P = [{0},{0}]
first = True
for i in range(len(s)):
    if s[i]=="F":
        cnt+=1
    else:
        tmp = set()
        if first:
            for p in P[dir%2]:
                tmp |= {p+cnt}
            first=False
        else:
            for p in P[dir%2]:
                tmp |= {p+cnt, p-cnt}
        P[dir%2] =tmp
        dir += 1
        cnt = 0
if x in P[0] and y in P[1]:
    print("Yes")
else:
    print("No")

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

z = [0]
for i in s:
    if i=='F':
        z[-1] += 1
    else:
        z.append(0)
_y = len(z)//2
_x = len(z) - _y
len_s = len(s)
dp_x = [0 for _i in range((2*len_s+2))]
dp_x_to = [0 for _i in range((2*len_s+2))]
dp_y = [0 for _i in range((2*len_s+2))]
dp_y_to = [0 for _i in range((2*len_s+2))]

dp_x[z[0]] = 1
dp_y[0] = 1

for i in range(1, len(z)):
    if i%2==0:
        for j in range(-len_s, len_s+1):
            dp_x_to[j] = max(
                dp_x[j-z[i]],
                dp_x[j+z[i]]
            )
        dp_x = dp_x_to.copy()
    else:
        for j in range(-len_s, len_s+1):
            dp_y_to[j] = max(
                dp_y[j-z[i]],
                dp_y[j+z[i]]
            )
        dp_y = dp_y_to.copy()

if dp_x[x] == dp_y[y] == 1:
    print('Yes')
else:
    print('No')

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

dpx=[False]*16001
dpy=[False]*16001

dpx[8000],dpy[8000]=True,True

xxx=True
judge=True
s+='T'
cnt=0
for i in s:
  if i=='F':
      cnt+=1

  else:
    if judge:
        judge=False
    
    else:
        judge=True

    if cnt==0:
        xxx=False
        continue

    sin=[False]*16001
    if not judge:
        for j in range(16001):
            if dpx[j]==True:
                if not xxx:
                  sin[j-cnt]=True
                sin[j+cnt]=True
        dpx=sin
    
    else:
        for j in range(16001):
            if dpy[j]==True:
                sin[j-cnt]=True
                sin[j+cnt]=True
        dpy=sin

    cnt=0
    xxx=False




if dpx[x+8000] and dpy[y+8000]:
    print('Yes')

else:
    print('No')

Solution (Python):
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
ceil = math.ceil
FT = []
s = input()
x,y = ma()
if s[0]=="T":
    FT.append(0)
prev = s[0]
cnt=0
for i in range(len(s)):
    if s[i]==prev:
        cnt+=1
    else:
        FT.append(cnt)
        cnt=1
    prev=s[i]
FT.append(cnt)
Fxy = [[],[]]
idx=0
for i in range(len(FT)):
    if i%2==0:
        Fxy[idx].append(FT[i])
    else:
        if FT[i]%2==0:
            pass
        else:
            idx=(idx+1)%2
def reach(xls,x,f=False):
    l = len(xls)
    su = sum(xls)
    if abs(x) >su:
        return False
    dp = [[False]*(2*su+1) for _ in range(l+1)] 
    dp[0][0] =True

    for i in range(l):
        d = xls[i]
        if i==0 and f:
            if s[0]=="F":
                dp[1][d]=True
                continue
        for j in range(-su,su+1):
            dp[i+1][j] = dp[i][j-d] or dp[i][j+d]
    
    return dp[-1][x]
fx = reach(Fxy[0],x,f=True)
fy = reach(Fxy[1],y)
yn(fx and fy)

Solution (Python):
def main():
	s=input()
	X,Y=map(int,input().split())
	p=[]
	F=T=0
	if s[0]=="T":
		p.append(0)
	for i in range(len(s)):
		if s[i]=="F":
			if T:
				p.append(T)
			T=0
			F+=1
		else:
			if F:
				p.append(F)
			F=0
			T+=1
	p.append(max(F,T))
	a=[]
	b=[]
	now=0
	for i in range(len(p)//2):
		if now==0:
			a.append(p[i*2])
		else:
			b.append(p[i*2])
		if p[i*2+1]%2==1:
			if now==0:
				now=1
			else:
				now=0
	if len(p)%2==1:
		if now==0:
			a.append(p[-1])
		else:
			b.append(p[-1])
	dp1=[0]*(2*len(s)+1)
	dp2=[0]*(2*len(s)+1)
	dp1[len(s)]=1
	dp2[len(s)]=1
	first=0
	for x in a:
		dp1_copy=[0]*(2*len(s)+1)
		for i in range(2*len(s)+1):
			if i-x>=0:
				dp1_copy[i]=max(dp1[i-x],dp1_copy[i])
			if i+x<=2*len(s) and first:
				dp1_copy[i]=max(dp1[i+x],dp1_copy[i])
		dp1=dp1_copy.copy()
		first=1
	for x in b:
		dp2_copy=[0]*(2*len(s)+1)
		for i in range(2*len(s)+1):
			if i-x>=0:
				dp2_copy[i]=max(dp2[i-x],dp2_copy[i])
			if i+x<=2*len(s):
				dp2_copy[i]=max(dp2[i+x],dp2_copy[i])
		dp2=dp2_copy.copy()
	print("Yes" if dp1[len(s)+X] and dp2[len(s)+Y] else "No")
if __name__ == '__main__':
	main()

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

now = 1
YOKO = []
TATE = []
cnt=0
for i in range(N):
    if now==1:
        if S[i]=="F":
            cnt+=1
        elif S[i]=="T":
            if cnt!=0:
                YOKO.append(cnt)
            cnt=0
            now=-1*now
    elif now==-1:
        if S[i]=="F":
            cnt+=1
        elif S[i]=="T":
            if cnt!=0:
                TATE.append(cnt)
            cnt=0
            now=-1*now
if cnt>0:
    if now==1:
        YOKO.append(cnt)
    if now==-1:
        TATE.append(cnt)

NYOKO = len(YOKO)
NTATE = len(TATE)

dpYOKO = [0]*16001
dpTATE = [0]*16001
dpYOKO[0+8000]=1
dpTATE[0+8000]=1
if S[0]=="F":
    YOKOd = YOKO[1-1]
    newdp1 = [0]*16001
    for j in range(0,16001):
        if 0<=j-YOKOd:
            newdp1[j] = dpYOKO[j-YOKOd]
    dpYOKO = newdp1

    for i in range(2,NYOKO+1):
        YOKOd = YOKO[i-1]
        newdp1 = [0]*16001
        for j in range(0,16001):
            if (0<=j-YOKOd)and(j+YOKOd<=16000):
                newdp1[j] = max(dpYOKO[j-YOKOd],dpYOKO[j+YOKOd])
            elif (0<=j-YOKOd):
                newdp1[j] = dpYOKO[j-YOKOd]
            elif (j+YOKOd<=16000):
                newdp1[j] = dpYOKO[j+YOKOd]
        dpYOKO = newdp1
else:
    for i in range(1,NYOKO+1):
        YOKOd = YOKO[i-1]
        newdp1 = [0]*16001
        for j in range(0,16001):
            if (0<=j-YOKOd)and(j+YOKOd<=16000):
                newdp1[j] = max(dpYOKO[j-YOKOd],dpYOKO[j+YOKOd])
            elif (0<=j-YOKOd):
                newdp1[j] = dpYOKO[j-YOKOd]
            elif (j+YOKOd<=16000):
                newdp1[j] = dpYOKO[j+YOKOd]
        dpYOKO = newdp1

for i in range(1,NTATE+1):
    TATEd = TATE[i-1]
    newdp2 = [0]*16001
    for j in range(0,16001):
        if (0<=j-TATEd)and(j+TATEd<=16000):
            newdp2[j] = max(dpTATE[j-TATEd],dpTATE[j+TATEd])
        elif (0<=j-TATEd):
            newdp2[j] = dpTATE[j-TATEd]
        elif (j+TATEd<=16000):
            newdp2[j] = dpTATE[j+TATEd]
    dpTATE = newdp2

if dpYOKO[x+8000]==1 and dpTATE[y+8000]==1:
    print("Yes")
else:
    print("No")

Solution (Python):



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


def judge(l, m):
    s = sum(l)
    t = s - m if m > 0 else s + m
    if t < 0 or t % 2 == 1:
        return False

    t //= 2
    n = len(l)
    w = [[False] * (t + 1) for _ in range(n + 1)]
    w[0][0] = True
    for i in range(1, n + 1):
        for j in range(t + 1):
            if w[i - 1][j]:
                w[i][j] = True
                if j + l[i - 1] <= t:
                    w[i][j + l[i - 1]] = True

    return w[-1][-1]                


def main():
    n = len(S)
    d = 0
    for s in S:
        if s == "F":
            d += 1
        else:
            break

    dx, dy = [], []
    side = True
    i = d
    while i < n:
        
        while i < n and S[i] == "T":
            side = not side
            i += 1
        
        
        c = 0
        while i < n and S[i] == "F":
            c += 1
            i += 1
        if side:
            dx.append(c)
        else:
            dy.append(c)
    
    b1 = judge(dx, X - d)
    b2 = judge(dy, Y)
    ans = "Yes" if b1 and b2 else "No"
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
s = input()
x, y = map(int, input().split())
cnt = 0
xm = []
ym = []
flag = True

for i in range(len(s)):
    if s[i]=='F':
        cnt += 1
    else:
        if flag:
            xm.append(cnt)
            flag = False
        else:
            ym.append(cnt)
            flag = True
        
        cnt = 0

if flag:
    xm.append(cnt)
else:
    ym.append(cnt)
    
s = {0}

for i in range(len(xm)):
    ns = set()
    
    if i==0:
        for si in s:
            ns.add(si+xm[i])
    else:
        for si in s:
            ns.add(si-xm[i])
            ns.add(si+xm[i])
    
    s = ns

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

s = {0}

for i in range(len(ym)):
    ns = set()
    
    for si in s:
        ns.add(si-ym[i])
        ns.add(si+ym[i])
    
    s = ns
    
if y not in s:
    print('No')
    exit()

print('Yes')

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

D = map(len, S.split("T"))
X, Y = {next(D)}, {0}
for i, d in enumerate(D, 1):
    if i % 2 == 0:
        X = {x + d for x in X} | {x - d for x in X}
    else:
        Y = {y + d for y in Y} | {y - d for y in Y}

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

Solution (Python):
def f(m,l):
    tmpm=set()
    for x in m:
        tmpm.add(x+l);tmpm.add(x-l)
    return tmpm

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

while s and s[0]=="F":
    x-=1
    s=s[1:]
ss=[{0},{0}]
mode=0
l=0
for i in range(len(s)):
    if s[i]=="F": l+=1
    else:
        ss[mode]=f(ss[mode],l)    
        mode^=1
        l=0

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

Solution (Python):
def f(m,l):
    tmpm=set()
    for x in m:
        tmpm.add(x+l);tmpm.add(x-l)
    return tmpm

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

while s and s[0]=="F":
    x-=1
    s=s[1:]
ss=[{0},{0}]
mode=0
l=0
for i in range(len(s)):
    if s[i]=="F": l+=1
    else:
        ss[mode]=f(ss[mode],l)    
        mode^=1
        l=0

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

Solution (Python):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


S = sys.stdin.buffer.readline().decode().rstrip()
X, Y = list(map(int, sys.stdin.buffer.readline().split()))


d = 0
cnt = 0
xy = [[], []]
for c in S:
    if c == 'F':
        cnt += 1
    else:
        xy[d].append(cnt)
        cnt = 0
        d = d + 1 & 1
xy[d].append(cnt)
X -= xy[0].pop(0)


def can_make(moves, obj):
    if obj < -8000:
        return False
    obj = 1 << (8000 + obj)
    dp = 1 << 8000
    for mv in moves:
        if mv == 0:
            continue
        dp = (dp << mv) | (dp >> mv)
    return (dp & obj) > 0


ok = can_make(xy[0], X) and can_make(xy[1], Y)
if ok:
    print('Yes')
else:
    print('No')

Solution (Python):
s=list(input())
x,y=map(int,input().split())
f=0
t=0
s.append('T')
n=len(s)
xl=[]
yl=[]
for i in range(n):
    if s[i]=='F':
        f+=1
    else:
        if f>0:
            if t%2==0:
                if t==0:
                    x-=f
                else:
                    xl.append(f)
            else:
                yl.append(f)
            f=0
        t+=1
def solve(x,l):
    n=len(l)
    k=2*8000+1
    dp=[[-1 for p in range(k)] for q in range(n+1)]
    dp[0][k//2]=0
    for i in range(1,n+1):
        for j in range(k):
            if dp[i-1][j]>=0:
                dp[i][j-l[i-1]]=1
                dp[i][j+l[i-1]]=1
                
    if dp[-1][x+k//2]>=0:
        return True
    else:
        return False
if solve(x,xl) and solve(y,yl):
    print('Yes')
else:
    print('No')

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

n = len(s)

temp = 0
xys = [[],[]]

xy = 0

first = True


for i in range(n):
    if(s[i] == 'F'):
        temp += 1
    else:
        if(temp!=0):
            if(first):
                x -= temp
            else:
                xys[xy].append(temp)
            temp = 0
        xy = (xy + 1) % 2
        first = False
        
if(temp!=0):
    if(first):
        x -= temp
    else:
        xys[xy].append(temp)
        temp = 0

for xy,target in zip([0,1], [x,y]):
    nums = xys[xy]
    len_ = len(nums)
    
    if(len_ == 0):
        if(target == 0):
            continue
        else:
            print('No')
            exit()
    
    target = abs(target)
    sum_ = sum(nums)
    
    if((sum_ - target) % 2 == 1) | (target > sum_):
        print('No')
        exit()
        
    max = 1 + (sum_ - target)//2
    
    dp = [[0] * max for _ in range(len_ + 1)]
    dp[0][0] = 1
    
    for i in range(1, len_ + 1):
        dp[i][0] = 1
        temp = nums[i-1]
        for j in range(temp, max):
            dp[i][j] = dp[i-1][j-temp] | dp[i-1][j]
        
        if(dp[i][-1] == 1):
            break
            
        if(i == len_):
            print('No')
            exit()
    

print('Yes')

Solution (Python):
s = input().strip()
Xs = list(map(int, input().split()))
bs = [[],[]]
bb=t=0
for c in s:
    if c == 'T':
        bs[bb].append(t)
        bb=1-bb
        t = 0
    else:
        t += 1
bs[bb].append(t)
rr, bs[0] = bs[0][0],bs[0][1:]
for i in range(2):
    t = 1<<8000
    if i == 0:
        t = t<<rr
    for x in bs[i]:
        t = (t<<x) | (t>>x)
    if not (t>>(Xs[i]+8000))%2:
        print("No")
        break
else:
    print("Yes")

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

flg = 1
x_list = []
y_list = []
tmp = 0
for i, si in enumerate(s):
    if si == 'F':
        tmp += 1
    else:
        if flg == 1:
            x_list.append(tmp)
        else:
            y_list.append(tmp)
        flg *= -1
        tmp = 0
if flg == 1:
    x_list.append(tmp)
else:
    y_list.append(tmp)

x = sum(x_list) - x
y = sum(y_list) - y
if x % 2 != 0 or y % 2 != 0:
    print('No')
    exit()

if x < 0 or y < 0:
    print('No')
    exit()

x //= 2
y //= 2

if s[0] == 'F':
    x_list.reverse()
    x_list.pop()

dpx = [[0] * (x+1) for _ in range(len(x_list)+1)]
dpx[0][0] = 1
dpy = [[0] * (y+1) for _ in range(len(y_list)+1)]
dpy[0][0] = 1
for i in range(len(x_list)):
    for j in range(x+1):
        if dpx[i][j]:
            dpx[i+1][j] = 1
            if j + x_list[i] <= x:
                dpx[i+1][j+x_list[i]] = 1
for i in range(len(y_list)):
    for j in range(y+1):
        if dpy[i][j]:
            dpy[i+1][j] = 1
            if j + y_list[i] <= y:
                dpy[i+1][j+y_list[i]] = 1

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

Solution (Python):
import sys
s=input()
x,y=map(int,input().split())
s=s+'T'
dp=[[] for i in range(2)]
t=0
buf=0
for j in range(len(s)):
    if s[j]=='T':
        break

for i in range(j,len(s)):
    if s[i]=='F':
        buf+=1
    else:
        if buf!=0:
            dp[t].append(buf)
            buf=0
        t=1-t


xb=1<<(len(s)+j)
yb=1<<(len(s))

for i in range(len(dp[0])):
    xb=(xb<<dp[0][i])|(xb>>dp[0][i])
if xb & 1<<(len(s)+x)==0:
    print('No')
    sys.exit()
for i in range(len(dp[1])):
    yb=(yb<<dp[1][i])|(yb>>dp[1][i])

if yb & 1<<(len(s)+y)==0:
    print('No')
    sys.exit()
print('Yes')

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def no():
    print("No")
    exit()

def main():
    def ok(dir):
        goal = abs(xy[dir])
        over=total[dir]-goal
        if over<0 or over%2:
            return False
        if over==0:
            return True
        over//=2
        dp = [False] * (over + 1)
        dp[0] = True
        for d in dd[dir]:
            for i in range(d, over + 1):
                dp[i] |= dp[i - d]
            if dp[-1]: return True
        return False

    s = input()[:-1]
    xy = list(map(int, input().split()))

    dd = [[], []]
    total=[0,0]
    dir = 0
    cnt = 0
    first = True
    for c in s:
        if c == "F":
            cnt += 1
        if c == "T":
            if first:
                xy[0] -= cnt
                first = False
            else:
                if cnt: dd[dir].append(cnt)
                total[dir]+=cnt
            dir = 1 - dir
            cnt = 0
    if first:
        xy[0] -= cnt
    else:
        if cnt: dd[dir].append(cnt)
        total[dir] += cnt
    

    for dir in range(2):
        if not ok(dir): no()
    print("Yes")

main()

Solution (Python):

S = [len(x) for x in input().split('T')]

x,y = map(int,input().split(" "))
xstart = 0
dir = 0 
first = True

temp = 0
spin_count = 0
x_array = []
y_array = []

xstart = S[0]
for c in S[1:]:
    if c != 0:
        dir += 1
        dir %= 2
        temp = c
        if dir == 0:
            x_array.append(temp)
        else:
            y_array.append(temp)
    else:
        dir += 1






dpx ={}
dpx[xstart] = 1


for i in x_array:
    temp = []
    remove = []
    for k,v  in dpx.items():
        if v != -1:
            temp.append(k + i)
            temp.append(k - i)
            remove.append(k)
    for k in remove:
        dpx[k] = -1
    for k in temp:
        dpx[k] = 1

dpy ={}
dpy[0] = 1
for i in y_array:
    temp = []
    remove = []

    for k,v  in dpy.items():
        if v != -1:
            temp.append(k + i)
            temp.append(k - i)
            remove.append(k)
    for k in remove:
        dpy[k] = -1
    for k in temp:
        dpy[k] = 1

if x in dpx and y in dpy and dpx[x] == 1 and dpy[y] == 1 :
    print("Yes")
else:
    print("No")

Solution (Python):
def main():

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

    x1, y1 = 0, 0
    i = 0
    while i < n and s[i] == "F":
        x1 += 1
        i += 1

    count = 0
    UD = []
    LR = []
    while i < n:
        if s[i] == "T":
            count += 1
            i += 1
        else:
            d = 0
            while i < n and s[i] == "F":
                d += 1
                i += 1
            if count % 2 == 1: UD.append(d)
            else: LR.append(d)

    s1 = set([x1])
    for v in LR:
        temp = set()
        for u in s1:
            temp.add(u+v)
            temp.add(u-v)
        s1 = temp
    if x not in s1:
        return "No"
    s2 = set([0])
    for v in UD:
        temp = set()
        for u in s2:
            temp.add(u+v)
            temp.add(u-v)
        s2 = temp
    if y not in s2:
        return "No"

    return "Yes"




if __name__ == '__main__':
    print(main())

Solution (Python):
s=list(input())
x,y=map(int,input().split())
P="A"
A=[]
X=[0]
Y=[0]

cnt=0
for i in range(len(s)):
    if P=="A":
        if s[i]=="F":
            cnt+=1
        else:
            A.append(cnt)
            cnt=0
            P="Y"
    elif P=="X":
        if s[i]=="F":
            cnt+=1
        else:
            X.append(cnt)
            cnt=0
            P="Y"
    else:
        if s[i]=="F":
            cnt+=1
        else:
            Y.append(cnt)
            cnt=0
            P="X"
if P=="A":
    A.append(cnt)
elif P=="X":
    X.append(cnt)
else:
    Y.append(cnt)


DX1={}
DX1[0]=1
DX2={}
for i in X:
    for j in DX1:
        DX2[j+i]=1
        DX2[j-i]=1
    DX1=DX2
    DX2={}

if x-A[0] in DX1:
    flagx=1
else:
    flagx=0

DY1={}
DY1[0]=1
DY2={}
for i in Y:
    for j in DY1:
        DY2[j+i]=1
        DY2[j-i]=1
    DY1=DY2
    DY2={}
if y in DY1:
    flagy=1
else:
    flagy=0

if flagx*flagy==1:
    print("Yes")
else:
    print("No")

Solution (Python):
from collections import defaultdict
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve():
    S = input()[:-1]
    GX, GY = map(int, input().split())

    
    
    S_parsed = []
    prev = S[0]
    seq = 1
    for c in S[1:]:
        if c == "F":
            if c == prev:
                seq += 1
            else:
                prev = c
                if seq % 2 == 0:
                    pass
                else:
                    S_parsed.append("T")
                seq = 1
        elif c == "T":
            if c == prev:
                seq += 1
            else:
                S_parsed.append(seq)
                seq = 1
            prev = c
    else:
        if S[-1] == "F":
            S_parsed.append(seq)

    
    x_move = []
    y_move = []

    dir_x = True
    for c in S_parsed:
        if c == "T":
            dir_x = not dir_x
        else:
            if dir_x:
                x_move.append(c)
            else:
                y_move.append(c)

    
    dp_x = set()
    if len(x_move) > 0:
        if S[0] != "T":
            
            dp_x.add(x_move[0])
        else:
            
            dp_x.add(x_move[0])
            dp_x.add(-x_move[0])
    else:
        dp_x.add(0)

    for x in x_move[1:]:
        dp_x = {key+x for key in dp_x} | {key-x for key in dp_x}

    dp_y = set()
    if len(y_move) == 0:
        dp_y.add(0)
    else:
        dp_y.add(y_move[0])
        dp_y.add(-y_move[0])
    for y in y_move[1:]:
        dp_y = {key+y for key in dp_y} | {key-y for key in dp_y}

    if GX in dp_x and GY in dp_y:
        print("Yes")
    else:
        print("No")


solve()

Solution (Python):

from collections import defaultdict
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve():
    S = input()[:-1]
    GX, GY = map(int, input().split())

    
    
    S_parsed = []
    prev = S[0]
    seq = 1
    for c in S[1:]:
        if c == "F":
            if c == prev:
                seq += 1
            else:
                prev = c
                if seq % 2 == 0:
                    pass
                else:
                    S_parsed.append("T")
                seq = 1
        elif c == "T":
            if c == prev:
                seq += 1
            else:
                S_parsed.append(seq)
                seq = 1
            prev = c
    else:
        if S[-1] == "F":
            S_parsed.append(seq)

    
    x_move = []
    y_move = []

    dir_x = True
    for c in S_parsed:
        if c == "T":
            dir_x = not dir_x
        else:
            if dir_x:
                x_move.append(c)
            else:
                y_move.append(c)

    
    dp_x = defaultdict(bool)
    if len(x_move) > 0:
        if S[0] != "T":
            dp_x[x_move[0]] = True
        else:
            dp_x[x_move[0]] = True
            dp_x[-x_move[0]] = True
    else:
        dp_x[0] = True

    for x in x_move[1:]:
        tmp = defaultdict(bool)
        for key, val in dp_x.items():
            tmp[key+x] = True
            tmp[key-x] = True
        dp_x = tmp

    dp_y = defaultdict(bool)
    if len(y_move) == 0:
        dp_y[0] = True
    else:
        dp_y[y_move[0]] = True
        dp_y[-y_move[0]] = True
    for y in y_move[1:]:
        tmp = defaultdict(bool)
        for key, val in dp_y.items():
            tmp[key+y] = True
            tmp[key-y] = True
        dp_y = tmp

    if dp_x[GX]and dp_y[GY]:
        print("Yes")
    else:
        print("No")


solve()

Solution (Python):
from collections import defaultdict


import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve():
    S = input()[:-1]
    GX, GY = map(int, input().split())

    S_parsed = []
    prev = S[0]
    seq = 1
    for c in S[1:]:
        if c == "F":
            if c == prev:
                seq += 1
            else:
                prev = c
                if seq % 2 == 0:
                    pass
                else:
                    S_parsed.append("T")
                seq = 1
        elif c == "T":
            if c == prev:
                seq += 1
            else:
                S_parsed.append(seq)
                seq = 1
            prev = c
    else:
        if S[-1] == "F":
            S_parsed.append(seq)

    x_move = []
    y_move = []

    dir_x = True
    for c in S_parsed:
        if c == "T":
            dir_x = not dir_x
        else:
            if dir_x:
                x_move.append(c)
            else:
                y_move.append(c)

    dp_x = defaultdict(bool)
    if len(x_move) > 0:
        if S[0] != "T":
            dp_x[x_move[0]] = True
        else:
            dp_x[x_move[0]] = True
            dp_x[-x_move[0]] = True
    else:
        dp_x[0] = True

    for x in x_move[1:]:
        tmp = defaultdict(bool)
        for key, val in dp_x.items():
            tmp[key+x] = True
            tmp[key-x] = True
        dp_x = tmp

    dp_y = defaultdict(bool)
    if len(y_move) == 0:
        dp_y[0] = True
    else:
        dp_y[y_move[0]] = True
        dp_y[-y_move[0]] = True
    for y in y_move[1:]:
        tmp = defaultdict(bool)
        for key, val in dp_y.items():
            tmp[key+y] = True
            tmp[key-y] = True
        dp_y = tmp

    if dp_x[GX]and dp_y[GY]:
        print("Yes")
    else:
        print("No")


solve()

Solution (Python):
S=input()
S+="E"
x,y=map(int,input().split())
go=[[],[]]
idx=0
while S[idx]=="F":
    x-=1
    idx+=1
g=1
idx+=1
while idx<len(S):
    now=0
    while S[idx]=="F":
        now+=1
        idx+=1
    if now>0:
        go[g].append(now)
    g=(g+1)%2
    idx+=1
def check(L,m):
    dp={0}
    for x in L:
        dpc=dp.copy()
        dp=set()
        for now in dpc:
            dp|={now+x,now-x}
    if m in dp:
        return True
    return False
if check(go[0],x) and check(go[1],y):
    print("Yes")
else:
    print("No")

Solution (Python):
s = input().split("T")
x,y = map(int,input().split())
xmove = []
ymove = []
for i in range(len(s)):
    if i % 2 == 1:
        ymove.append(s[i].count("F"))
    else:
        xmove.append(s[i].count("F"))


def solve(l,cor):
    dp = {0}
    for c in l:
        dp = {c+i for i in dp}|{i-c for i in dp}
    return cor in dp

if solve(xmove[1:],x-xmove[0]) and solve(ymove,y):
    print("Yes")
else:
    print("No")

Solution (Python):
from functools import reduce


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

C = [len(fs) for fs in s.split('T')]



def f(cs, t):
    return t in reduce(
        lambda acc, c: {i + c for i in acc} | {i - c for i in acc},
        cs,
        {0}
    )


ans = (
    'Yes' if f(C[2::2], x - C[0]) and f(C[1::2], y) else
    'No'
)

print(ans)

Solution (Python):

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

C = [len(fs) for fs in s.split('T')]



def f(cs, t):
    dp = {0}
    for c in cs:
        dp = {i + c for i in dp} | {i - c for i in dp}
    return t in dp


ans = (
    'Yes' if f(C[2::2], x - C[0]) and f(C[1::2], y) else
    'No'
)

print(ans)

Solution (Python):



def main():
	s = raw_input()
	s2 = raw_input()
	x, y = [int(x) for x in s2.split()]
	

	xt, yt = [], []
	d, step = 0, 0
	for c in s:
		if c == 'T':
			if d == 0:
				xt.append(step)
			else:
				yt.append(step)
			step = 0
			d = 1 - d
		else:
			step += 1
	if step > 0:
		if d == 0:
			xt.append(step)
		else:
			yt.append(step)

	x -= xt[0]
	xt = xt[1:]
	xd, yd = set([0]), set([0])
	for t in xt:
		xd2 = set()
		for i in xd:
			xd2.add(i + t)
			xd2.add(i - t)
		xd = xd2
	for t in yt:
		yd2 = set()
		for i in yd:
			yd2.add(i + t)
			yd2.add(i - t)
		yd = yd2

	if x in xd and y in yd:
		print 'Yes'
	else:
		print 'No'


if __name__ == '__main__':
	main()

Solution (Python):
ai = lambda: list(map(int,input().split()))
ai_ = lambda: [int(x)-1 for x in input().split()]


s = input().split('T')
x,y = ai()

xx,yy = [],[]
c = 0
for i in range(len(s)):
    if i%2:
        yy.append(len(s[i]))
    else:
        xx.append(len(s[i]))

anx = set()
for i in range(len(xx)):
    if i == 0:
        anx = {xx[i]}
    else:
        aanx = set()
        for j in anx:
            aanx.add(j+xx[i])
            aanx.add(j-xx[i])
        anx = aanx
any = {0}
for i in range(len(yy)):
    aany = set()
    for j in any:
        aany.add(j+yy[i])
        aany.add(j-yy[i])
    any = aany

if x in anx and y in any:
    print('Yes')
else:
    print('No')

Solution (Python):
s=input()
x,y=map(int, input().split())
sl=[len(i) for i in s.split('T')]
a,b={0:1},{0:1}
for i in sl[2::2]:
    if i==0:continue
    a2={}
    for j in a.keys():
        a2[j+i]=1
        a2[j-i]=1
    a=a2
for i in sl[1::2]:
    if i==0:continue
    b2={}
    for j in b.keys():
        b2[j+i]=1
        b2[j-i]=1
    b=b2
if x-sl[0] in a and y in b:print('Yes')
else:print('No')

Solution (Python):
from collections import defaultdict

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

X = []
Y = []
judge = "x"
l = 0
for i in s:
	if i == "F":
		l += 1
	else:
		if judge == "x":
			if l != 0:
				X.append(l)
			judge = "y"
		else:
			if l != 0:
				Y.append(l)
			judge = "x"
		l = 0

if judge == "x":
	if l != 0:
		X.append(l)
else:
	if l != 0:
		Y.append(l)

jj = "bad"
if len(s) >= 2:
	if s[0] == "T":
		jj = "good"


dx = defaultdict(int)
if X != []:
	if jj == "good":
		dx[X[0]] = 1
		dx[-X[0]] = 1
	else:
		dx[X[0]] = 1
	X.remove(X[0])
else:
	dx[0] = 1

for i in range(len(X)):
	D = defaultdict(int)
	for j in dx:
		if dx[j] == 1:
			D[j + X[i]] = 1
			D[j - X[i]] = 1
	dx = D


dy = defaultdict(int)
if Y != []:
	dy[Y[0]] = 1
	dy[-Y[0]] = 1
	Y.remove(Y[0])
else:
	dy[0] = 1

for i in range(len(Y)):
	D = defaultdict(int)
	for j in dy:
		if dy[j] == 1:
			D[j + Y[i]] = 1
			D[j - Y[i]] = 1
	dy = D

if dx[x] == 1 and dy[y] == 1:
	print("Yes")
else:
	print("No")

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

MoveX, MoveY = [], []
dir = "X"
count = 0
for i in range(len(s)): 
    if s[i] == "T":
        if count:
            if dir == "X": MoveX.append(count)
            else: MoveY.append(count)
        count = 0
        dir = ("Y" if dir == "X" else "X")      
    else:
        count += 1
if s[-1] == "F":
    if dir == "X": MoveX.append(count)
    else: MoveY.append(count)

iniX = 0
if s[0] == "F":
    iniX = MoveX.pop(0)

def DP(i, z, ini, Move = list):
    canMove = set([ini])
    for j in range(i):
        temp = []
        for item in canMove:
            temp.append(item+Move[j])
            temp.append(item-Move[j])
        canMove = set(temp)
    return z in canMove

if DP(len(MoveX), x, iniX, MoveX) and DP(len(MoveY), y, 0, MoveY): print("Yes")
else: print("No")