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:
S = input()
X, Y = map(int, input().split())

if S.find("F") < 0:
    if X != 0 or Y != 0:
        print("No")
    else:
        print("Yes")
    exit()

n = len(S)
d = list(map(len, S.split("T")))
xs = d[::2]
ys = d[1::2]

dpx = [set() for _ in range(len(xs))]
for i in range(len(xs)):
    if i == 0:
        d = (-1) ** S.find("F")
        dpx[i].add(d * xs[i] + n)
        continue
    
    for j in dpx[i - 1]:
        dpx[i].add(j + xs[i])
        dpx[i].add(j - xs[i])

dpy = [set() for _ in range(len(ys) + 1)]
dpy[0].add(n)
for i in range(len(ys)):
    for j in dpy[i]:
        dpy[i + 1].add(j + ys[i])
        dpy[i + 1].add(j - ys[i])

ans = (X + n in dpx[-1]) & (Y + n in dpy[-1])
print("Yes" if ans else "No")

Submission status: Accepted

Solution code:
s = input()
x,y = map(int,input().split())
cur_x = 0
cur_y = 0
L_x = []
L_y = []
s += 'K'
temp = 0
dirc = 'yoko'
for i in range(len(s)):
    if s[i] == 'F':
        temp += 1
    elif s[i] == 'T':
        if temp == 0:
            if dirc == 'yoko':
                dirc = 'tate'
            else:
                dirc = 'yoko'
        else:
            if dirc == 'yoko':
                L_x.append(temp)
                temp = 0
                dirc = 'tate'
            else:
                L_y.append(temp)
                temp = 0
                dirc = 'yoko'
    else:
        if temp != 0:
            if dirc == 'yoko':
                L_x.append(temp)
                temp = 0
                dirc = 'tate'
            else:
                L_y.append(temp)
                temp = 0
                dirc = 'yoko'
dp_x = [[False]*8001 for i in range(len(L_x)+1)]
dp_y = [[False]*8001 for i in range(len(L_y)+1)]
dp_x[0][0] = True
dp_y[0][0] = True
if s[0] != 'F':
    for i in range(1,len(L_x)+1):
        for j in range(8001):
            if j+L_x[i-1] <= 8000:
                dp_x[i][j] = dp_x[i-1][abs(j-L_x[i-1])] or dp_x[i-1][j+L_x[i-1]]
            else:
                dp_x[i][j] = dp_x[i-1][abs(j-L_x[i-1])]
    for i in range(1,len(L_y)+1):
        for j in range(8001):
            if j+L_y[i-1] <= 8000:
                dp_y[i][j] = dp_y[i-1][abs(j-L_y[i-1])] or dp_y[i-1][j+L_y[i-1]]
            else:
                dp_y[i][j] = dp_y[i-1][abs(j-L_y[i-1])]
    if dp_x[len(L_x)][abs(x)] == True and dp_y[len(L_y)][abs(y)] == True:
        print('Yes')
    else:
        print('No')
else:
    for i in range(2,len(L_x)+1):
        for j in range(8001):
            if j+L_x[i-1] <= 8000 and abs(j-L_x[i-1]) <= 8000:
                dp_x[i-1][j] = dp_x[i-2][abs(j-L_x[i-1])] or dp_x[i-2][j+L_x[i-1]]
            elif abs(j-L_x[i-1]) <= 8000:
                dp_x[i-1][j] = dp_x[i-2][abs(j-L_x[i-1])]
    for i in range(1,len(L_y)+1):
        for j in range(8001):
            if j+L_y[i-1] <= 8000 and abs(j-L_y[i-1]) <= 8000:
                dp_y[i][j] = dp_y[i-1][abs(j-L_y[i-1])] or dp_y[i-1][j+L_y[i-1]]
            elif abs(j-L_y[i-1]) <= 8000:
                dp_y[i][j] = dp_y[i-1][abs(j-L_y[i-1])]
    if abs(x-L_x[0]) <= 8000:
        if dp_x[len(L_x)-1][abs(x-L_x[0])] == True and dp_y[len(L_y)][abs(y)] == True:
            print('Yes')
        else:
            print('No')
    else:
        print('No')

Submission status: Accepted

Solution code:
import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]
def init_dp1(init, N): return [init for _ in range(N)]
def init_dp2(init, N, M): return [[init for _ in range(M)] for _ in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v
 
    def __str__(self):
        return str(self.v)
 
    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n) 


def main():
    S = input().strip()
    X, Y = read_values()
    F = list(map(len, S.split("T")))

    S = {F[0]}
    if len(F) >= 2:
        for x in F[2::2]:
            SS = set()
            for s in S:
                SS.add(s + x)
                SS.add(s - x)
            S = SS

    if not X in S:
        print("No")
        return
    
    S = {0}
    if len(F) >= 1:
        for x in F[1::2]:
            SS = set()
            for s in S:
                SS.add(s + x)
                SS.add(s - x)
            S = SS
    
    if not Y in S:
        print("No")
        return 
    
    print("Yes")


if __name__ == "__main__":
    main()

Submission status: Accepted

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

direction_x=True
xlist,ylist=[],[]
cnt=0
for s in slist:
  if s=="F":
    cnt+=1
  else:
    if direction_x:
      xlist.append(cnt)
    else:
      ylist.append(cnt)
    cnt=0     
    direction_x=not direction_x
else:
  if direction_x:
    xlist.append(cnt)
  else:
    ylist.append(cnt)

x-=xlist.pop(0)
#print(x,y,xlist,ylist)

# check each x,y
set_x={0}
for i in range(len(xlist)):
  new_set_x=set()
  for xx in set_x:
    new_set_x.add(xx+xlist[i])
    new_set_x.add(xx-xlist[i])
  set_x=new_set_x
    
set_y={0}
for i in range(len(ylist)):
  new_set_y=set()
  for yy in set_y:
    new_set_y.add(yy+ylist[i])
    new_set_y.add(yy-ylist[i])
  set_y=new_set_y

#print(set_x,set_y)
if x in set_x and y in set_y:
  print("Yes")
else:
  print("No")

Submission status: Accepted

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

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

direction_x=True
xlist=[]
ylist=[]
cnt=0
for s in slist:
  if s=="F":
    cnt+=1
  else:
    if direction_x:
      xlist.append(cnt)
    else:
      ylist.append(cnt)
    cnt=0     
    direction_x=not direction_x
else:
  if direction_x:
    xlist.append(cnt)
  else:
    ylist.append(cnt)

x-=xlist[0]
xlist=xlist[1:]
#print(x,y,xlist,ylist)

# check each x,y
dp_x=[]
for _ in range(len(xlist)+1):
  dp_x.append(set())
dp_x[0]={0}
for i in range(1,len(xlist)+1):
  for j in dp_x[i-1]:
    dp_x[i].add(j+xlist[i-1])
    dp_x[i].add(j-xlist[i-1])
#print(dp_x)

dp_y=[]
for _ in range(len(ylist)+1):
  dp_y.append(set())
dp_y[0]={0}
for i in range(1,len(ylist)+1):
  for j in dp_y[i-1]:
    dp_y[i].add(j+ylist[i-1])
    dp_y[i].add(j-ylist[i-1])
#print(dp_y)

#print(dp_x,dp_y)
if x in dp_x[-1] and y in dp_y[-1]:
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
"""Knapsack"""
import sys

sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
s = input().rstrip()
gx, gy = map(int, input().split())
rx, ry = 0, 0
head, inst = 0, []
while head < len(s) and s[head] == "F":
    head += 1
    rx += 1

xlist, ylist = [], []
heading_x = True
while head < len(s):
    tcnt = 0
    while head < len(s) and s[head] == "T":
        tcnt += 1
        head += 1
    if head == len(s):
        break
    fcnt = 0
    while head < len(s) and s[head] == "F":
        fcnt += 1
        head += 1
    if ((tcnt % 2) == 1) ^ heading_x:
        heading_x = True
        xlist.append(fcnt)
    else:
        heading_x = False
        ylist.append(fcnt)


def fill(fs, goal_offset):
    f_total = sum(fs)
    if f_total < goal_offset:
        return False
    if f_total == goal_offset:
        return True
    f_diff = f_total - goal_offset
    if f_diff % 2 == 1:
        return False
    f_diff //= 2
    diffs = {0}
    for f in fs:
        for d in list(diffs):
            d2 = f + d
            if d2 > f_diff:
                continue
            if d2 == f_diff:
                return True
            diffs.add(d2)
    return False


if fill(xlist, abs(gx - rx)) and fill(ylist, abs(gy - ry)):
    print("Yes")
else:
    print("No")

Submission status: Accepted

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

x_mov = []
y_mov = []

s += "T"
flag = 'x'
mov = 0
for c in s:
    if c=='F':
        mov += 1
    elif flag == 'x':
        flag = 'y'
        x_mov.append(mov)
        mov = 0
    else:
        flag = 'x'
        y_mov.append(mov)
        mov = 0

def get_pos(pos,mov_set):
    for i in mov_set:
        new_pos = set()
        for j in pos:
            new_pos.add(j+i)
            new_pos.add(j-i)
        pos = new_pos
    return pos

if s[0] =="T":
    x_pos = get_pos([0],x_mov)
else:
    x_pos = get_pos([x_mov[0]],x_mov[1:])
y_pos = get_pos([0],y_mov)

print("Yes" if x in x_pos and y in y_pos else "No")

Submission status: Accepted

Solution code:
from itertools import groupby


s=input()
x,y=map(int,input().split())
data=[s[i] for i in range(0,len(s))]
data=groupby(data)
LR=[]
UD=[]
point='LR'
flag=0
checkx=2**8000
for key,group in data:
    g=len(list(group))
    if key=='F':
        if flag==0:
            checkx=checkx<<g
            flag=1
        else:
            if point=='LR':
                LR.append(g)
            else:
                UD.append(g)
    else:
        if g%2==1:
            if point=='LR':
                point='UD'
            else:
                point='LR'
        flag=1


for i in range(0,len(LR)):
    checkx=(checkx<<LR[i])|(checkx>>LR[i])

checky=2**8000
for i in range(0,len(UD)):
    checky=(checky<<UD[i])|(checky>>UD[i])


if checkx>>(x+8000) &1==1 and checky>>(y+8000) &1==1:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
# solution

import io
import sys
import math

array = list(map(len, input().split('T')))
nim, mike = map(int, input().split())

tom = [set([array[0]]), set([0])]

for i in range(1, len(array)):
    nxt = set()
    for j in tom[i % 2]:
        nxt.add(j + array[i])
        nxt.add(j - array[i])
    tom[i % 2] = nxt

if nim in tom[0] and mike in tom[1]:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from itertools import accumulate, combinations, permutations, product
# from math import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

s=input()
x,y=map(int,input().split())
tate,yoko=[],[]
kaisi=0
tmp=0
flag=1
for i in range(len(s)):
    if s[i]=="F":
        tmp+=1
    elif s[i]=="T":
        if kaisi==0:
            kaisi=1
            x-=tmp
        elif flag==0:
            flag=1
            yoko.append(tmp)
        else:
            flag=0
            tate.append(tmp)
        tmp=0


if kaisi==0:
    kaisi=1
    x-=tmp
elif flag==0:
    flag=1
    yoko.append(tmp)
else:
    flag=0
    tate.append(tmp)

# if sum(yoko)==0 and sum(tate)==0:
#     if x==0 and y==0:
#         print("Yes")
#     else:
#         print("No")
#     quit()
# # print(yoko,tate)

if abs(x)>sum(yoko) or abs(y)>sum(tate):
    print("No")
    quit()
yokolst=[[0]*(sum(yoko)*2+1) for i in range(len(yoko)+1)]
tatelst=[[0]*(sum(tate)*2+1) for i in range(len(tate)+1)]
yokolst[0][sum(yoko)]=1
tatelst[0][sum(tate)]=1
# print(yokolst)
# print(tatelst)
for i in range(len(tate)):
    for j in range(len(tatelst[0])):
        if tatelst[i][j]==1:
            tatelst[i+1][j+tate[i]]=1
            tatelst[i+1][j-tate[i]]=1


for i in range(len(yoko)):
    for j in range(len(yokolst[0])):
        if yokolst[i][j]==1:
            yokolst[i+1][j+yoko[i]]=1
            yokolst[i+1][j-yoko[i]]=1


# print(yokolst)
# print(tatelst)

if tatelst[-1][sum(tate)+y]==1 and yokolst[-1][sum(yoko)+x]==1:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from itertools import accumulate, combinations, permutations, product
# from math import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

s=input()
x,y=map(int,input().split())
tate,yoko=[],[]
kaisi=0
tmp=0
flag=1
for i in range(len(s)):
    if s[i]=="F":
        tmp+=1
    elif s[i]=="T":
        if kaisi==0:
            kaisi=1
            x-=tmp
        elif flag==0:
            flag=1
            yoko.append(tmp)
        else:
            flag=0
            tate.append(tmp)
        tmp=0


if kaisi==0:
    kaisi=1
    x-=tmp
elif flag==0:
    flag=1
    yoko.append(tmp)
else:
    flag=0
    tate.append(tmp)

if sum(yoko)==0 and sum(tate)==0:
    if x==0 and y==0:
        print("Yes")
    else:
        print("No")
    quit()
# print(yoko,tate)

if abs(x)>sum(yoko) or abs(y)>sum(tate):
    print("No")
    quit()
yokolst=[[0]*(sum(yoko)*2+1) for i in range(len(yoko)+1)]
tatelst=[[0]*(sum(tate)*2+1) for i in range(len(tate)+1)]
yokolst[0][sum(yoko)]=1
tatelst[0][sum(tate)]=1
# print(yokolst)
# print(tatelst)
for i in range(len(tate)):
    for j in range(len(tatelst[0])):
        if tatelst[i][j]==1:
            tatelst[i+1][j+tate[i]]=1
            tatelst[i+1][j-tate[i]]=1


for i in range(len(yoko)):
    for j in range(len(yokolst[0])):
        if yokolst[i][j]==1:
            yokolst[i+1][j+yoko[i]]=1
            yokolst[i+1][j-yoko[i]]=1


# print(yokolst)
# print(tatelst)

if tatelst[-1][sum(tate)+y]==1 and yokolst[-1][sum(yoko)+x]==1:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

horizontal = []
vertical = []

switch = 1
distance = 0

for i in range(len(s)):
    if s[i] == "F":
        distance += 1
    elif switch:
        horizontal.append(distance)
        distance = 0
        switch ^= 1
    else:
        vertical.append(distance)
        distance = 0
        switch ^= 1

if switch:
    horizontal.append(distance)
else:
    vertical.append(distance)

dp_x = [[0] * (2 * sum(horizontal) + 1) for _ in range(len(horizontal) + 1)]
dp_y = [[0] * (2 * sum(vertical) + 1) for _ in range(len(vertical) + 1)]

sx = sum(horizontal)
sy = sum(vertical)

if not (-sx <= x <= sx and -sy <= y <= sy):
    print("No")
    exit()

dp_x[0][0 + sx] = 1
dp_y[0][0 + sy] = 1
dp_x[1][horizontal[0] + sx] = 1

for i in range(2, len(horizontal) + 1):
    x_ = horizontal[i - 1]
    for j in range(-sx, sx + 1):
        if -sx <= j - x_ <= sx: 
            dp_x[i][j + sx] = max(dp_x[i][j + sx], dp_x[i - 1][j - x_ + sx])
        if -sx <= j + x_ <= sx:
            dp_x[i][j + sx] = max(dp_x[i][j + sx], dp_x[i - 1][j + x_ + sx])


for i in range(1, len(vertical) + 1):
    y_ = vertical[i - 1]
    for j in range(-sy, sy + 1):
        if -sy <= j - y_ <= sy: 
            dp_y[i][j + sy] = max(dp_y[i][j + sy], dp_y[i - 1][j - y_ + sy])
        if -sy <= j + y_ <= sy:
            dp_y[i][j + sy] = max(dp_y[i][j + sy], dp_y[i - 1][j + y_ + sy])

if dp_x[-1][x + sx] and dp_y[-1][y + sy]:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
import itertools

s = input()
x,y = map(int,input().split())
d = [len(fs) for fs in s.split('T')]
x -= d[0]
dx = d[2::2]
dy = d[1::2]

def check(d,x):
    set_ = set([0])
    for dx in d:
        set_ = {a+b for a,b in itertools.product(set_,[dx,-dx])}
    return x in set_
if check(dx,x) and check(dy,y):
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
from collections import deque

S = input()
X, Y = map(int, input().split())
F = [deque() for _ in (0, 1)]

i = 0
tmp = 0
for s in S:
    if s == 'F':
        tmp += 1
    else:
        F[i].append(tmp)
        i = (i + 1) % 2
        tmp = 0
if tmp:
    F[i].append(tmp)

first = F[0].popleft()

T = [{first}, {0}]
for i in range(2):
    for j in F[i]:
        T[i] = {k - j for k in T[i]} | {k + j for k in T[i]}
if X in T[0] and Y in T[1]:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    s = input().rstrip()
    x,y = map(int, input().split())
    f = s.split('T')
    dx_list = list(map(len, f[::2]))
    dy_list = list(map(len, f[1::2]))
    
    nx = dx_list.pop(0)
    ny = 0

    dx_list.sort(reverse=True)
    dy_list.sort(reverse=True)

    for dx in dx_list:
        if x >= nx:
            nx += dx
        else:
            nx -= dx
    for dy in dy_list:
        if y >= ny:
            ny += dy
        else:
            ny -= dy

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    s = input().rstrip() + 'T'
    x,y = map(int, input().split())
    # 奇数はy方向, 偶数はx方向
    l = []
    susumu = 0
    for i in range(len(s)):
        if s[i] == 'T':
            l.append(susumu)
            susumu = 0
        else:
            susumu += 1
    b = len(l)
    dp = [[0]*16001 for _ in range(3)]
    dp[0][8000] = 1
    dp[1][8000] = 1
    for i in range(b):
        if i==0:
            dp[2][8000+l[i]] = 1
        elif i%2 == 0:
            for j in range(16000):
                if dp[0][j]:
                    dp[2][j-l[i]] = 1
                    dp[2][j+l[i]] = 1
        elif i%2 == 1:
            for j in range(16000):
                if dp[0][j]:
                    dp[2][j-l[i]] = 1
                    dp[2][j+l[i]] = 1
        dp = dp[1:] + [[0]*16000]
    if b%2 == 0:
        if dp[0][x+8000] == 1 and dp[1][y+8000] == 1:
            print('Yes')
        else:
            print('No')
    else:
        if dp[1][x+8000] == 1 and dp[0][y+8000] == 1:
            print('Yes')
        else:
            print('No')

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
def main():
    order = [len(el) for el in list(map(str, input().rstrip().split("T")))]
    x, y = map(int, input().split())
    order_x = order[2::2]
    order_y = order[1::2]
    L_x, L_y = len(order_x), len(order_y)
    n = 10000
    dp_x = [np.zeros(2*n, dtype=np.bool) for _ in range(L_x + 1)]
    dp_y = [np.zeros(2*n, dtype=np.bool) for _ in range(L_y + 1)]
    dp_x[0][n + order[0]] = True
    dp_y[0][n] = True
    for i, d in enumerate(order_x):
        dp_x[i+1][d:] |= dp_x[i][:2*n-d]
        dp_x[i+1][:2*n-d] |= dp_x[i][d:]
    for i, d in enumerate(order_y):
        dp_y[i+1][d:] |= dp_y[i][:2*n-d]
        dp_y[i+1][:2*n-d] |= dp_y[i][d:]
    if dp_x[L_x][n + x] and dp_y[L_y][n + y]:
        print("Yes")
    else:
        print("No")

if __name__ == "__main__":
    main()

Submission status: Accepted

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

    dx = []
    dy = []
    dir = 0
    cnt = 0
    for i in range(n):
        if s[i] == 'F':
            cnt += 1
        else:
            if dir%2 == 0:
                dx.append(cnt)
                cnt = 0
                dir += 1
            else:
                dy.append(cnt)
                cnt = 0
                dir += 1
    else:
        if dir%2 == 0:
            dx.append(cnt)
        else:
            dy.append(cnt)

    dpx = [0]*(len(dx)+1)
    dpy = [0]*(len(dy)+1)

    import numpy as np
    D = 2*n+1
    dpx[0] = np.zeros(D, np.bool)
    dpy[0] = np.zeros(D, np.bool)
    dpx[0][n] = 1
    dpy[0][n] = 1

    for i in range(len(dx)):
        d = dx[i]
        if i == 0:
            b = np.zeros(D, np.bool)
            b[d:] |= dpx[i][:D-d]
        else:
            b = np.zeros(D, np.bool)
            b[d:] |= dpx[i][:D-d]
            b[:D-d] |= dpx[i][d:]
        dpx[i+1] = b

    for i in range(len(dy)):
        d = dy[i]
        b = np.zeros(D, np.bool)
        b[d:] |= dpy[i][:D-d]
        b[:D-d] |= dpy[i][d:]
        dpy[i+1] = b

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
try:
    from typing import List
except ImportError:
    pass

YES = "Yes"  # type: str
NO = "No"  # type: str


def solve(s: str, x: int, y: int):
    try:
        k = s.index("T")
    except ValueError:
        k = len(s)
    x -= k
    s = s[k:]
    xs = []
    ys = []
    d = 0
    v = 0
    for c in s:
        if c == "T":
            if v != 0:
                if d == 0:
                    xs.append(v)
                else:
                    ys.append(v)
            d = 1 - d
            v = 0
        else:
            v += 1
    if v != 0:
        if d == 0:
            xs.append(v)
        else:
            ys.append(v)
    mx = sum(xs)
    dpx = [0] * (mx + 1)
    dpx[0] = 1
    for xi in xs:
        for j in reversed(range(xi, len(dpx))):
            dpx[j] |= dpx[j - xi]
    my = sum(ys)
    dpy = [0] * (my + 1)
    dpy[0] = 1
    for yi in ys:
        for j in reversed(range(yi, len(dpy))):
            dpy[j] |= dpy[j - yi]
    if (
        -mx <= x <= mx and (mx - x) % 2 == 0 and dpx[(mx - x) // 2] and
        -my <= y <= my and (my - y) % 2 == 0 and dpy[(my - y) // 2]
    ):
        print(YES)
    else:
        print(NO)



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    s = next(tokens)  # type: str
    x = int(next(tokens))  # type: int
    y = int(next(tokens))  # type: int
    solve(s, x, y)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""


def main():

    ss = list(input())[:-1]

    ss = s(ss)

    n = len(ss)

    x,y = m()

    x += 8000

    y += 8000

    dpx = set([8000])

    dpy = set([8000])

    ro = 0

    for i in range(1,n+1):
        pop = ss[i-1]
        if pop[0] == "T":
            if pop[1] % 2 == 1:
                ro ^= 1
        else:
            if i == 1:
                dpx = set([8000+pop[1]])
            else:
                if ro == 0:
                    ch = set()
                    for num in dpx:
                        if num + pop[1] <= 16000:
                            ch.add(num + pop[1])    
                        if num - pop[1] >= 0:
                            ch.add(num - pop[1])
                        dpx = ch
                else:
                    ch = set()
                    for num in dpy:
                        if num + pop[1] <= 16000:
                            ch.add(num + pop[1])
                        if num - pop[1] >= 0:
                            ch.add(num - pop[1])
                        dpy = ch

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

            


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
str = input()
x,y = map(int,input().split())
dx,dy = 0,0

fn = [len(n) for n in str.split("T")]
dir = [[],[]]

for i in range(len(fn)):
  dir[i%2].append(fn[i])

xdir = dir[0]
ydir = dir[1]

dx = xdir.pop(0)
xdir.sort()
while xdir != []:
  if dx < x:
    dx += xdir.pop()
  else:
    dx -= xdir.pop()
    
ydir.sort()
while ydir != []:
  if dy < y:
    dy += ydir.pop()
  else:
    dy -= ydir.pop()

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
s=list(input())
X,Y=map(int,input().split())

if "T" in s:
    pass
else:
    if X==len(s) and Y==0:
        print("Yes")
    else:
        print("No")
    exit()


way=[[],[]]

switch=0
judge=True

lx=[]
ly=[0]
temp=0
for item in s:
    if item == "T":
        if judge:
            judge=False
            lx.append(temp)
        else:
            way[switch].append(temp)
        temp=0
        switch^=1
    else:
        temp+=1
way[switch].append(temp)

#print(lx)

for x in way[0]:
    temp_x=[]
    for item in lx:
        temp_x.append(item+x)
        temp_x.append(item-x)
    lx=temp_x[:]
    lx=list(set(lx))
#print(lx)

for y in way[1]:
    temp_y=[]
    for item in ly:
        temp_y.append(item+y)
        temp_y.append(item-y)
    ly=temp_y[:]
    ly=list(set(ly))
#print(ly)

if (X in lx) and (Y in ly):
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
#082_D
s = input()
x, y = map(int, input().split())
xgo, ygo = [], []

cnt = 0
flg = True
check = True if s[0] == 'F' else False
for i in range(len(s)):
    if s[i] == 'T':
        if flg and cnt > 0:
            xgo.append(cnt)
        elif cnt > 0:
            ygo.append(cnt)
        cnt = 0
        flg = (flg + 1) % 2
    else:
        cnt += 1

if flg and cnt > 0:
    xgo.append(cnt)
elif cnt > 0:
    ygo.append(cnt)

flg1, flg2 = len(xgo)==0, len(ygo)==0

sum_x, sum_y = sum(xgo), sum(ygo)
dp1 = [[False for _ in range(2*sum_x+1)] for _ in range(len(xgo)+1)]
dp2 = [[False for _ in range(2*sum_y+1)] for _ in range(len(ygo)+1)]

if check:
    dp1[1][sum_x+xgo[0]] = True
    for i in range(2, len(xgo)+1):
        for j in range(-sum_x, sum_x+1):
            if 0 <= j+sum_x-xgo[i-1] <= 2*sum_x:
                if dp1[i-1][j+sum_x-xgo[i-1]]:
                    dp1[i][j+sum_x] = True
            if 0 <= j+sum_x+xgo[i-1] <= 2*sum_x:
                if dp1[i-1][j+sum_x+xgo[i-1]]:
                    dp1[i][j+sum_x] = True
else:
    dp1[0][sum_x] = True
    for i in range(1, len(xgo)+1):
        for j in range(-sum_x, sum_x+1):
            if 0 <= j+sum_x-xgo[i-1] <= 2*sum_x:
                if dp1[i-1][j+sum_x-xgo[i-1]]:
                    dp1[i][j+sum_x] = True
            if 0 <= j+sum_x+xgo[i-1] <= 2*sum_x:
                if dp1[i-1][j+sum_x+xgo[i-1]]:
                    dp1[i][j+sum_x] = True

dp2[0][sum_y] = True
for i in range(1, len(ygo)+1):
    for j in range(-sum_y, sum_y+1):
        if 0 <= j+sum_y-ygo[i-1] <= 2*sum_y:
            if dp2[i-1][j+sum_y-ygo[i-1]]:
                dp2[i][j+sum_y] = True
        if 0 <= j+sum_y+ygo[i-1] <= 2*sum_y:
            if dp2[i-1][j+sum_y+ygo[i-1]]:
                dp2[i][j+sum_y] = True
                
if flg1:
    flgx = (x == 0)
elif abs(x) > sum_x:
    flgx = False
else:
    flgx = dp1[len(xgo)][sum_x+x]
    
if flg2:
    flgy = (y == 0)
elif abs(y) >sum_y:
    flgy = False
else:
    flgy = dp2[len(ygo)][sum_y+y]
    
print('Yes' if flgx and flgy else 'No')

Submission status: Accepted

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

Submission status: Accepted

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

X = []
Y = []
for i, s in enumerate(S):
    if i % 2 == 0:
        X.append(len(s))
    else:
        Y.append(len(s))

if sum(X) < abs(x) or sum(Y) < abs(y):
    print('No')
    exit()

pos = set([X[0]])
for i in range(1, len(X)):
    d = X[i]
    npos = set()
    for p in pos:
        npos.add(p + d)
        npos.add(p - d)
    pos = npos
flag_x = x in pos

pos = set([0])
for i in range(len(Y)):
    d = Y[i]
    npos = set()
    for p in pos:
        npos.add(p + d)
        npos.add(p - d)
    pos = npos
flag_y = y in pos

if flag_x and flag_y:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
def main():
    from sys import exit
    s=input()
    x,y=map(int,input().split())

    def groupby(a):
        a2=[[a[0],1]]
        for i in range(1,len(a)):
            if a2[-1][0]==a[i]:
                a2[-1][1]+=1
            else:
                a2.append([a[i],1])
        return a2

    s=groupby(s)
    a,b=[],[]
    turn=True
    #初めまっすぐの場合
    if s[0][0]=="F":
        x-=s[0][1]
        s.pop(0)
    for i in s:
        if i[0]=="F":
            if turn:
                a.append(-i[1])
            else:
                b.append(-i[1])
        elif i[1]%2==1:
            turn=not turn
    a.sort()
    b.sort()
    for i in a:
        x=abs(x)-abs(i)
    if x!=0:
        print("No")
        exit()
    for i in b:
        y=abs(y)-abs(i)
    if y!=0:
        print("No")
        exit()
    print("Yes")
main()