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: Accepted

Solution code:
def main():
    """
    Tの命令 組合せ: 2^N_T
    max_N_T: 8000

    |x|,|y|<=8000
    -> (x,y) 組合せ: 10^8 < 16,000^2 < 10^9
    """
    s = input()
    x, y = map(int, input().split())

    f(s, x, y)
    return
    solve(s, x, y)


def f(s, x, y):
    # Fの数をまとめる
    f_counts = [
        len(fs)
        for fs in s.split("T")
    ]
    # 曲がる方向が90°より、x,yに分解
    x_counts = []
    y_counts = []
    for i, count in enumerate(f_counts):
        if i % 2 == 0:
            x_counts.append(count)
        else:
            y_counts.append(count)

    x_dp = [{} for _ in range(len(x_counts))]
    y_dp = [{} for _ in range(len(y_counts) + 1)]
    pos_x = x_counts.pop(0)
    x_dp[0][pos_x] = True
    y_dp[0][0] = True

    for i, count in enumerate(x_counts):
        for pos in x_dp[i].keys():
            x_dp[i + 1][pos + count] = True
            x_dp[i + 1][pos - count] = True
    for i, count in enumerate(y_counts):
        for pos in y_dp[i].keys():
            y_dp[i + 1][pos + count] = True
            y_dp[i + 1][pos - count] = True

    if x_dp[-1].get(x) and y_dp[-1].get(y):
        print("Yes")
    else:
        print("No")


def solve(s, x, y):
    pos_x, pos_y = 0, 0
    dir_x, dir_y = 1, 0
    for q in s:
        if q == "F":
            pos_x += dir_x
            pos_y += dir_y
        else:
            pass

    if (pos_x, pos_y) == (x, y):
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    main()

Submission status: Accepted

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


def part_sum0(a, A):
    # 初期化
    N = len(a)
    dp = [[0 for i in range(A + 1)] for j in range(N + 1)]
    dp[0][0] = 1

    # DP
    for i in range(N):
        for j in range(A + 1):
            if a[i] <= j:
                dp[i + 1][j] = dp[i][j - a[i]] or dp[i][j]
            else:
                dp[i + 1][j] = dp[i][j]
    return dp[N][A]


fs = s.split('T')
f_num = [len(f) for f in fs]
dir_lst = [[], []]
for i in range(len(f_num)):
    dir_lst[i % 2].append(f_num[i])
x_lst = dir_lst[0]
x -= x_lst[0]
x_lst = x_lst[1:]
y_lst = dir_lst[1]

can_x = False
can_y = False

dx = abs(sum(x_lst)) - abs(x)
if (dx % 2 == 1) or (dx < 0):
    pass
elif dx == 0:
    can_x = True
else:
    if part_sum0(x_lst, dx // 2):
        can_x = True

if can_x:
    dy = abs(sum(y_lst)) - abs(y)
    if (dy % 2 == 1) or (dy < 0):
        pass
    elif dy == 0:
        can_y = True
    else:
        if part_sum0(y_lst, dy // 2):
            can_y = True

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

Submission status: Accepted

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

fs = s.split('T')
f_num = [len(f) for f in fs]
dir_lst = [[], []]
for i in range(len(f_num)):
    dir_lst[i % 2].append(f_num[i])
x_lst = dir_lst[0]
x -= x_lst[0]
x_lst = x_lst[1:]
y_lst = dir_lst[1]

can_x = False
can_y = False

s = set([0])
for x_ in x_lst:
    s_ = set()
    for e in s:
        s_.add(e+x_)
        s_.add(e-x_)
    s = s_
if x in s:
    can_x = True

s = set([0])
for y_ in y_lst:
    s_ = set()
    for e in s:
        s_.add(e+y_)
        s_.add(e-y_)
    s = s_
if y in s:
    can_y = True

if can_x and can_y:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
s = input().split('T')
gx, gy = tuple(map(int, input().split()))

dir = 0
xmoves = []
ymoves = []
for moves in s:
    if dir % 2 == 0:
        xmoves.append(len(moves))
    else:
        ymoves.append(len(moves))
    dir += 1

curx, cury = xmoves.pop(0), 0

for move in sorted(xmoves, reverse=True):
    if curx > gx:
        curx -= move
    else:
        curx += move

for move in sorted(ymoves, reverse=True):
    if cury > gy:
        cury -= move
    else:
        cury += move

if curx == gx and cury == gy:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
import numpy as np

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

#print(s)

susumu = [0]
for moji in s:
    if moji == 'F':
        susumu[-1] += 1
    else:
        susumu.append(0)

#print(susumu)

syokiti = susumu[0]

susumu_NS = [i for i in susumu[1::2] if i!=0]
susumu_EW = [i for i in susumu[2::2] if i!=0]

x -= syokiti

#print(susumu)
#print(susumu_NS)
#print(susumu_EW)
# ＥＷの部分和でx? NSの部分和でy？

LL = len(s)+2
dp = np.zeros(LL*2, dtype=bool)
dp[LL] = 1
for j in range(len(susumu_NS)):
    dpleft =np.r_[dp[susumu_NS[j]:],np.zeros(susumu_NS[j], dtype=bool)]
    dpright=np.r_[np.zeros(susumu_NS[j], dtype=bool),dp[:-susumu_NS[j]]]
    dp = dpleft|dpright
#    print(dp)

can_y = 0
if dp[y+LL] != False:
    can_y = 1
#print(dp)
#print(susumu_NS)

#########################################
LL = len(s)+2
dp = np.zeros(LL*2, dtype=bool)
dp[LL] = 1
for j in range(len(susumu_EW)):
    dpleft =np.r_[dp[susumu_EW[j]:],np.zeros(susumu_EW[j], dtype=bool)]
    dpright=np.r_[np.zeros(susumu_EW[j], dtype=bool),dp[:-susumu_EW[j]]]
    dp = dpleft|dpright
#    print(dp)

can_x = 0
if dp[x+LL] != False:
    can_x = 1

#print(dp)
#print(susumu_EW)

if can_x == 1 & can_y == 1:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
s=list(map(len,input().split("T")))
x,y=map(int,input().split())
def f(t,w):
    m=[{} for i in range(len(t)+1)]
    m[0][0]=1
    for i in range(len(t)):
        for z in m[i]:
            m[i+1][z+t[i]]=1
            m[i+1][z-t[i]]=1
    return w in m[len(t)]
print(["No","Yes"][f(s[2::2],x-s[0]) and f(s[1::2],y)])

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def M(n): return ModInt(n)
def MI(): return M(I())


def main():
    s = S()
    x,y = LI()
    a = [len(_) for _ in s.split('T')]
    l = len(a)
    x -= a[0]
    x = abs(x)
    y = abs(y)

    s = set([0])
    for i in range(2,l,2):
        t = set()
        for c in s:
            t.add(c+a[i])
            t.add(c-a[i])
        s = t
    if x not in s and -x not in s:
        return 'No'
    s = set([0])
    for i in range(1,l,2):
        t = set()
        for c in s:
            t.add(c+a[i])
            t.add(c-a[i])
        s = t
    if y not in s and -y not in s:
        return 'No'


    return 'Yes'



print(main())

Submission status: Accepted

Solution code:
def F(a, d):
	s = set([0])
	for i in a:
		t = set([])
		for j in s:
			t.add(j - i)
			t.add(j + i)
		s = t
	return d in s
s = raw_input()
a = map(len, s.split('T'))
x, y = map(int, raw_input().split())
print ['No', 'Yes'][F(a[2::2], x - a[0]) and F(a[1::2], y)]

Submission status: Accepted

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

offset = 0
continuousMovement = 0
verticalMovement = []
horizontalMovement = []
flag = "start"

for letter in s:
    if letter == "F":
        continuousMovement += 1
    else:
        if flag == "vert":
            flag = "hori"
            verticalMovement.append(continuousMovement)
        elif flag == "hori":
            flag = "vert"
            horizontalMovement.append(continuousMovement)
        else:
            offset = continuousMovement
            flag = "vert"
        continuousMovement = 0
else:
    if flag == "vert":
        verticalMovement.append(continuousMovement)
    elif flag == "hori":
        horizontalMovement.append(continuousMovement)
    else:
        offset = continuousMovement

distanceX = x - offset
distanceY = y

vertMovementVariation = {0, }
for x in verticalMovement:
    newVariation = set()
    for y in vertMovementVariation:
        newVariation.add(y + x)
        newVariation.add(y - x)
    vertMovementVariation = newVariation

horiMovementVariation = {0, }
for x in horizontalMovement:
    newVariation = set()
    for y in horiMovementVariation:
        newVariation.add(y + x)
        newVariation.add(y - x)
    horiMovementVariation = newVariation

if distanceX in horiMovementVariation and distanceY in vertMovementVariation:
    print "Yes"
else:
    print "No"

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-     
import sys

s = input()
dstx, dsty = list(map(int,input().split()))
x = []
y = []
tmp = 0
xy = 0
for i in range(len(s)):
    if(s[i]=='F'):
        tmp += 1
    elif(s[i]=='T' and xy==0):
        x.append(tmp)
        tmp = 0
        xy = 1
    elif(s[i]=='T' and xy==1):
        y.append(tmp)
        tmp = 0
        xy = 0
else:
    if(xy==0):
        x.append(tmp)
    else:
        y.append(tmp)
dpx = [{} for i in range(len(x))]
dpy = [{} for i in range(len(y)+1)]
dpx[0][x[0]] = True
dpy[0][0] = True
for i, dx in enumerate(x[1:]):
    for j in dpx[i]:
        dpx[i+1][j+dx] = True
        dpx[i+1][j-dx] = True
if(dstx not in dpx[-1]):
    print('No')
    sys.exit()
for i, dy in enumerate(y):
    for j in dpy[i]:
        dpy[i+1][j+dy] = True
        dpy[i+1][j-dy] = True
    
if(dstx not in dpx[-1] or dsty not in dpy[-1]):
    print('No')
else:
    print('Yes')

Submission status: Accepted

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

a = [0, 1]

fs = S.split("T")
f_num = [len(f) for f in fs]

dir_lst = [[], []]

for i in range(len(f_num)):
    dir_lst[i%2].append(f_num[i])
    

x_lst = dir_lst[0].copy()
y_lst = dir_lst[1].copy()

nowx = x_lst.pop(0)
x_lst.sort()
while x_lst != []:
    if nowx > x:
        nowx -= x_lst.pop()
    else:
        nowx += x_lst.pop()

y_lst.sort()
nowy = 0
while y_lst != []:
    if nowy > y:
        nowy -= y_lst.pop()
    else:
        nowy += y_lst.pop()

if nowx == x and nowy == y:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
# coding:utf-8                                                                  

alls = raw_input()
tarx, tary = map(int, raw_input().split())

ss = alls.split("T")

xs = []
ys = []
posx = set()
posy = set()
posy.add(0)
for i, s in enumerate(ss):
    if i == 0:
        posx.add(len(s))
    elif i % 2 == 0:
        xs.append(len(s))
    else:
        ys.append(len(s))

for x in xs:
    tmpx = set()
    for px in posx:
        tmpx.add(px+x)
        tmpx.add(px-x)
    posx = tmpx

for y in ys:
    tmpy = set()
    for py in posy:
        tmpy.add(py+y)
        tmpy.add(py-y)
    posy = tmpy

if tarx in posx and tary in posy:
    print "Yes"
else:
    print "No"

Submission status: Accepted

Solution code:
import sys
from collections import deque

s = deque(input() + 'T')
X, Y = map(int, input().split())

while True:
    if s[0] == 'T':
        break
    s.popleft()
    X -= 1

dirx = True
x_list = []
y_list = []
count = 0
for c in s:
    if c == 'F':
        count += 1
    else:
        if count > 0:
            x_list.append(count) if dirx else y_list.append(count)
            count = 0
        dirx = not(dirx)

if (sum(x_list) + X) % 2 != 0 or (sum(y_list) + Y) % 2 != 0 or sum(x_list) < abs(X) or sum(y_list) < abs(Y):
    print('No')
    sys.exit()

dx = (sum(x_list) - abs(X)) // 2
dp_x = [False for _ in range(dx + 1)]
dp_x[0] = True
for x in x_list:
    for i in range(dx - x, -1, -1):
        if dp_x[i]:
            dp_x[i + x] = True
    if dp_x[dx]:
        break
if not dp_x[dx]:
    print('No')
    sys.exit()

dy = (sum(y_list) - abs(Y)) // 2
dp_y = [False for _ in range(dy + 1)]
dp_y[0] = True
for y in y_list:
    for i in range(dy - y, -1, -1):
        if dp_y[i]:
            dp_y[i + y] = True
    if dp_y[dy]:
        break
if not dp_y[dy]:
    print('No')
    sys.exit()

print('Yes')

Submission status: Accepted

Solution code:
import sys
from collections import deque

s = deque(input() + 'T')
X, Y = map(int, input().split())

while True:
    if s[0] == 'T':
        break
    s.popleft()
    X -= 1

dirx = True
x_list = []
y_list = []
count = 0
for c in s:
    if c == 'F':
        count += 1
    else:
        if count > 0:
            x_list.append(count) if dirx else y_list.append(count)
            count = 0
        dirx = not(dirx)

if (sum(x_list) + X) % 2 != 0 or (sum(y_list) + Y) % 2 != 0 or sum(x_list) < abs(X) or sum(y_list) < abs(Y):
    print('No')
    sys.exit()

dx = (sum(x_list) + abs(X)) // 2
dy = (sum(y_list) + abs(Y)) // 2

dp_x = [False for _ in range(dx + 1)]
dp_x[0] = True
for x in x_list:
    for i in range(dx - x, -1, -1):
        if dp_x[i] and i + x <= dx:
            dp_x[i + x] = True
if not dp_x[dx]:
    print('No')
    sys.exit()

dp_y = [False for _ in range(dy + 1)]
dp_y[0] = True
for y in y_list:
    for i in range(dy - y, -1, -1):
        if dp_y[i] and i + y <= dy:
            dp_y[i + y] = True
if not dp_y[dy]:
    print('No')
    sys.exit()

print('Yes')

Submission status: Accepted

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

h,v = [],[]
hv = 0
for ft in s:
    if hv % 2 == 0:
        h.append(len(ft))
    else:
        v.append(len(ft))
    hv += 1
    
ax,ay = False,False
tx,ty = h.pop(0),0

for i in sorted(h,reverse=True):
    if tx > x:
        tx -= i
    else:
        tx += i
if tx == x:
    ax = True

for i in sorted(v,reverse=True):
    if ty > y:
        ty -= i
    else:
        ty += i
if ty == y:
    ay = True
    
if ax and ay:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
s=list(raw_input().split("T"))
x,y=map(int,raw_input().split())
lx=[len(_) for _ in s[2::2]]
ly=[len(_) for _ in s[1::2]]

dpx=set()
dpx.add(len(s[0]))
for i in lx:
	tmp=set()
	for j in dpx:
		tmp.add(j+i)
		tmp.add(j-i)
	dpx=tmp


dpy=set()
dpy.add(0)
for i in ly:
	tmp=set()
	for j in dpy:
		tmp.add(j+i)
		tmp.add(j-i)
	dpy=tmp

print "Yes" if x in dpx and y in dpy else "No"

Submission status: Accepted

Solution code:
s=list(raw_input().split("T"))
#print s
x,y=map(int,raw_input().split())
cx,cy=len(s[0]),0
lx=[len(_) for _ in s[2::2]]
ly=[len(_) for _ in s[1::2]]
#print lx
#print ly

memo_x=[set((cx,))]
for ind_i,i in enumerate(lx):
	next_memo_x = set()
	for j in memo_x[ind_i]:
		next_memo_x.add(j+i)		
		next_memo_x.add(j-i)
	memo_x.append(next_memo_x)
#print memo_x


memo_y=[set((cy,))]
for ind_i,i in enumerate(ly):
	next_memo_y = set()
	for j in memo_y[ind_i]:
		next_memo_y.add(j+i)		
		next_memo_y.add(j-i)
	memo_y.append(next_memo_y)
#print memo_y

if x in memo_x[-1] and y in memo_y[-1]:
	print "Yes"
else:
	print "No"

Submission status: Accepted

Solution code:
s=list(raw_input().split("T"))
x,y=map(int,raw_input().split())
cx,cy=len(s[0]),0
#print cx,cy
sx=s[2::2]
sy=s[1::2]
#print sx
#print sy
sx1=[len(_) for _ in sx]
sy1=[len(_) for _ in sy]
sx1.sort()
sx1.reverse()
sy1.sort()
sy1.reverse()
#print sx1
#print sy1

for i in sx1:
	if cx<x:
		cx+=i
	else:
		cx-=i

#print cx

for i in sy1:
	if cy<y:
		cy+=i
	else:
		cy-=i

#print cy
print "Yes" if cx==x and cy==y else "No"

Submission status: Accepted

Solution code:
def d_FT_Robot(s, x, y):
    x_start = 0  # 最初の1回でどれだけ右に動いたか(ロボットは最初に右を向いているので、特別扱いする)
    f_count = 0  # Fが出てきた回数
    movex = []  # x軸方向に何度動くか(初回を除く)
    movey = []  # y軸方向に何度動くか
    is_directed_x = True  # x軸方向を向いてるか
    is_appeared_t = False  # 注目している文字の前にTが1度でも現れたか
    for c in s:
        if c == "F":
            f_count += 1
            continue
        elif c == 'T':
            if f_count > 0:
                if is_directed_x:
                    if is_appeared_t:
                        movex.append(f_count)
                    else:
                        # 最初にTが現れるまでに出てきたFの数によって
                        # 進んだところのx座標は、必ず到達できる
                        # そのためのちにdp_x[0][x_start]=Trueとする
                        x_start = f_count
                else:
                    movey.append(f_count)
        is_appeared_t = True
        is_directed_x = not is_directed_x
        f_count = 0
    # 最後にTが出たあとsの中にTが出ないままsの末尾にくるときのため
    if f_count > 0:
        if is_directed_x:
            if is_appeared_t:
                movex.append(f_count)
            else:
                x_start = f_count
        else:
            movey.append(f_count)

    # DPでいくが、リストだとうまくいかないので辞書型を使う
    # dp[i]には、i回目の移動で到達できる座標が辞書型で格納される
    dp_x = [{} for _ in range(len(movex) + 1)]
    dp_y = [{} for _ in range(len(movey) + 1)]
    dp_x[0][x_start] = True
    dp_y[0][0] = True

    # 座標dp_*[i]から到達できる座標の一覧をdp_*[i+1]に格納
    for i, dx in enumerate(movex):
        for j in dp_x[i]:
            dp_x[i + 1][j - dx] = True
            dp_x[i + 1][j + dx] = True
    for i, dy in enumerate(movey):
        for j in dp_y[i]:
            dp_y[i + 1][j - dy] = True
            dp_y[i + 1][j + dy] = True

    # 末尾の要素にゴール座標がなかった場合、ゴールには到達できないということ
    if x not in dp_x[len(movex)]:
        return 'No'
    if y not in dp_y[len(movey)]:
        return 'No'
    return 'Yes'
  
s = input()
x,y = [int(_) for _ in input().split()]
print(d_FT_Robot(s, x, y))

Submission status: Accepted

Solution code:
s=input().split('T')
p=list(map(int,input().split()))
mv=[len(i) for i in s]
d=[[],[]]
for i in range(len(mv)):
    d[i&1].append(mv[i])
x=d[0].copy()
y=d[1].copy()
t=[x.pop(0),0]
x.sort()
y.sort()
while x!=[]:
    if t[0]>p[0]:t[0]-=x.pop()
    else:t[0]+=x.pop()
while y!=[]:
    if t[1] > p[1]:t[1] -= y.pop()
    else:t[1] += y.pop()
print(['No','Yes'][p==t])

Submission status: Accepted

Solution code:
s, (x, y) = raw_input().split('T'), map(int, raw_input().split())
cx, cy = len(s[0]), 0
for _ in sorted([len(_) for _ in s[2::2]])[::-1]:
    cx += _ if cx < x else -_
for _ in sorted([len(_) for _ in s[1::2]])[::-1]:
    cy += _ if cy < y else -_
print 'Yes' if (cx, cy) == (x, y) else 'No'

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
s = input()
x, y = map(int, input().split())

d = [len(s) for s in s.split('T')]
nx = [set([d[0]])]
ny = [set([0])]

for i, l in enumerate(d[2::2]):
    new_n = set()
    for n in nx[i]:
        new_n.add(n + l)
        new_n.add(n - l)
    nx.append(new_n)

for i, l in enumerate(d[1::2]):
    new_n = set()
    for n in ny[i]:
        new_n.add(n + l)
        new_n.add(n - l)
    ny.append(new_n)

print('Yes' if (x in nx[-1] and y in ny[-1]) else 'No')

Submission status: Accepted

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

F = [len(s) for s in S.split('T')]

memo_x = [set((F[0], ))]
memo_y = [set((0, ))]

for i, f in enumerate(F[2::2]):
    next_memo_x = set()
    for prev_x in memo_x[i]:
        next_memo_x.add(prev_x + f)
        next_memo_x.add(prev_x - f)
    memo_x.append(next_memo_x)

for i, f in enumerate(F[1::2]):
    next_memo_y = set()
    for prev_y in memo_y[i]:
        next_memo_y.add(prev_y + f)
        next_memo_y.add(prev_y - f)
    memo_y.append(next_memo_y)

if x in memo_x[-1] and y in memo_y[-1]:
    print('Yes')
else:
    print('No')

Submission status: Accepted

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

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

def main():
    s = input()
    x, y = list(map(int, input().split()))
    x_start = 0
    f_count = 0
    x_dirs = []
    y_dirs = []
    is_x = True # xを向いてるか
    t_exist = False
    for c in s:
        if c == "F":
            f_count += 1
            continue
        # T
        if f_count > 0:
            if is_x:
                if t_exist:
                    x_dirs.append(f_count)
                else:
                    x_start = f_count
            else:
                y_dirs.append(f_count)
        t_exist = True
        is_x = not is_x
        f_count = 0
        continue
    if f_count > 0:
        if is_x:
            if t_exist:
                x_dirs.append(f_count)
            else:
                x_start = f_count
        else:
            y_dirs.append(f_count)

    memo_x = [{} for _ in range(len(x_dirs)+1)]
    memo_y = [{} for _ in range(len(y_dirs)+1)]
    memo_x[0][x_start] = True
    memo_y[0][0] = True

    for i, dx in enumerate(x_dirs):
        for j in memo_x[i]:
            memo_x[i+1][j-dx] = True
            memo_x[i+1][j+dx] = True
    for i, dy in enumerate(y_dirs):
        for j in memo_y[i]:
            memo_y[i+1][j-dy] = True
            memo_y[i+1][j+dy] = True

    # print(x_dirs, y_dirs)
    # print(memo_x)
    # print(memo_y)

    if x not in memo_x[len(x_dirs)]:
        return no()
    if y not in memo_y[len(y_dirs)]:
        return no()
    print("Yes")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
data = raw_input().split("T")
x_a, y_a = map(int,raw_input().split(" "))
x = []
y = []

for a in range(len(data)):
   if a % 2 == 0:
      x.append( data[a].count("F") )
   else:
      y.append( data[a].count("F") )

x_now = x[0]
x = x[1:]

x = sorted(x)[::-1]
y = sorted(y)[::-1]

for xx in range(0,len(x)):
  if x_now > x_a:
    x_now -= x[xx]
  else:
    x_now += x[xx]
  
y_now = 0
for yy in range(0,len(y)):
  if y_now > y_a:
    y_now -= y[yy]
  else:
    y_now += y[yy]

if x_now == x_a and y_now == y_a:
  print "Yes"
else:
  print "No"

Submission status: Accepted

Solution code:
from itertools import product

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

a = [0, 1]

fs = s.split("T")
f_num = [len(f) for f in fs]

dir_lst = [[], []]

for i in range(len(f_num)):
    dir_lst[i%2].append(f_num[i])

x_lst = dir_lst[0].copy()
y_lst = dir_lst[1].copy()

nowx = x_lst.pop(0)
x_lst.sort()
while x_lst != []:
    if nowx > x:
        nowx -= x_lst.pop()
    else:
        nowx += x_lst.pop()

y_lst.sort()
nowy = 0
while y_lst != []:
    if nowy > y:
        nowy -= y_lst.pop()
    else:
        nowy += y_lst.pop()

if x == nowx and y == nowy:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
from collections import Counter
a=input().strip()
x,y=[int(i) for i in input().split(" ")]

e1=[0]
e2=[0]
f=0
t=0
for i in a:
  if i=='F':
    f+=1
  elif i=='T':
    if f!=0:
      if t==0:
        e1.append(f)
      elif t==1:
        e2.append(f)
    t=(t+1)%2
    f=0

if t==0:
  e1.append(f)
elif t==1:
  e2.append(f)

s={0}
o=0
for i in e1:
  if a[0]=='F' and o==1:
    s=set([k+i for k in s])
  else:
    sp=set([k+i for k in s])
    sm=set([k-i for k in s])
    s=sp|sm
  o+=1
r={0}
for i in e2:
  rp=set([k+i for k in r])
  rm=set([k-i for k in r])
  r=rp|rm


if x in s and y in r:
  print('Yes')
else:
  print('No')

Submission status: Accepted

Solution code:
s = list(input(),)
x, y = map(int, input().split())
dir, move, fmove, dx, dy = 0, 0, 0, [], [abs(y)]
s.append('E')
first = True
for i in s:
    if i == 'F':
        move += 1
    else:
        if first:
            fmove = move
            first = False
        else:
            if dir % 2 == 0:
                dx.append(move)
            else:
                dy.append(move)
        move = 0
        dir += 1

def to0(d, ini):
    ad = ini
    for i in sorted(d, reverse=True):
        if ad < 0:
            ad += i
        else:
            ad -= i
    return ad
adx, ady = to0(dx, -x + fmove), to0(dy, 0)
if adx == ady == 0:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
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):
        a = 0
        for v in sorted(vs, reverse=True):
            if t < a:
                a -= v
            else:
                a += v
        return a == t

    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")

Submission status: Accepted

Solution code:
s=input()
x,y=map(int,input().split())
dpx=set()
dpy=set()
xs=[]
ys=[]
tmp=0
d=0
for c in s:
    if c=='F':
        tmp+=1
    elif c=='T':
        if tmp:
            if d==0:
                xs.append(tmp)
            else:
                ys.append(tmp)
        d^=1
        tmp=0
if tmp:
    if d==0:
        xs.append(tmp)
    else:
        ys.append(tmp)
if s[0]=='F':
    dpx.add(xs[0])
    xs=xs[1:]
else:
    dpx.add(0)
dpy.add(0)
for a in xs:
    tmp=set()
    for dx in dpx:
        tmp.add(dx+a)
        tmp.add(dx-a)
    dpx=tmp
for a in ys:
    tmp=set()
    for dy in dpy:
        tmp.add(dy+a)
        tmp.add(dy-a)
    dpy=tmp

#print(dpx,dpy)
if x in dpx and y in dpy:
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
s = list(input())
x, y = map(int, input().split())
dir, move, dx, dy = 0, 0, [], [abs(y)]
s.append('E')
first = True
fmove = 0
for i in s:
    if i == 'F':
        move += 1
    else:
        if first:
            fmove = move
            first = False
        else:
            if dir % 2 == 0:
                dx.append(move)
            else:
                dy.append(move)
        move = 0
        dir += 1
temp = (-x + fmove)
for i in sorted(dx, reverse=True):
    if temp < 0:
        temp += i
    else:
        temp -= i
adx = temp
temp = 0
for i in sorted(dy, reverse=True):
    if temp < 0:
        temp += i
    else:
        temp -= i
ady = temp
if adx == ady == 0:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:

def YesNo(b):
    print("Yes" if b else "No")


def solve(v, l, ini):
    ml = 8000*2+1
    dp = [[False]*ml for i in range(len(l)+1)]
    for i in range(len(ini)):
        dp[0][ini[i]+8000] = True
    for i in range(1, len(l)+1):
        for j in range(ml):
            if (j+l[i-1]) < ml and dp[i-1][j]:
                dp[i][j+l[i-1]] = True
            if (j-l[i-1]) >= 0 and dp[i-1][j]:
                dp[i][j-l[i-1]] = True
    return dp[len(l)][v+8000]


s = input()
x, y = map(int, input().split())
si = 0
while si < len(s) and s[si] == 'F':
    si += 1

s = s[si:]
if len(s) == 0:
    YesNo(x == si and y == 0)
else:
    f = s.split('T')[1:]
    xl, yl = [], []
    for i in range(len(f)):
        if len(f[i]) == 0:
            continue
        if i % 2 == 0:
            yl.append(len(f[i]))
        else:
            xl.append(len(f[i]))

    YesNo(solve(x, xl, [si]) and solve(y, yl[1:], [-yl[0], yl[0]]))

Submission status: Runtime Error

Solution code:


def YesNo(b):
    print("Yes" if b else "No")


def solve(n, v, l, ini):
    ml = n*2+1
    dp = [[False]*ml for i in range(len(l)+1)]
    for i in range(len(ini)):
        dp[0][ini[i]+n] = True
    for i in range(1, len(l)+1):
        for j in range(ml):
            if (j+l[i-1]) < ml and dp[i-1][j]:
                dp[i][j+l[i-1]] = True
            if (j-l[i-1]) >= 0 and dp[i-1][j]:
                dp[i][j-l[i-1]] = True
    return dp[len(l)][v+n]


s = input()
x, y = map(int, input().split())
si = 0
while si < len(s) and s[si] == 'F':
    si += 1

s = s[si:]
if len(s) == 0:
    YesNo(x == si and y == 0)
else:
    f = s.split('T')[1:]
    xl, yl = [], []
    for i in range(len(f)):
        if i % 2 == 0:
            yl.append(len(f[i]))
        else:
            xl.append(len(f[i]))

    if len(yl) == 0:
        yl.append(0)
    YesNo(solve(sum(xl), x, xl, [si]) and solve(sum(yl), y, yl[1:], [-yl[0], yl[0]]))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**6)

s = str(input())
obj_x, obj_y = list(map(int, input().split()))


x = []
y = []
flag = 0
num = 0
num_x = 0
num_y = 0
for i in range(len(s)):
    if s[i] == "T":
        if flag % 2 == 0:
            x.append(num)
            num_x += abs(num)
        else:
            y.append(num)
            num_y += abs(num)
        flag += 1
        num = 0
    else:
        num += 1

if num > 0:
    if flag % 2 == 0:
        x.append(num)
        num_x += abs(num)
    else:
        y.append(num)
        num_y += abs(num)


if x == []:
    x = [0]
if y == []:
    y = [0]

# print(x)
# print(y)

# num_x = sum(x)+2
# num_y = sum(y)+2
# num_x = 8000
# num_y = 8000
num_x += 1
num_y += 1
max_x = num_x*2+1
max_y = num_y*2+1

dp_x = [[0]*((len(x)+1)) for i in range(max_x)]
dp_y = [[0]*((len(y)+1)) for i in range(max_y)]

dp_x[num_x][0] = 1
dp_y[num_y][0] = 1

for i in range(0, len(x)):
    num = x[i]
    for j in range(0, len(dp_x)):

        if j+num < max_x:
            dp_x[j+num][i+1] = max(dp_x[j+num][i+1], dp_x[j][i]*1)
        if j-num > 0:
            dp_x[j-num][i+1] = max(dp_x[j-num][i+1], dp_x[j][i]*1)
        # if dp_x[j][i] == 1:
            # print(j+num, j-num)
for i in range(0, len(y)):
    num = y[i]
    for j in range(0, len(dp_y)):

        if j+num < max_y:
            dp_y[j+num][i+1] = max(dp_y[j+num][i+1], dp_y[j][i]*1)
        if j-num > 0:
            dp_y[j-num][i+1] = max(dp_y[j-num][i+1], dp_y[j][i]*1)
        # if dp_y[j][i] == 1:
            # print(j+num, j-num)


ans_x = dp_x[num_x+obj_x][-1]
ans_y = dp_y[num_y+obj_y][-1]

if ans_x*ans_y == 1:
    print("Yes")
else:
    print("No")