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: Runtime Error

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:
                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]]
            else:
                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:
                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)-1][abs(x-L_x[0])] == True and dp_y[len(L_y)][abs(y)] == True:
        print('Yes')
    else:
        print('No')

Submission status: Runtime Error

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
    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.append(d2)
    return False


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

Submission status: Runtime Error

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
for key,group in data:
    g=len(list(group))
    if key=='F':
        if flag==0:
            x-=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

checkx=2**8000
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: Runtime Error

Solution code:
import sys

sys.setrecusionlimit(1000000)

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

memo={}
def dfs(startx,starty,direction,num):
    if (startx,starty,direction,num) in memo.keys():
        return memo[(startx,starty,direction,num)]
    if num==N:
        memo[(startx,starty,direction,num)]=0
        memo[(startx,starty,direction,num)]=(startx==x and starty==y)
        return (startx==x and starty==y)

    if s[num]=='F':
        if direction==0:
            memo[(startx,starty,direction,num)]=0
            memo[(startx,starty,direction,num)]=dfs(startx+1,starty,direction,num+1)
            return memo[(startx,starty,direction,num)]
        if direction==90:
            memo[(startx,starty,direction,num)]=0
            memo[(startx,starty,direction,num)]=dfs(startx,starty+1,direction,num+1)
            return memo[(startx,starty,direction,num)]
        if direction==180:
            memo[(startx,starty,direction,num)]=0
            memo[(startx,starty,direction,num)]=dfs(startx-1,starty,direction,num+1)
            return memo[(startx,starty,direction,num)]
        if direction==270:
            memo[(startx,starty,direction,num)]=0
            memo[(startx,starty,direction,num)]=dfs(startx,starty-1,direction,num+1)
            return memo[(startx,starty,direction,num)]

    if s[num]=='T':
        memo[(startx,starty,direction,num)]=0
        if dfs(startx,starty,(direction+90)%360,num+1):
            memo[(startx,starty,direction,num)]=True
            return True
        if dfs(startx,starty,(direction-90)%360,num+1):
            memo[(startx,starty,direction,num)]=True
            return True

        memo[(startx,starty,direction,num)]=False
        return False

if dfs(0,0,0,0):
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

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

memo={}
def dfs(startx,starty,direction,num):
    if (startx,starty,direction,num) in memo.keys():
        return memo[(startx,starty,direction,num)]
    if num==N:
        memo[(startx,starty,direction,num)]=0
        memo[(startx,starty,direction,num)]=(startx==x and starty==y)
        return (startx==x and starty==y)

    if s[num]=='F':
        if direction==0:
            memo[(startx,starty,direction,num)]=0
            memo[(startx,starty,direction,num)]=dfs(startx+1,starty,direction,num+1)
            return memo[(startx,starty,direction,num)]
        if direction==90:
            memo[(startx,starty,direction,num)]=0
            memo[(startx,starty,direction,num)]=dfs(startx,starty+1,direction,num+1)
            return memo[(startx,starty,direction,num)]
        if direction==180:
            memo[(startx,starty,direction,num)]=0
            memo[(startx,starty,direction,num)]=dfs(startx-1,starty,direction,num+1)
            return memo[(startx,starty,direction,num)]
        if direction==270:
            memo[(startx,starty,direction,num)]=0
            memo[(startx,starty,direction,num)]=dfs(startx,starty-1,direction,num+1)
            return memo[(startx,starty,direction,num)]

    if s[num]=='T':
        memo[(startx,starty,direction,num)]=0
        if dfs(startx,starty,(direction+90)%360,num+1):
            memo[(startx,starty,direction,num)]=True
            return True
        if dfs(startx,starty,(direction-90)%360,num+1):
            memo[(startx,starty,direction,num)]=True
            return True

        memo[(startx,starty,direction,num)]=False
        return False

if dfs(0,0,0,0):
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
s = input()
x, y = map(int,input().split())
flag = 1
X = []
Y = []
cnt = 0
for i in s:
    if i == 'T':
        if flag == 1 and cnt:
            X.append(cnt)
            cnt = 0
        elif flag == -1 and cnt:
            Y.append(cnt)
            cnt = 0
        flag *= -1
    else:
        cnt += 1
if flag == 1 and cnt:
    X.append(cnt)
elif flag == -1 and cnt:
    Y.append(cnt)

dp_x = [[0 for i in range(160001)] for j in range(len(X))]
dp_y = [[0 for i in range(16000)] for j in range(len(Y))]

for i in range(len(X)):
    if i == 0:
        if s[0] == 'T':
            dp_x[0][X[0] + 8000] = 1
            dp_x[0][- X[0] + 8000] = 1
        else:
            dp_x[0][X[0] + 8000] = 1
    else:
        for j in range(16001):
            if dp_x[i-1][j] and 0 <= j + X[i] <= 16000:
                dp_x[i][j + X[i]] = 1
            if dp_x[i-1][j] and 0 <= j - X[i] <= 16000:
                dp_x[i][j - X[i]] = 1

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

if len(X) == 0 and len(Y) == 0:
    if x == 0 and y == 0:
        print('Yes')
    else:
        print('No')
elif len(X) == 0:
    if x == 0 and dp_y[-1][y+8000]:
        print('Yes')
    else:
        print('No')
elif len(Y) == 0:
    if y == 0 and dp_x[-1][x+8000]:
        print('Yes')
    else:
        print('No')
else:
    if dp_y[-1][y+8000] and dp_x[-1][x+8000]:
        print('Yes')
    else:
        print(dp_y[-1][y+8000],dp_x[-1][x+8000])
        print('No')

Submission status: Runtime Error

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)

# print(yoko,tate)

if x>sum(yoko) or 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: Runtime Error

Solution code:
from collections import deque
def check():
    S = input()
    x, y = map(int, input().split())
    fs = list(map(len,S.split('T')))
    if len(fs)<3:
        if x != fs[0]:
            return False
        if len(fs)==1:
            if y == 0:
                return True
            else:
                return False
        if y != fs[1] and y != -fs[1]:
            return False
        return True
    xs = fs[2::2]
    ys = fs[1::2]
    xs.sort(reverse = True)
    N = len(xs)
    M = len(ys)
    cum = [0]*(N+1)
    for i in range(N-1,-1,-1):
        cum[i] = cum[i+1]+xs[i]
    pos = deque([fs[0]-x])
    for i in range(N):
        l = len(pos)
        if l == 0:
            return False
        for _ in range(l):
            d = pos.popleft()
            if abs(d+xs[i])<=cum[i+1] and d+xs[i] not in pos:
                pos.append(d+xs[i])
            if abs(d-xs[i])<=cum[i+1] and d-xs[i] not in pos:
                pos.append(d-xs[i])
    if 0 not in pos:
        return False
    ys.sort(reverse = True)
    cum = [ys[-1]]*(M+1)
    for i in range(M-1,-1,-1):
        cum[i] = cum[i+1]+ys[i]
    pos = deque([0])
    for i in range(M):
        l = len(pos)
        if l == 0:
            return False
        for _ in range(l):
            d = pos.popleft()
            if abs(d+ys[i]-y)<=cum[i+1] and d+xs[i] not in pos:
                pos.append(d+ys[i])
            if abs(d-ys[i]-y)<=cum[i+1] and d-xs[i] not in pos:
                pos.append(d-ys[i])
    if y not in pos:
        return False
    return True
if check():
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
from collections import deque
def check():
    S = input()
    x, y = map(int, input().split())
    fs = list(map(len,S.split('T')))
    xs = fs[2::2]
    ys = fs[1::2]
    xs.sort(reverse = True)
    N = len(xs)
    M = len(ys)
    cum = [0]*(N+1)
    for i in range(N-1,-1,-1):
        cum[i] = cum[i+1]+xs[i]
    pos = deque([fs[0]-x])
    for i in range(N):
        l = len(pos)
        if l == 0:
            return False
        for _ in range(l):
            d = pos.popleft()
            if abs(d+xs[i])<=cum[i+1] and d+xs[i] not in pos:
                pos.append(d+xs[i])
            if abs(d-xs[i])<=cum[i+1] and d-xs[i] not in pos:
                pos.append(d-xs[i])
    if 0 not in pos:
        return False
    if len(ys)==0:
        if y == 0:
            return True
        else:
            return False
    ys.sort(reverse = True)
    cum = [ys[-1]]*(M+1)
    for i in range(M-1,-1,-1):
        cum[i] = cum[i+1]+ys[i]
    pos = deque([0])
    for i in range(M):
        l = len(pos)
        if l == 0:
            return False
        for _ in range(l):
            d = pos.popleft()
            if abs(d+ys[i]-y)<=cum[i+1] and d+xs[i] not in pos:
                pos.append(d+ys[i])
            if abs(d-ys[i]-y)<=cum[i+1] and d-xs[i] not in pos:
                pos.append(d-ys[i])
    if y not in pos:
        return False
    return True
if check():
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
from collections import deque
def check():
    S = input()
    x, y = map(int, input().split())
    fs = list(map(len,S.split('T')))
    xs = fs[::2]
    ys = fs[1::2]
    xs.sort(reverse = True)
    N = len(xs)
    M = len(ys)
    cum = [0]*(N+1)
    for i in range(N-1,-1,-1):
        cum[i] = cum[i+1]+xs[i]
    pos = deque([-x])
    for i in range(N):
        l = len(pos)
        if l == 0:
            return False
        for _ in range(l):
            d = pos.popleft()
            if abs(d+xs[i])<=cum[i+1]:
                pos.append(d+xs[i])
            if abs(d-xs[i])<=cum[i+1]:
                pos.append(d-xs[i])
    if 0 not in pos:
        return False
    ys.sort(reverse = True)
    cum = [ys[-1]]*(M+1)
    for i in range(M-1,-1,-1):
        cum[i] = cum[i+1]+ys[i]
    pos = deque([0])
    for i in range(M):
        l = len(pos)
        if l == 0:
            return False
        for _ in range(l):
            d = pos.popleft()
            if abs(d+ys[i]-y)<=cum[i+1]:
                pos.append(d+ys[i])
            if abs(d-ys[i]-y)<=cum[i+1]:
                pos.append(d-ys[i])
    if y not in pos:
        return False
    return True
if check():
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
from collections import deque
def check():
    S = input()
    x, y = map(int, input().split())
    fs = list(map(len,S.split('T')))
    xs = fs[::2]
    ys = fs[1::2]
    xs.sort(reverse = True)
    N = len(xs)
    M = len(ys)
    cum = [0]*(N+1)
    for i in range(N-1,-1,-1):
        cum[i] = cum[i+1]+xs[i]
    pos = deque([-x])
    for i in range(N):
        l = len(pos)
        if l == 0:
            return False
        for _ in range(l):
            d = pos.popleft()
            if abs(d+xs[i])<=cum[i+1]:
                pos.append(d+xs[i])
            if abs(d-xs[i])<=cum[i+1]:
                pos.append(d-xs[i])
    if 0 not in pos:
        return False
    ys.sort(reverse = True)
    cum = [ys[-1]]*(M+1)
    for i in range(M-1,-1,-1):
        cum[i] = cum[i+1]+ys[i]
    pos = deque([0])
    for i in range(M):
        l = len(pos)
        if l == 0:
            return False
        for _ in range(l):
            d = pos.popleft()
            if abs(d+ys[i])<=cum[i+1]+abs(y):
                pos.append(d+ys[i])
            if abs(d-ys[i])<=cum[i+1]+abs(y):
                pos.append(d-ys[i])
    if y not in pos:
        return False
    return True
if check():
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
from collections import deque
def check():
    S = input()
    x, y = map(int, input().split())
    fs = list(map(len,S.split('T')))
    xs = fs[::2]
    ys = fs[1::2]
    xs.sort(reverse = True)
    N = len(xs)
    M = len(ys)
    cum = [xs[-1]]*N
    for i in range(N-2,-1,-1):
        cum[i] = cum[i+1]+xs[i]
    pos = deque([-x])
    for i in range(N):
        l = len(pos)
        if l == 0:
            return False
        for _ in range(l):
            d = pos.popleft()
            if abs(d)<=cum[i]:
                pos.append(d+xs[i])
                pos.append(d-xs[i])
    if 0 not in pos:
        return False
    ys.sort(reverse = True)
    cum = [ys[-1]]*M
    for i in range(M-2,-1,-1):
        cum[i] = cum[i+1]+ys[i]
    pos = deque([0])
    for i in range(M):
        l = len(pos)
        if l == 0:
            return False
        for _ in range(l):
            d = pos.popleft()
            if abs(d)<=cum[i]:
                pos.append(d+ys[i])
                pos.append(d-ys[i])
    if y not in pos:
        return False
    return True
if check():
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
import sys

s = input()
x, y = map(int, input().split())
fn = [ int(len(f)) for f in s.split('T')]

h = []
v = []
for i in range(len(fn)):
    if i % 2 == 0:
        h.append(fn[i])
    else:
        v.append(fn[i])

if sum(h) < abs(x) or sum(v) < abs(y):
    print("No")
    sys.exit(0)


# 水平
dph = [False]*(2*sum(h)+1)
# 垂直
dpv = [False]*(2*sum(v)+1)

dph[sum(h)] = True
dpv[sum(v)] = True

for i in range(0, len(h)):
    dp_tmp = [False]*(2*sum(h)+1)
    for j in range(2*sum(h)+1):
        if j + h[i] <= 2*sum(h) and j - h[i] >= 0:
            dp_tmp[j] = dp_tmp[j] or dph[j+h[i]] or dph[j-h[i]]
        elif j + h[i] <= 2*sum(h):
            dp_tmp[j] = dp_tmp[j] or dph[j+h[i]]
        elif j - h[i] >= 0:
            dp_tmp[j] = dp_tmp[j] or dph[j-h[i]]
    for j in range(2*sum(h)+1):
        dph[j] = dp_tmp[j]

for i in range(0, len(v)):
    dp_tmp = [False]*(2*sum(v)+1)
    for j in range(2*sum(v)+1):
        if j + v[i] <= 2*sum(v) and j - v[i] >= 0:
            dp_tmp[j] = dp_tmp[j] or dph[j+v[i]] or dph[j-v[i]]
        elif j + v[i] <= 2*sum(v):
            dp_tmp[j] = dp_tmp[j] or dph[j+v[i]]
        elif j - v[i] >= 0:
            dp_tmp[j] = dp_tmp[j] or dph[j-v[i]]
    for j in range(2*sum(v)+1):
        dpv[j] = dp_tmp[j]

if dph[sum(h)+x] and dpv[sum(v)+y]:
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

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 x_error == 0:
    dp_x[-1][x + sx] = 1
if y_error == 0:
    dp_y[-1][y + sy] = 1

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

Submission status: Runtime Error

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()
    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.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 x >= nx:
            nx += dy
        else:
            nx -= dy

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

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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
    a = sum(l)
    b = len(l)
    dp = [[0]*16000 for _ in range(b+2)]
    dp[0][a] = 1
    dp[1][a] = 1
    for i in range(b):
        if i==0:
            dp[i+2][a+l[i]] = 1
            continue
        if i%2 == 0:
            for j in range(16000):
                if dp[i][j]:
                    dp[i+2][j-l[i]] = 1
                    dp[i+2][j+l[i]] = 1
        if i%2 == 1:
            for j in range(16000):
                if dp[i][j]:
                    dp[i+2][j-l[i]] = 1
                    dp[i+2][j+l[i]] = 1
    if b%2 == 0:
        if dp[-2][x+a] == 1 and dp[-1][y+a] == 1:
            print('Yes')
        else:
            print('No')
    else:
        if dp[-1][x+a] == 1 and dp[-2][y+a] == 1:
            print('Yes')
        else:
            print('No')

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# coding: utf-8

# https://atcoder.jp/contests/abc082/tasks/arc087_b
# 16:28-


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

    n_f = len([1 for c in s if c == "F"])

    directions = {
        "r": ["u", "d"], "l": ["u", "d"], "u": ["l", "r"], "d": ["l", "r"]
    }

    def rec(now_x, now_y, dire, f_rest, s_idx, s, len_s, to_x, to_y):
        dist = abs(to_x - now_x) + abs(to_y - now_y)

        if s_idx == len_s and dist == 0:
            return True

        if dist > f_rest:
            return False  # ?

        # まだ可能性がある
        # print(s_idx, dist, f_rest)
        if s[s_idx] == "F":
            if dire == "u":
                return rec(now_x, now_y+1, dire, f_rest-1, s_idx+1, s, len_s, to_x, to_y)
            elif dire == "d":
                return rec(now_x, now_y-1, dire, f_rest-1, s_idx+1, s, len_s, to_x, to_y)
            elif dire == "r":
                return rec(now_x+1, now_y, dire, f_rest-1, s_idx+1, s, len_s, to_x, to_y)
            else:
                return rec(now_x-1, now_y, dire, f_rest-1, s_idx+1, s, len_s, to_x, to_y)  
        else:
            edges = []
            next_dires = directions[dire]
            for next_dire in next_dires:
                edges.append(rec(now_x, now_y, next_dire, f_rest, s_idx+1, s, len_s, to_x, to_y))
            return edges[0] or edges[1]

    ans = rec(0, 0, "r", n_f, 0, s, len(s), x, y)

    return "Yes" if ans else "No"


print(main())

Submission status: Runtime Error

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][0] = 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+0] = 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)
else:
    flgx = dp1[len(xgo)][sum_x+x]
    
if flg2:
    flgy = (y == 0)
else:
    flgy = dp2[len(ygo)][sum_y+y]
print('Yes' if flgx and flgy else 'No')

Submission status: Runtime Error

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

flg = True
cnt = 0
for i in range(len(s)):
    if s[i] == 'T':
        if flg and cnt > 0:
            xgo.append(cnt)
        elif cnt > 0:
            ygo.append(cnt)
        flg = (flg + 1) % 2
        cnt = 0
    else:
        cnt += 1
        
if flg and cnt > 0:
    xgo.append(cnt)
elif cnt > 0:
    ygo.append(cnt)
    
def search(L, x, flg):
    s = sum(L)
    dp = [[False for _ in range(s*2+1)] for _ in range(len(L)+1)]
    dp[0][s + 1] = True
    
    for i in range(1, len(L)+1):
        for j in range(-s, s+1):
            k = j + s
            if dp[i-1][k-L[i-1]]:
                dp[i][k] = True
            if dp[i-1][k+L[i-1]]:
                if flg and i == 1:
                    continue
                else:
                    dp[i][k] = True

    return dp[len(L)][s+x]

print('Yes' if search(xgo, x, True) and search(ygo, y, False) else 'No')

Submission status: Runtime Error

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

flg = True
cnt = 0
for i in range(len(s)):
    if s[i] == 'T':
        if flg and cnt > 0:
            xgo.append(cnt)
        elif cnt > 0:
            ygo.append(cnt)
        flg = (flg + 1) % 2
        cnt = 0
    else:
        cnt += 1
        
if flg and cnt > 0:
    xgo.append(cnt)
elif cnt > 0:
    ygo.append(cnt)
    
def search(L, x, flg):
    s = sum(L)
    dp = [[False for _ in range(s*2+1)] for _ in range(len(L)+1)]
    dp[0][s + 1] = True
    
    for i in range(1, len(L)+1):
        for j in range(-s, s+1):
            k = j + s
            if dp[i-1][k-L[i-1]]:
                dp[i][k] = True
            if flg and dp[i-1][k+L[i-1]]:
                dp[i][k] = True

    return dp[len(L)][s+x]

print('Yes' if search(xgo, x) and search(ygo, y) else 'No')

Submission status: Runtime Error

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

flg = True
cnt = 0
for i in range(len(s)):
    if s[i] == 'T':
        if flg and cnt > 0:
            xgo.append(cnt)
        elif cnt > 0:
            ygo.append(cnt)
        flg = (flg + 1) % 2
        cnt = 0
    else:
        cnt += 1
        
if flg and cnt > 0:
    xgo.append(cnt)
elif cnt > 0:
    ygo.append(cnt)
    
def search(L, x):
    dp = [[False for _ in range(20000)] for _ in range(len(L)+1)]
    dp[0][10000] = True
    
    for i in range(1, len(L)+1):
        for j in range(-sum(L), sum(L)+1):
            k = j + 10000
            if dp[i-1][k-L[i-1]]:
                dp[i][k] = True
            if dp[i-1][k+L[i-1]]:
                dp[i][k] = True

    return dp[len(L)][10000+x]

print('Yes' if search(xgo, x) and search(ygo, y) else 'No')