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



Sample input:
FTFFTFFF
4 2


Sample output:
Yes

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

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

	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 = [0], [0]
	for t in xt:
		xd2 = []
		for i in xd:
			xd2.append(i + t)
			xd2.append(i - t)
		xd = xd2
	for t in yt:
		yd2 = []
		for i in yd:
			yd2.append(i + t)
			yd2.append(i - t)
		yd = yd2

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


if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

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

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

	dp = set([(0,0,0)])
	for instr in s:
		if instr == 'F':
			dp = set([(t[0]+DIR[t[2]][0], t[1]+DIR[t[2]][1], t[2]) for t in dp])
		else:
			dp = set([(t[0], t[1], (t[2]+1)%4) for t in dp]) | set([(t[0], t[1], (t[2]-1+4)%4) for t in dp])

	for i in xrange(4):
		if (x, y, i) in dp:
			print 'Yes'
			return
	print 'No'

if __name__ == '__main__':
	main()

Submission status: Time Limit Exceeded

Solution code:
s=input()
x,y=map(int, input().split())
h=[(1,0),(0,1),(-1,0),(0,-1)]
nh=0
p={(0,0,0)}
for k in s:
    np=set()
    for i,j,ho in p:
        if k=='F':
            np.add((i+h[ho][0],j+h[ho][1],ho))
        else:
            np.add((i,j,(ho+1)%4))
            np.add((i,j,(ho+3)%4))
    p=np
ans='No'
for i,j,ho in p:
    if i==x and j==y:ans='Yes'
print(ans)

Submission status: Time Limit Exceeded

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

x_dp=[[0]*16001 for i in range(2)]
y_dp=[[0]*16001 for i in range(2)]

x_dp[0][8000+X[0]]=1

for i in range(1,len(X)):
    for j in range(16001):
        if 0<=j-X[i] and j-X[i]<=16000 :
            if x_dp[0][j-X[i]]==1:
                x_dp[1][j]=1
        if 0<=j+X[i] and j+X[i]<=16000 :
            if x_dp[0][j+X[i]]==1:
                x_dp[1][j]=1
    for k in range(16001):
        x_dp[0][k]=x_dp[1][k]
        x_dp[1][k]=0

y_dp[0][8000]=1
for i in range(1,len(Y)):
    for j in range(16001):
        if 0<=j-Y[i] and j-Y[i]<=16000 :
            if y_dp[0][j-Y[i]]==1:
                y_dp[1][j]=1
        if 0<=j+Y[i] and j+Y[i]<=16000 :
            if y_dp[0][j+Y[i]]==1:
                y_dp[1][j]=1
    for k in range(16001):
        y_dp[0][k]=y_dp[1][k]
        y_dp[1][k]=0

if x_dp[0][x+8000]==1 and y_dp[0][y+8000]==1:
    print("Yes")
else :
    print("No")

Submission status: Time Limit Exceeded

Solution code:
import sys
s=input()
A,B =map(int,input().split())
t = 0
k=0
L=len(s)
table=[[] for i in range(2)]
for i in range(len(s)):
    if s[i]=='F':
        k+=1
    else:
        table[t].append(k)
        t=(t+1)%2
        k=0
table[t].append(k)
Lx=set()
Lx.add(table[0][0])
for i in range(1,len(table[0])):
    x=table[0][i]
    T=set()
    for a in Lx:
        if -L<=a+x<=L:
            T.add(a+x)
        if -L<=a-x<=L:
            T.add(a-x) 
    Lx=T
flag=True
for a in Lx:
    if a==A:
        flag =False
if flag:
    print('No')
    sys.exit()

Lx=set()
Lx.add(0)
for i in range(len(table[1])):
    x=table[1][i]
    T=set()
    for a in Lx:
        if -L<=a+x<=L:
            T.add(a+x)
        if -L<=a-x<=L:
            T.add(a-x) 
    Lx=T
flag=True
for a in Lx:
    if a==B:
        flag =False
if flag:
    print('No')
    sys.exit()
print('Yes')

Submission status: Time Limit Exceeded

Solution code:
import sys
s=input()
A,B =map(int,input().split())
table=[[] for i in range(2)]#0が横1が縦
t = 0
k=0
for i in range(len(s)):
    if s[i]=='F':
        k+=1
    else:
        table[t].append(k)
        t=(t+1)%2
        k=0
table[t].append(k)
L=len(s)
Lx=[set() for i in range(len(table[0]) +1)]
Ly=[set() for i in range(len(table[1]) +1)]
Lx[0].add(0)
Ly[0].add(0)
for i in range(len(table[0])):
    x = table[0][i]
    if i==0:
        Lx[1].add(x)
        continue
    for a in Lx[i]:
        if -L<=a+x<=L:
            Lx[i+1].add(a+x)
        if -L<=a-x<=L:
            Lx[i+1].add(a-x) 
flag=True
for a in Lx[len(table[0])]:
    if a==A:
        flag =False
if flag:
    print('No')
    sys.exit()
    
for i in range(len(table[1])):
    x = table[1][i]
    for a in Ly[i]:
        if -L<=a+x<=L:
            Ly[i+1].add(a+x)
        if -L<=a-x<=L:
            Ly[i+1].add(a-x)
flag=True
for a in Ly[len(table[1])]:
    if a==B:
        flag =False
if flag:
    print('No')
    sys.exit()
    
print('Yes')

Submission status: Time Limit Exceeded

Solution code:
s=input()
a,b =map(int,input().split())
table=[[] for i in range(2)]#0が横1が縦
t = 0
k=0
for i in range(len(s)):
    if s[i]=='F':
        k+=1
    else:
        table[t].append(k)
        t=(t+1)%2
        k=0
table[t].append(k)
#booldp [2][x] 
L=2*len(s)+1
dp=[[False]*(L) for i in range(len(table[0])+1)]
dpy=[[False]*(L) for i in range(len(table[1])+1)]
dp[0][len(s)]=True
dpy[0][len(s)]=True
#print(table)
for i in range(len(table[0])):
    x=table[0][i]
    for z in range(L):
        if i==0 and 0<= z+x <L:
            dp[i+1][z+x] = dp[i][z]
            continue
        if 0<= z+x <L:
            dp[i+1][z+x] = dp[i+1][z+x]|dp[i][z]
        if 0<= z-x <L:
            dp[i+1][z-x] = dp[i+1][z-x]|dp[i][z]
for i in range(len(table[1])):
    x=table[1][i]
    for z in range(L):
        if 0<= z+x <L:
            dpy[i+1][z+x] = dpy[i+1][z+x]|dpy[i][z]
        if 0<= z-x <L:
            dpy[i+1][z-x] = dpy[i+1][z-x]|dpy[i][z]
if dp[len(table[0])][a+len(s)] and dpy[len(table[1])][b+len(s)]:
    print('Yes')
else:
    print('No')

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import sys
s=input()
a,b =map(int,input().split())
o=[0,0,0]#x,y,向き
X=[1,0,-1,0]
Y=[0,1,0,-1]
q = deque()
q.append([0,0,0,0])
while q:
    x,y,z,k= q.popleft()
    if x==a and y==b and k==len(s):
        print('Yes')
        sys.exit()
    if k==len(s):
        continue
    if s[k]=='F':
        q.append([x+X[z],y+Y[z],z,k+1])
    else:
        q.append([x,y,(z+1)%4,k+1])
        q.append([x,y,(z-1)%4,k+1])
print('No')

Submission status: Time Limit Exceeded

Solution code:
s = raw_input().strip()
n = len(s)
x, y = map(int, raw_input().strip().split())

st = 0
while st < n and s[st] == "F":
    st += 1

x -= st

X, Y = range(2)

x_blocks = []
y_blocks = []
curr = 0
cdi = X

for i in xrange(st, n):
    ch = s[i]
    if ch == "F":
        curr += 1
    else:
        if curr != 0:
            if cdi == X:
                x_blocks.append(curr)
            else:
                y_blocks.append(curr)
        curr = 0
        cdi = (cdi + 1) % 2
if curr != 0:
    if cdi == X:
        x_blocks.append(curr)
    else:
        y_blocks.append(curr)

x, y = abs(x), abs(y)
lxb, lyb = len(x_blocks), len(y_blocks)
sxb, syb = sum(x_blocks), sum(y_blocks)
x_blocks = sorted(x_blocks)
y_blocks = sorted(y_blocks)

if sum(x_blocks) < x or sxb % 2 != x % 2 or sum(y_blocks) < y or syb % 2 != y % 2:
    print "No"
    exit()

tx = (x + sxb) / 2
stack = [([0] * lxb, 0, -1)]
xgood = False

while stack:
    curr, val, top = stack.pop()
    if val == tx:
        xgood = True
        break
    for i in xrange(top + 1, lxb):
        if val + x_blocks[i] > tx:
            break
        nxt = curr[::]
        nxt[i] = 1
        stack.append((nxt, val + x_blocks[i], i))
    if xgood:
        break

if not xgood:
    print "No"
    exit()

ty = (y + syb) / 2
stack = [([0] * lyb, 0, -1)]
ygood = False

while stack:
    curr, val, top = stack.pop()
    if val == ty:
        ygood = True
        break
    for i in xrange(top + 1, lyb):
        if val + y_blocks[i] > ty:
            break
        nxt = curr[::]
        nxt[i] = 1
        stack.append((nxt, val + y_blocks[i], i))
    if ygood:
        break

if not ygood:
    print "No"
else:
    print "Yes"

Submission status: Time Limit Exceeded

Solution code:
s = input().split('T')
x, y = map(int, input().split())
x = abs(x-len(s[0]))
y = abs(y)
dx = []
for i in range(2, len(s), 2) :
    dx.append(len(s[i]))
dy = []
for i in range(1, len(s), 2) :
    dy.append(len(s[i]))

if sum(dx) < x or sum(dy) < y :
    print('No')
    exit()

xl = len(dx)
for i in range(1 << xl) :
    nx = 0
    for j in range(xl) :
        if i>>j & 1 :
            nx -= dx[j]
        else :
            nx += dx[j]
    if nx == x :
        break
else :
    print('No')
    exit()

yl = len(dy)
for i in range(1 << yl) :
    ny = 0
    for j in range(yl) :
        if i>>j & 1 :
            ny -= dy[j]
        else :
            ny += dy[j]
    if ny == y :
        break
else :
    print('No')
    exit()

print('Yes')

Submission status: Time Limit Exceeded

Solution code:
s = input().split('T')
x, y = map(int, input().split())
x = abs(x-len(s[0]))
y = abs(y)
i = 0
j = 0
dx = []
for i in range(2, len(s), 2) :
    dx.append(len(s[i]))
dy = []
for i in range(1, len(s), 2) :
    dy.append(len(s[i]))

if sum(dx) < x or sum(dy) < y :
    print('No')
    exit()

xl = len(dx)
for i in range(1 << xl) :
    nx = 0
    for j in range(xl) :
        if i>>j & 1 :
            nx -= dx[j]
        else :
            nx += dx[j]
    if nx == x :
        break
else :
    print('No')

yl = len(dy)
for i in range(1 << yl) :
    ny = 0
    for j in range(yl) :
        if i>>j & 1 :
            ny -= dy[j]
        else :
            ny += dy[j]
    if ny == y :
        break
else :
    print('No')

print('Yes')

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    s = input()
    x, y = map(int, input().split())

    s = s.split("T")
    s = [len(_) for _ in s]

    x_move = [ele for (i, ele) in enumerate(s) if i % 2 == 0]
    y_move = [ele for (i, ele) in enumerate(s) if i % 2 == 1]

    dpx = [[False for _ in range(sum(x_move) * 2 + 1)] for _ in range(len(x_move) + 1)]

    dpx[1][x_move[0]] = True
    for i, _x in enumerate(x_move):
        if i == 0:
            continue

        for j in range(-sum(x_move), sum(x_move) + 1):
            dpx[i + 1][j] = dpx[i][j - _x] or dpx[i][j + _x]

    dpy = [[False for _ in range(sum(y_move) * 2 + 1)] for _ in range(len(y_move) + 1)]

    dpy[0][0] = True
    for i, _y in enumerate(y_move):
        for j in range(-sum(y_move), sum(y_move) + 1):
            dpy[i + 1][j] = dpy[i][j - _y] or dpy[i][j + _y]

    try:
        dpx[len(x_move)][x] and dpy[len(y_move)][y]
    except:
        print("No")
        exit()

    if dpx[len(x_move)][x] and dpy[len(y_move)][y]:
        print("Yes")
    else:
        print("No")

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache, reduce
from operator import xor
from heapq import heappush, heappop
INF = float("inf")
sys.setrecursionlimit(10**7)

# 4近傍（右, 下, 左, 上）
dy4, dx4 = [0, -1, 0, 1], [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W
def ceil(a, b): return (a + b - 1) // b

YES = "Yes"
NO = "No"


def check(x, nums):
    x = abs(x)
    dp = [False] * (x + 1)
    dp[0] = True
    for y in nums:
        tmp = [False] * (x + 1)
        for i in range(len(dp)):
            if i + y < len(tmp):
                tmp[i + y] |= dp[i]
            if i - y >= 0:
                tmp[i - y] |= dp[i]
        dp = tmp
    return dp[x]


def solve(s, x, y):
    type_num = [(k, len(list(g))) for k, g in groupby(s)]
    if s[0] == "F":
        x -= type_num[0][1]
        type_num.pop(0)

    h, v = [], []
    now = 0
    for t, n in type_num:
        if t == "F":
            if now == 0:
                h.append(n)
            else:
                v.append(n)
        else:
            if n % 2 != 0:
                now = (now + 1) % 2
    return YES if check(x, h) and check(y, v) else NO


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

    print(solve(s, x, y))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache, reduce
from operator import xor
from heapq import heappush, heappop
INF = float("inf")
sys.setrecursionlimit(10**7)

# 4近傍（右, 下, 左, 上）
dy4, dx4 = [0, -1, 0, 1], [1, 0, -1, 0]


def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W
def ceil(a, b): return (a + b - 1) // b
YES = "Yes"
NO = "No"



memo = {}
def ok(i, l, now, target):
    if i >= len(l):
        return now == target
    key = (i, now)
    if key in memo:
        return memo[key]

    memo[key] = ok(i + 1, l, now + l[i], target) or ok(i + 1, l, now - l[i], target)
    return memo[key]


def solve(s, x, y):
    ss = [(k, len(list(g))) for k, g in groupby(s)]
    if ss[0][0] == "F":
        x -= ss[0][1]
        ss.pop(0)

    h, v = [], []
    now = 0
    for t, n in ss:
        if t == "F":
            if now == 0:
                h.append(n)
            else:
                v.append(n)
        else:
            if n % 2 != 0:
                now = (now + 1) % 2

    memo.clear()
    ans = ok(0, h, 0, x)
    memo.clear()
    ans &= ok(0, v, 0, y)
    return YES if ans else NO


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

    print(solve(s, x, y))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s=input()
x,y=[int(x) for x in input().split(' ')]
s_part=[len(x) for x in s.split('T')]
x_part=s_part[0::2]
y_part=s_part[1::2]
x_dp=[False]*8001
y_dp=[False]*8001
x_dp[x_part[0]]=True
y_dp[0]=True
for xx in x_part[1:]:
    x_dp = [x_dp[i] if i<xx else x_dp[i]|x_dp[i-xx] for i in range(len(x_dp))]
for yy in y_part:
    y_dp = [y_dp[i] if i<yy else y_dp[i]|y_dp[i-yy] for i in range(len(y_dp))]
flag=True
if((x+sum(x_part))%2==1 or (y+sum(y_part))%2==1): flag=False
else:
    flag=flag&x_dp[(x+sum(x_part))//2]&y_dp[(y+sum(y_part))//2]
print('Yes' if flag else 'No')

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10000)
inp=list(map(len,input().split("T")))
inx,iny=list(map(int,input().split()))
memo={}
def tg(g,n,t):
 if (tuple(g),n,t) in memo:
  return memo[(tuple(g),n,t)]
 if g==[]:
  if n==t:
   return True
  else:
   return False
 if tg(g[1:],n+g[0],t)or tg(g[1:],n-g[0],t):
  memo[(tuple(g),n,t)]=True
  return True
 else:
  memo[(tuple(g),n,t)]=False
  return False

if tg(inp[1::2],0,iny)and tg(inp[2::2],inp[0],inx):
 print("Yes")
else:
 print("No")

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**5)

s = raw_input()
ox,oy = map(int,raw_input().split(" "))

res = []
prev = ""
ss = list(s) + [" "]

if ss[0] == "T":
    res.append(0)
while ss:
    c = ss.pop(0)
    if c == "F":
        tc = 1
        while ss[0] == "F":
            c = ss.pop(0)
            tc += 1
        res.append(tc)
    elif c == "T":
        while ss[0] == "T":
            ss.pop(0)
            res.append(0)
    elif c == " ":
        break


xd = []
yd = []
for i,v in enumerate(res):
    if i%2 == 0:
        xd.append(v)
    else:
        yd.append(v)
ox -= xd[0]
xd.pop(0)



def dppp(tgt,ls):
    dp = [{} for i in range(4001)]

    def loop(pos,accm):
        if accm in dp[pos]:
            return dp[pos][accm]
        if pos == len(ls):
            if accm == tgt:
                return True
            else:
                return False


        if (loop(pos+1,accm + ls[pos]) or 
                loop(pos+1,accm - ls[pos])):
            return True
        else:
            return False
    return loop(0,0)


if dppp(ox,xd) and dppp(oy,yd):
    print "Yes"
else:
    print "No"

Submission status: Time Limit Exceeded

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


dx=[]
dy=[]

dir_is_X=True
step=0
for char in s:
        if char=='F':
                step+=1
        else:
                if dir_is_X:
                        dx.append(step)
                        dir_is_X=False
                        step=0
                else:
                        dy.append(step)
                        step=0
                        dir_is_X=True
#print(dx)
#print(dy)
bpos=len(s)+1
dpX=[[False for j in range(bpos*2+1)] for i in range(len(dx)+1)]
dpY=[[False for j in range(bpos*2+1)] for i in range(len(dy)+1)]

#10^7なら行けるっしょ
dpX[1][bpos+dx[0]]=True
dpY[0][bpos]=True

for i in range(1,len(dx)):
        for j in range(bpos*2+1):
                if dpX[i][j]:
                        dpX[i+1][j-dx[i]]=True
                        dpX[i+1][j+dx[i]]=True
for i in range(len(dy)):
        for j in range(bpos*2+1):
                if dpY[i][j]:
                        dpY[i+1][j-dy[i]]=True
                        dpY[i+1][j+dy[i]]=True
#print()
#for nums in dpX:
#       print(nums)
#rint()
#       print(nums)
if dpX[len(dx)][bpos+x] and dpY[len(dy)][bpos+y]:
        print("Yes")
else:
        print("No")

Submission status: Time Limit Exceeded

Solution code:
import re

d = [[1, 0], [0, -1], [-1, 0], [0, 1]]

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

s = re.sub(r'T+$', "", s)

k = s.split("T")
b = list(map(len, k))
# print(b)

ax = b[2::2]
ay = b[1::2]
# print(ax)
# print(ay)

mx = [{} for i in range(len(ax) + 1)]
mx[0] = { b[0]: True }

my = [{} for i in range(len(ay) + 1)]
my[0] = { 0: True }

for i, v in enumerate(ax):
    for k in mx[i].keys():
        mx[i + 1][k + v] = True
        mx[i + 1][k - v] = True

for i, v in enumerate(ay):
    for k in my[i].keys():
        my[i + 1][k + v] = True
        my[i + 1][k - v] = True


xok = x in mx[len(ax)]
yok = y in my[len(ay)]
res = xok and yok
print("Yes" if res else "No")

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(100000)
s = input().strip()
gx, gy = map(int, (input().split()))

flag = False

def dd(x, y, d, s):
    global flag
    if len(s) == 0:
        if x == gx and y == gy:
            flag = True
    else:
        if s[0] == 'F':
            if d == 0:
                dd(x+1, y, d, s[1:])
            elif d == 1:
                dd(x, y+1, d, s[1:])
            elif d == 2:
                dd(x-1, y, d, s[1:])
            else:
                dd(x, y-1, d, s[1:])
        elif s[0] == 'T':
            dd(x, y, (d+1)%4, s[1:])
            dd(x, y, (d-1)%4, s[1:])
dd(0, 0, 0, s)
if flag:
    print('Yes')
else:
    print('No')

Submission status: Time Limit Exceeded

Solution code:
import sys

s = input()
lmax = len(s)
x, y = [int(x) for x in input().split()]

while len(s) > 0 and s[0] == 'F':
    s = s[1:]
    x -= 1

if len(s) == 0:
    print('Yes' if x == 0 and y == 0 else 'No')
    sys.exit()

s = s[1:]

x = abs(x)
y = abs(y)
# print('goto', x, y, 'rest', s)

dx = []
dy = [0]
l = dy

for c in s:
    if c == 'T':
        if l[-1] == 0:
            del l[-1]
        l = dx if l == dy else dy
        l.append(0)
    else:
        l[-1] += 1


def dodp(l, x):
    dp = [0] * (lmax + 2)
    dp[0] = 1
    for li in l:
        ndp = [0] * (lmax + 2)
        for i in range(len(dp)):
            if (i - li >= 0 and dp[i - li] == 1):
                ndp[i] = 1
            elif (i + li < len(dp) and dp[i + li] == 1):
                ndp[i] = 1
        dp = ndp[:]
        #print(dp)
    #print()
    return dp[x] == 1

print('Yes' if dodp(dx, x) and dodp(dy, y) else 'No')

Submission status: Time Limit Exceeded

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

s2 = s.split('T')
sXs = list(map(len, s2[0::2]))
sYs = list(map(len, s2[1::2]))

memoX = [{sXs[0]}, set()]
for i, sX in enumerate(sXs[1:]):
    memoX[1 - i % 2] = set()
    for p in memoX[i % 2]:
        memoX[1 - i % 2].add(p + sX)
        memoX[1 - i % 2].add(p - sX)

memoY = [{0}, set()]
for i, sY in enumerate(sYs):
    memoY[1 - i % 2] = set()
    for p in memoY[i % 2]:
        memoY[1 - i % 2].add(p + sY)
        memoY[1 - i % 2].add(p - sY)

if x in memoX[1 - len(sXs) % 2] and y in memoY[len(sYs) % 2]:
    print('Yes')
else:
    print('No')

Submission status: Time Limit Exceeded

Solution code:
s = [len(x) for x in input().split('T')]
x, y = map(int, input().split())
len_s = len(s)
sx = [s[i] for i in range(len_s) if i % 2 == 0]
sy = [s[i] for i in range(len_s) if i % 2 == 1]

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



def is_reachable(p, s):
    origin = 8000
    if origin+p < 0:
        return False
        
    n = len(s)
    reachable = [[False for _ in range(origin * 2 + 1)] for i in range(n + 2)]
    reachable[0][origin] = True

    for i in range(n):
        for j in range(origin * 2 + 1):
            if not reachable[i][j]:
                continue
            if j - s[i] >= 0:
                reachable[i + 1][j - s[i]] = True
            if j + s[i] <= origin * 2:
                reachable[i + 1][j + s[i]] = True
    return reachable[n][p + origin] or reachable[n][-p + origin]


if is_reachable(x, sx) and is_reachable(y, sy):
    print('Yes')
else:
    print('No')

Submission status: Time Limit Exceeded

Solution code:
import sys
s = input()
X, Y = map(int, input().split())

sys.setrecursionlimit(8001)

dirs = [(1, 0), (0, -1), (-1, 0), (0, 1)]
S = list(map(len, s.split('T')))
N = len(S)
memo = {}
def process(i, x, y, d):
  if (i,x,y,d) in memo:
    return
  if i >= N:
    if x == X and y == Y:
      print('Yes')
      exit()
    return
  memo[(i,x,y,d)] = 1
  x += dirs[d][0] * S[i]
  y += dirs[d][1] * S[i]
  process(i+1, x, y, (d+1)%4)
  process(i+1, x, y, (d-1)%4)

process(0, 0, 0, 0)
print('No')

Submission status: Time Limit Exceeded

Solution code:
import sys
s = input()
X, Y = map(int, input().split())

sys.setrecursionlimit(8001)

dirs = [(1, 0), (0, -1), (-1, 0), (0, 1)]
S = list(map(len, s.split('T')))
N = len(S)
memo = {}
def process(i, x, y, d):
  if (i,x,y,d) in memo:
    return
  if i >= N:
    if x == X and y == Y:
      print('Yes')
      exit()
    return
  x += dirs[d][0] * S[i]
  y += dirs[d][1] * S[i]
  process(i+1, x, y, (d+1)%4)
  process(i+1, x, y, (d-1)%4)
  memo[(i,x,y,d)] = 1

process(0, 0, 0, 0)
print('No')

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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#0->x 1->y
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[i][j] i 個使ったときにjにいられるか
    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]
    #print(dp)
    return dp[-1][x]
fx = reach(Fxy[0],x,f=True)
fy = reach(Fxy[1],y)
yn(fx and fy)
#print(FT)
#print(dpx)
#print(dpy)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
#!python3

# input
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:
        # change
        while i < n and S[i] == "T":
            side = not side
            i += 1
        
        # forward
        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()