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: Wrong Answer

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 len(yoko)==0 and len(tate)==0:
    if x==0 and y==0:
        print("Yes")
    else:
        print("No")
    quit()
# 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: Wrong Answer

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 tmp!=0:
    if flag==0:
        yoko.append(tmp)
    else:
        tate.append(tmp)

if x>sum(yoko) or y>sum(tate):
    print("No")
    quit()
# print(yoko,tate)
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: Wrong Answer

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=0
for i in range(len(s)):
    if s[i]=="F":
        tmp+=1
    elif s[i]=="T":
        if kaisi==0:
            kaisi=1
            x-=tmp
            flag=1
        elif flag==0:
            flag=1
            yoko.append(tmp)
        else:
            flag=0
            tate.append(tmp)
        tmp=0
if tmp!=0:
    if flag==0:
        flag=1
        yoko.append(tmp)
    else:
        flag=0
        tate.append(tmp)
if x>sum(yoko) or y>sum(tate):
    print("No")
    quit()
# print(yoko,tate)
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:
            print(i,j)
            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: Wrong Answer

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([xs[0]-x])
    for i in range(1,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: Wrong Answer

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] 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: Wrong Answer

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
    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]:
                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: Wrong Answer

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))]
dp_y = [[False]*8001 for i in range(len(L_y))]
if len(L_x) > 0:
    dp_x[0][L_x[0]] = True
if len(L_y) > 0:
    dp_y[0][L_y[0]] = True
for i in range(1,len(L_x)):
    t = L_x[i]
    for j in range(8001):
        if j-t >= 0 and j+t <= 8000:
            dp_x[i][j] = dp_x[i-1][j-t] or dp_x[i-1][j+t]
        elif j-t < 0 and j+t <= 8000:
            dp_x[i][j] = dp_x[i-1][abs(j-t)] or dp_x[i-1][j+t]
        elif j-t >= 0 and j+t > 8000:
            dp_x[i][j] = dp_x[i-1][j-t]
        else:
            dp_x[i][j] = dp_x[i-1][abs(j-t)]
for i in range(1,len(L_y)):
    t = L_y[i]
    for j in range(8001):
        if j-t >= 0 and j+t <= 8000:
            dp_y[i][j] = dp_y[i-1][j-t] or dp_y[i-1][j+t]
        elif j-t < 0 and j+t <= 8000:
            dp_y[i][j] = dp_y[i-1][abs(j-t)] or dp_y[i-1][j+t]
        elif j-t >= 0 and j+t > 8000:
            dp_y[i][j] = dp_y[i-1][j-t]
        else:
            dp_y[i][j] = dp_y[i-1][abs(j-t)]
x = abs(x)
y = abs(y)
if len(L_x) == 0 and len(L_y) == 0:
    if x == y == 0:
        print('Yes')
    else:
        print('No')
elif len(L_x) != 0 and len(L_y) == 0:
    if y == 0 and dp_x[len(L_x)-1][x] == True:
        print('Yes')
    else:
        print('No')
elif len(L_x) == 0 and len(L_y) != 0:
    if x == 0 and dp_y[len(L_y)-1][y] == True:
        print('Yes')
    else:
        print('No')
else:
    if dp_x[len(L_x)-1][x] == True and dp_y[len(L_y)-1][y] == True:
        print('Yes')
    else:
        print('No')

Submission status: Wrong Answer

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))]
dp_y = [[False]*8001 for i in range(len(L_y))]
if len(L_x) > 0:
    dp_x[0][L_x[0]] = True
if len(L_y) > 0:
    dp_y[0][L_y[0]] = True
for i in range(1,len(L_x)):
    t = L_x[i]
    for j in range(8001):
        if j-t >= 0 and j+t <= 8000:
            dp_x[i][j] = dp_x[i-1][j-t] or dp_x[i-1][j+t]
        elif j-t < 0 and j+t <= 8000:
            dp_x[i][j] = dp_x[i-1][abs(j-t)] or dp_x[i-1][j+t]
        elif j-t >= 0 and j+t > 8000:
            dp_x[i][j] = dp_x[i-1][j-t]
        else:
            None
for i in range(1,len(L_y)):
    t = L_y[i]
    for j in range(8001):
        if j-t >= 0 and j+t <= 8000:
            dp_y[i][j] = dp_y[i-1][j-t] or dp_y[i-1][j+t]
        elif j-t < 0 and j+t <= 8000:
            dp_y[i][j] = dp_y[i-1][abs(j-t)] or dp_y[i-1][j+t]
        elif j-t >= 0 and j+t > 8000:
            dp_y[i][j] = dp_y[i-1][j-t]
        else:
            None
x = abs(x)
y = abs(y)
if len(L_x) == 0 and len(L_y) == 0:
    if x == y == 0:
        print('Yes')
    else:
        print('No')
elif len(L_x) != 0 and len(L_y) == 0:
    if y == 0 and dp_x[len(L_x)-1][x] == True:
        print('Yes')
    else:
        print('No')
elif len(L_x) == 0 and len(L_y) != 0:
    if x == 0 and dp_y[len(L_y)-1][y] == True:
        print('Yes')
    else:
        print('No')
else:
    if dp_x[len(L_x)-1][x] == True and dp_y[len(L_y)-1][y] == True:
        print('Yes')
    else:
        print('No')

Submission status: Wrong Answer

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'
flag_x = False
flag_y = False
if s[0] == 'F':
    cur_x = L_x[0]
    cur_y = 0
    for i in range(1, len(L_x)):
        if cur_x > x:
            cur_x -= L_x[i]
        elif cur_x < x:
            cur_x += L_x[i]
        else:
            gu = 0
            ki = 0
            for j in range(i, len(L_x)):
                if j % 2 == 0:
                    gu += L_x[j]
                else:
                    ki += L_x[j]
            if gu != ki:
                flag_x = False
                break
            else:
                flag_x = True
                break
else:
    cur_x = 0
    cur_y = 0
    for i in range(len(L_x)):
        if cur_x > x:
            cur_x -= L_x[i]
        elif cur_x < x:
            cur_x += L_x[i]
        else:
            gu = 0
            ki = 0
            for j in range(i, len(L_x)):
                if j % 2 == 0:
                    gu += L_x[j]
                else:
                    ki += L_x[j]
            if gu != ki:
                flag_x = False
                break
            else:
                flag_x = True
                break
for i in range(len(L_y)):
    if cur_y > y:
        cur_y -= L_y[i]
    elif cur_y < y:
        cur_y += L_y[i]
    else:
        gu = 0
        ki = 0
        for j in range(i, len(L_y)):
            if j % 2 == 0:
                gu += L_y[j]
            else:
                ki += L_y[j]
        if gu != ki:
            flag_y = False
            break
        else:
            flag_y = True
            break
if flag_x == flag_y == True:
    print('Yes')
elif flag_x == True and flag_y == False:
    if cur_y == y:
        print('Yes')
    else:
        print('No')
elif flag_x == False and flag_y == True:
    if cur_x == x:
        print('Yes')
    else:
        print('No')
else:
    if cur_x == x and cur_y == y:
        print('Yes')
    else:
        print('No')

Submission status: Wrong Answer

Solution code:
import sys
MAX = 8000

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

xa = []
ya = []

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

xasum = sum(xa)
yasum = sum(ya)

if xasum < abs(x) or yasum < abs(y) or xasum + yasum < abs(x) + abs(y):
    print("No")
    sys.exit(0)


dpx = [False]*(2*MAX+1)
dpy = [False]*(2*MAX+1)

dpx[MAX] = True
dpy[MAX] = True

for i in range(len(xa)):
    dp_tmp = [False]*(2*MAX+1)
    for j in range(2*MAX+1):
        if j + xa[i] <= 2*MAX:
            dp_tmp[j] = dp_tmp[j] or dpx[j+xa[i]]
        if j - xa[i] >= 0 and i > 0:
            dp_tmp[j] = dp_tmp[j] or dpx[j-xa[i]]
    for j in range(2*MAX+1):
        dpx[j] = dp_tmp[j]

for i in range(len(ya)):
    dp_tmp = [False]*(2*MAX+1)
    for j in range(2*MAX+1):
        if j + ya[i] <= 2*MAX:
            dp_tmp[j] = dp_tmp[j] or dpy[j+ya[i]]
        if j - ya[i] >= 0:
            dp_tmp[j] = dp_tmp[j] or dpy[j-ya[i]]
    for j in range(2*MAX+1):
        dpy[j] = dp_tmp[j]

if dpx[MAX+x] and dpy[MAX+y]:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
MAX = 8000

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

xa = []
ya = []

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

xasum = sum(xa)
yasum = sum(ya)

if xasum < abs(x) or yasum < abs(y) or xasum + yasum < abs(x) + abs(y):
    print("No")
    sys.exit(0)


dpx = [False]*(2*MAX+1)
dpy = [False]*(2*MAX+1)

dpx[MAX] = True
dpy[MAX] = True

for i in range(len(xa)):
    dp_tmp = [False]*(2*MAX+1)
    for j in range(2*MAX+1):
        if j + xa[i] <= 2*MAX:
            dp_tmp[j] = dp_tmp[j] or dpx[j+xa[i]]
        if j - xa[i] >= 0:
            dp_tmp[j] = dp_tmp[j] or dpx[j-xa[i]]
    for j in range(2*MAX+1):
        dpx[j] = dp_tmp[j]

for i in range(len(ya)):
    dp_tmp = [False]*(2*MAX+1)
    for j in range(2*MAX+1):
        if j + ya[i] <= 2*MAX:
            dp_tmp[j] = dp_tmp[j] or dpy[j+ya[i]]
        if j - ya[i] >= 0:
            dp_tmp[j] = dp_tmp[j] or dpy[j-ya[i]]
    for j in range(2*MAX+1):
        dpy[j] = dp_tmp[j]

if dpx[MAX+x] and dpy[MAX+y]:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
MAX = 8000

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

xa = []
ya = []

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

xasum = sum(xa)
yasum = sum(ya)


if xasum < abs(x) or yasum < abs(y):
    print("No")
    sys.exit(0)


dpx = [False]*(2*MAX+1)
dpy = [False]*(2*MAX+1)

dpx[xasum] = True
dpy[yasum] = True

for i in range(len(xa)):
    dp_tmp = [False]*(2*MAX+1)
    for j in range(2*MAX+1):
        if j + xa[i] <= 2*MAX:
            dp_tmp[j] = dp_tmp[j] or dpx[j+xa[i]]
        if j - xa[i] >= 0:
            dp_tmp[j] = dp_tmp[j] or dpx[j-xa[i]]
    for j in range(2*MAX+1):
        dpx[j] = dp_tmp[j]

for i in range(len(ya)):
    dp_tmp = [False]*(2*MAX+1)
    for j in range(2*MAX+1):
        if j + ya[i] <= 2*MAX:
            dp_tmp[j] = dp_tmp[j] or dpy[j+ya[i]]
        if j - ya[i] >= 0:
            dp_tmp[j] = dp_tmp[j] or dpy[j-ya[i]]
    for j in range(2*MAX+1):
        dpy[j] = dp_tmp[j]

if dpx[xasum+x] and dpy[yasum+y]:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys

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

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

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


dpx = [False]*(2*sum(xa)+1)
dpy = [False]*(2*sum(ya)+1)

dpx[sum(xa)] = True
dpy[sum(ya)] = True

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

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

if dpx[sum(xa)+x] and dpy[sum(ya)+y]:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

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)

x_error = sum(horizontal) - abs(x)
y_error = sum(vertical) - abs(y)

if x_error < 0 or y_error < 0:
    print("No")
    exit()
elif x_error == 0 and y_error == 0:
    print("Yes")
    exit()

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)

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: Wrong Answer

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)

x_error = sum(horizontal) - abs(x)
y_error = sum(vertical) - abs(y)

if x_error < 0 or y_error < 0:
    print("No")
    exit()
elif x_error == 0 and y_error == 0:
    print("Yes")
    exit()

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)

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: Wrong Answer

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)

x_error = sum(horizontal) - abs(x)
y_error = sum(vertical) - abs(y)

if x_error < 0 or y_error < 0:
    print("No")
    exit()
elif x_error == 0 and y_error == 0:
    print("Yes")
    exit()

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)

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(-sum(horizontal), sum(horizontal) + 1):
        if -sum(horizontal) <= j - x_ <= sum(horizontal): 
            dp_x[i][j + sx] = max(dp_x[i][j + sx], dp_x[i - 1][j - x_ + sx])
        if -sum(horizontal) <= j + x_ <= sum(horizontal):
            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(-sum(vertical), sum(vertical) + 1):
        if -sum(vertical) <= j - y_ <= sum(vertical): 
            dp_y[i][j + sy] = max(dp_y[i][j + sy], dp_y[i - 1][j - y_ + sy])
        if -sum(vertical) <= j + y_ <= sum(vertical):
            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_error + sx] = 1
if y_error == 0:
    dp_y[-1][-y_error + sy] = 1

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

Submission status: Wrong Answer

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)

x_error = sum(horizontal) - abs(x)
y_error = sum(vertical) - abs(y)

if x_error < 0 or y_error < 0:
    print("No")
    exit()
elif x_error == 0 and y_error == 0:
    print("Yes")
    exit()

dp_x = [[[0, 0] for _ in range(2*(x_error + 1) - 1)] for _ in range(len(horizontal) + 1)]
dp_y = [[[0, 0] for _ in range(2*(y_error + 1) - 1)] for _ in range(len(vertical) + 1)]

dp_x[0][0 + x_error] = [1, 1]
dp_y[0][0 + y_error] = [1, 1]

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

for i in range(1, len(vertical) + 1):
    x_ = vertical[i - 1]
    for j in range(-y_error, y_error + 1):
        for k in range(2):
            if k:
                x_ *= -1
            if 0 <= j - x_ + y_error < 2*y_error + 1:
                dp_y[i][j + y_error][k] = max(dp_y[i - 1][j - x_ + y_error])

if x_error == 0:
    dp_x[-1][-1] = [1, 1]
if y_error == 0:
    dp_y[-1][-1] = [1, 1]

if max(dp_x[-1][-1]) and max(dp_y[-1][-1]):
    print("Yes")
else:
    print("No")