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()+'T'
x,y=map(int,input().split())
if 'T' in s:
  pos=s.index('T')
  x-=pos
  s=s[pos+1:]
arr=s.split('T')
xarr=[]
yarr=[]
for i in range(len(arr)):
  l=len(arr[i])
  if l==0:
    continue
  if i%2==0:
    yarr.append(l)
  else:
    xarr.append(l)
if len(xarr)!=0:
  xsum=sum(xarr)
else:
  xsum=0
if len(yarr)!=0:
  ysum=sum(yarr)
else:
  ysum=0
x+=xsum
y+=ysum
if x%2==1 or y%2==1:
  print('No')
else:
  #DP for xarr,yarr
  xdp=[0]*(xsum+5)
  xdp[0]=1
  for val in xarr:
    for i in range(xsum+4,-1,-1):
      if i-val<0:
        break
      if xdp[i-val]==1:
        xdp[i]=1
  ydp=[0]*(ysum+5)
  ydp[0]=1
  for val in yarr:
    for i in range(ysum+4,-1,-1):
      if i-val<0:
        break
      if ydp[i-val]==1:
        ydp[i]=1
  if xdp[x//2]==0:
    print('No')
  else:
    if ydp[y//2]==0:
      print('No')
    else:
      print('Yes')

Submission status: Runtime Error

Solution code:
s=input()+'T'
x,y=map(int,input().split())
if 'T' in s:
  pos=s.index('T')
  x-=pos
  s=s[pos+1:]
arr=s.split('T')
xarr=[]
yarr=[]
for i in range(len(arr)):
  l=len(arr[i])
  if l==0:
    continue
  if i%2==0:
    yarr.append(l)
  else:
    xarr.append(l)
xsum=sum(xarr)
ysum=sum(yarr)
x+=xsum
y+=ysum
if x%2==1 or y%2==1:
  print('No')
else:
  #DP for xarr,yarr
  xdp=[0]*(xsum+5)
  xdp[0]=1
  for val in xarr:
    for i in range(xsum+4,-1,-1):
      if i-val<0:
        break
      if xdp[i-val]==1:
        xdp[i]=1
  ydp=[0]*(ysum+5)
  ydp[0]=1
  for val in yarr:
    for i in range(ysum+4,-1,-1):
      if i-val<0:
        break
      if ydp[i-val]==1:
        ydp[i]=1
  if xdp[x//2]==0:
    print('No')
  else:
    if ydp[y//2]==0:
      print('No')
    else:
      print('Yes')

Submission status: Runtime Error

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

tmp = n 
for i in range(n):
    if s[i] == "T":
        tmp = i
        break
    x -= 1
    
up_down = False
cnt = 0
ud =[]
lr = []
for i in range(tmp, n):
    if s[i] == "T":
        if up_down and cnt != 0:
            ud.append(cnt)
        if not up_down and cnt != 0:
            lr.append(cnt)
        cnt = 0
        up_down = not up_down
    else:
        cnt += 1

def is_subset_sum(array, k, x_y):
    if (k + x_y) % 2 != 0:
        return False
    MOD = 10**9 + 1
    n = len(array)

    # dp[i][j] := i番目までを選んだときにsumがjになるかどうか
    dp = [[False] * (k + 1) for i in range(n + 1)]
    dp[0][0] = True

    for i in range(n):
        for j in range(k + 1):
            if j - array[i] < 0:
                dp[i + 1][j] = dp[i][j]
            else:
                dp[i + 1][j] = dp[i][j] or dp[i][j - array[i]]

    return dp[n][abs(k + x_y)//2]


if is_subset_sum(lr, sum(lr), x) and is_subset_sum(ud, sum(ud), y):
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

Solution code:
s=input()
x,y=map(int,input().split())
X,Y=[],[]
ct,dir,ss=0,1,1
for i in range(len(s)):
  if s[i]=="F":
    ct+=1
    if i==len(s)-1:
      if dir==1:
        X.append(ct)
      else:
        Y.append(ct)
  else:
    if ss==1:
      x-=ct
      ct,dir,ss=0,0,0
    elif dir==1:
      X.append(ct)
      ct,dir=0,0
    else:
      Y.append(ct)
      ct,dir=0,1

def chk(Z,z):
  tot,nz=sum(Z),len(Z)
  if tot==0:
    if z==0:
      return True
    else:
      return False
  elif z>tot:
    return False
  else:
    DP=[[False]*(2*tot+1) for _ in range(nz+1)]
    DP[0][tot]=True
    for i in range(nz):
      zz=Z[i]
      for j in range(2*tot+1):
        if j+zz<2*tot+1:
          DP[i+1][j]=DP[i][j+zz]
        elif j-zz>=0:
          DP[i+1][j]=DP[i][j-zz]
    return DP[nz][z+tot]

if chk(X,x) and chk(Y,y):
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

Solution code:
s=input()
x,y=map(int,input().split())
X,Y=[],[]
ct,dir,ss=0,1,1
for i in range(len(s)):
  if s[i]=="F":
    ct+=1
    if i==len(s)-1:
      if dir==1 and ct!=0:
        X.append(ct)
      elif dir==0 and ct!=0:
        Y.append(ct)
  else:
    if ss==1:
      x-=ct
      ct,dir,ss=0,0,0
    elif dir==1 and ct!=0:
      X.append(ct)
      ct,dir=0,0
    elif dir==0 and ct!=0:
      Y.append(ct)
      ct,dir=0,1

def chk(Z,z):
  tot,nz=sum(Z),len(Z)
  if tot==0:
    if z==0:
      return True
    else:
      return False
  elif z>tot:
    return False
  else:
    DP=[[False]*(2*tot+1) for _ in range(nz+1)]
    DP[0][tot]=True
    for i in range(nz):
      zz=Z[i]
      for j in range(2*tot+1):
        if j+zz<2*tot+1:
          DP[i+1][j]=DP[i][j+zz]
        elif j-zz>=0:
          DP[i+1][j]=DP[i][j-zz]
    return DP[nz][z+tot]

if chk(X,x) and chk(Y,y):
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

Solution code:
s=input()
x,y=map(int,input().split())
X,Y=[],[]
ct,dir,ss=0,1,1
for i in range(len(s)):
  if s[i]=="F":
    ct+=1
    if i==len(s)-1:
      if dir==1:
        X.append(ct)
      else:
        Y.append(ct)
  else:
    if ss==1:
      x-=ct
      ct,dir,ss=0,0,0
    elif dir==1:
      X.append(ct)
      ct,dir=0,0
    else:
      Y.append(ct)
      ct,dir=0,1

def chk(Z,z):
  tot,nz=sum(Z),len(Z)
  if tot==0:
    if z==0:
      return True
    else:
      return False
  else:
    DP=[[False]*(2*tot+1) for _ in range(nz+1)]
    DP[0][tot]=True
    for i in range(nz):
      zz=Z[i]
      for j in range(2*tot+1):
        if j+zz<2*tot+1:
          DP[i+1][j]=DP[i][j+zz]
        elif j-zz>=0:
          DP[i+1][j]=DP[i][j-zz]
    return DP[nz][z+tot]

if chk(X,x) and chk(Y,y):
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

Solution code:
s=input()
x,y=map(int,input().split())
X,Y=[],[]
ct,dir,ss=0,1,1
for i in range(len(s)):
  if s[i]=="F":
    ct+=1
    if i==len(s)-1:
      if dir==1:
        X.append(ct)
      else:
        Y.append(ct)
  else:
    if ss==1:
      x-=ct
      ct,dir,ss=0,0,0
    elif dir==1:
      X.append(ct)
      ct,dir=0,0
    else:
      Y.append(ct)
      ct,dir=0,1

def chk(Z,z):
  tot,nz=sum(Z),len(Z)
  if tot==0:
    if z==0:
      return True
    else:
      return False
  else:
    DP=[[False]*(2*tot+1) for _ in range(nz+1)]
    DP[0][tot]=True
    for i in range(nz):
      zz=Z[i]
      for j in range(2*tot+1):
        if j+zz<2*tot+1:
          DP[i+1][j]=DP[i][j+zz]
        elif j-zz>=0:
          DP[i+1][j]=DP[i][j-zz]
    return DP[nz][z+tot]

if chk(X,x) and chk(Y,y):
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

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

def chk(Z,z):
  tot=sum(Z)
  nz=len(Z)
  DP=[[False]*(2*tot+1) for _ in range(nz+1)]
  DP[0][tot]=True
  for i in range(nz):
    zz=Z[i]
    for j in range(2*tot+1):
      if j+zz<2*tot+1 and DP[i][j+zz]==True:
        DP[i+1][j]=True
      elif j-zz>=0 and DP[i][j-zz]==True:
        DP[i+1][j]=True
  return DP[nz][z+tot]

if chk(X,x) and chk(Y,y):
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

Solution code:
from itertools import accumulate
S=list(map(len,input().split("T")))
x,y=map(int,input().split())
x=abs(x-S[0])
y=abs(y)
L=S[2::2]
R=S[1::2]
La=list(accumulate(L))
Ra=list(accumulate(R))
sumL=sum(L)
sumR=sum(R)
flag=0
if sumL>=x and sumR>=y and (sumL-x)%2==0 and (sumR-y)%2==0 and flag==0:
    setL1={0}
    setR1={0}
    gL=(sumL-x)//2
    gR=(sumR-y)//2
    if gL==0:
        flag=1
    else:
        for cnt,i in enumerate(L):
            setL=setL1
            setL1=set()
            s=setL1.add
            h=sumL-La[cnt]
            for j in setL:
                k=i+j
                #if k<gL and h+k>=gL:
                    s(k)
                #if j<gL and h+j>=gL:
                    s(j)
                elif k==gL:
                    flag=1
                    break
            if flag==1:
                break
    if flag==1:
        if gR==0:
            flag=2
        else:
            for cnt,i in enumerate(R):
                setR=setR1
                setR1=set()
                s=setR1.add
                h=sumR-Ra[cnt]
                for j in setR:
                    k=i+j
                    #if k<gR and h+k>=gR:
                        s(k)
                    #if j<gR and h+j>=gR:
                        s(j)
                    elif k==gR:
                        flag=2
                        break
                if flag==2:
                    break
if flag==2:
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

Solution code:
from itertools import accumulate
S=list(map(len,input().split("T")))
x,y=map(int,input().split())
x=abs(x-S[0])
y=abs(y)
L=S[2::2]
R=S[1::2]
La=list(accumulate(L))
Ra=list(accumulate(R))
sumL=sum(L)
sumR=sum(R)
flag=0    
if sumL>=x and sumR>=y and (sumL-x)%2==0 and (sumR-y)%2==0 and flag==0:
    setL1={0}
    setR1={0}
    gL=(sumL-x)//2
    gR=(sumR-y)//2
    if gL==0:
        flag=1
    else:
        for i,cnt in enumerate(L):
            setL=setL1
            setL1=set()
            s=setL1.add
            h=sumL-La[cnt]
            for j in setL:
                k=i+j
                if k<gL and h+k>=gL:
                    s(k)
                if j<gL and h+j>=gL:
                    s(j)
                elif k==gL:
                    flag=1
                    break
            if flag==1:
                break
    if flag==1:
        if gR==0:
            flag=2
        else:
            for i in R:
                for j in setR:
                    setR=setR1
                    setR1=set()
                    s=setR1.add
                    h=sumR-Lb[cnt]
                    k=i+j
                    if k<gR and h+k>=gR:
                        s(k)
                    if j<gR and h+j>=gR:
                        s(j)
                    elif k==gR:
                        flag=2
                        break
                if flag==2:
                    break
if flag==2:
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

Solution code:
S=list(map(len,input().split("T")))
x,y=map(lambda x:abs(int(x)),input().split())
L=S[::2]
R=S[1::2]
sumL=sum(L)
sumR=sum(R)
flag=0    
if sumL>=x and sumR>=y and (sumL-x)%2==0 and (sumR-y)%2==0 and flag==0:
    setL={0}
    setR={0}
    gL=(sumL-x)//2
    gR=(sumR-y)//2
    if gL==0:
        flag=1
    else:
        for i in L:
            for j in setL:
                k=i+j
                if k<gL:
                    setL.add(k)
                elif k==gL:
                    flag=1
                    break
            if flag==1:
                break
    if flag==1:
        if gR==0:
            flag=2
        else:
            for i in R:
                for j in setR:
                    k=i+j
                    if k<gR:
                        setR.add(k)
                    elif k==gR:
                        flag=2
                        break
                if flag==2:
                    break
if flag==2:
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

Solution code:
s=input()
x,y=map(int,input().split())
h=[]
v=[]
num=0
m=1
for i in s:
    if i=='F':
        num+=1
    else:
        if num!=0:
            if m==1:
                h.append(num)
            else:
                v.append(num)
        m=-m
        num=0
if num!=0:
    if m==1:
        h.append(num)
    else:
        v.append(num)
if s[0]=='F':
    x=x-h[0]
    del h[0]
sumh=sum(h)
nh=len(h)
dph=[]
if nh!=0:
    for i in range(nh+1):
        sub_list=[False]*(1+2*sumh)
        dph.append(sub_list)
    dph[0][sumh]=True
    for i in range(1,nh+1):
        for j in range(1+2*sumh):
            if j-h[i-1]>=0:
                dph[i][j]=dph[i][j] or dph[i-1][j-h[i-1]]
            if j+h[i-1]<=2*sumh:
                dph[i][j]=dph[i][j] or dph[i-1][j+h[i-1]]
    sh=dph[nh][x+sumh]
else:
    if x==0:
        sh=True
    else:
        sh=False
sumv=sum(v)
nv=len(v)
dpv=[]
if nv!=0:
    for i in range(nv+1):
        sub_list=[False]*(1+2*sumv)
        dpv.append(sub_list)
    dpv[0][sumv]=True
    for i in range(1,nv+1):
        for j in range(1+2*sumv):
            if j-v[i-1]>=0:
                dpv[i][j]=dpv[i][j] or dpv[i-1][j-v[i-1]]
            if j+v[i-1]<=2*sumv:
                dpv[i][j]=dpv[i][j] or dpv[i-1][j+v[i-1]]
    sv=dpv[nv][y+sumv]
else:
    if y==0:
        sv=True
    else:
        sv=False
if sh and sv:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
from sys import exit
 
S = str(input())#["F","F","T","F"]
Gx, Gy = [int(n) for n in input().split()]#[4,4]
Xds = []
Yds = []
X = True
L = len(S)
for s in S.split("T"):
    if X:
        Xds.append(len(s))
    else:
        Yds.append(len(s))
    X = not X
# print(Xds)
# print(Yds)
if(len(Xds) != 0):
    memox = [set([Xds[0]])] + [0]*(len(Xds)-1)
else:
    memox = [0]
for i in range(1,len(Xds)):
    can = set()
    for x in memox[i-1]:
        can.add(x+Xds[i])
        can.add(x-Xds[i])
    memox[i]=can
 
if(len(Yds) != 0):
    memoy = [set([Yds[0],-Yds[0]])] + [0]*(len(Yds)-1)
else:
    memoy = [0]
for i in range(1,len(Yds)):
    can = set()
    for y in memoy[i-1]:
        can.add(y+Yds[i])
        can.add(y-Yds[i])
    memoy[i]=can
if Gx in memox[-1] and Gy in memoy[-1]:
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
import collections

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

def solve(S: str, X: int, Y: int):
    is_x_ok = False
    is_y_ok = False

    S2 = S.split("T")
    dp = [[0 for _ in range(17000)] for _ in range(len(S2[0::2]))] 
    dp[0][len(S2[0])+8000] = True

    maxx = len(S2[0])
    for i,s in enumerate(S2[0::2]):
        if i > 0:
            dx = len(s)
            maxx += dx
            for x in range(-maxx,maxx+1):
                dp[i][x+8000] = dp[i-1][x-dx+8000] or dp[i-1][x+dx+8000]

    if dp[len(S2[0::2])-1][X+8000]:
        is_x_ok = True

    dp = [[0 for _ in range(17000)] for _ in range(len(S2[1::2]))] 

    if len(S2) > 1:
        dp[0][len(S2[1])+8000] = True
        dp[0][-len(S2[1])+8000] = True

        maxy = len(S2[1])
        for i,s in enumerate(S2[1::2]):
            if i > 0:
                dy = len(s)
                maxy += dy
                for y in range(-maxy,maxy+1):
                    dp[i][y+8000] = dp[i-1][y-dy+8000] or dp[i-1][y+dy+8000]

        if dp[len(S2[1::2])-1][Y+8000]:
            is_y_ok = True
    elif Y == 0:
        is_y_ok = True

    if is_x_ok and is_y_ok:
        print(YES)
    else:
        print(NO)



# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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: Runtime Error

Solution code:
def cul(x,X):
    dic={0:1}
    for i in range(len(x)):
        tmp={}
        for j in dic:
            tmp[j+x[i]]=1
            tmp[j-x[i]]=1
        dic=tmp
    if X in dic: return True
    else: return False

s=list(input().split("T"))
X,Y=map(int,input().split())
x,y=[len(i) for i in s[::2]],[len(i) for i in s[1::2]]
if s[0][0]=="F": X=abs(X-x.pop(0))
Y=abs(Y)

x.sort(reverse=True)
y.sort(reverse=True)

print("Yes" if cul(x,X) and cul(y,Y) else "No")

Submission status: Runtime Error

Solution code:
#from numpy import*
#from scipy import*
from collections import* #defaultdict Counter deque appendleft
from fractions import gcd
from functools import* #reduce
from itertools import* #permutations("AB",repeat=2) combinations("AB",2) product("AB",2) groupby
from operator import mul
from bisect import* #bisect_left bisect_right
from heapq import* #heapify heappop heappushpop
from math import factorial
from copy import deepcopy
import sys
#input=sys.stdin.readline  #危険！基本オフにしろ！
sys.setrecursionlimit(10**6)

def main():
    *s,=map(len,input().split("T"))
    x,y=map(int,input().split())
    start=s[0]
    n=s[2::2]
    m=s[1::2]
#    print(n,m)
#    print(start)
    dx=deque()
    dy=deque()
    dx.append(start)
    dy.append(0)
    for i in range(len(n)):
        tmp=deque()
        for j in dx.copy():
            tmp.appendleft(j+n[i])
            tmp.appendleft(j-n[i])
        dx=tmp
#    print(dx)
    for i in range(len(m)):
        tmp=deque()
        for j in dy.copy():
            tmp.appendleft(j+m[i])
            tmp.appendleft(j-m[i])
        dy=tmp
#    print(dy)
    if x in dx and y in dy:
        print("Yes")
    else:
        print("No")


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
# gcd
# from fractions import gcd
# 切り上げ，切り捨て
# from math import ceil, floor
# リストの真のコピー（変更が伝播しない）
# from copy import deepcopy
# 累積和。list(accumulate(A))としてAの累積和
# from itertools import accumulate
# l = ['a', 'b', 'b', 'c', 'b', 'a', 'c', 'c', 'b', 'c', 'b', 'a']
# S = Counter(l)  # カウンタークラスが作られる。S=Counter({'b': 5, 'c': 4, 'a': 3})
# print(S.most_common(2))  # [('b', 5), ('c', 4)]
# print(S.keys())  # dict_keys(['a', 'b', 'c'])
# print(S.values())  # dict_values([3, 5, 4])
# print(S.items())  # dict_items([('a', 3), ('b', 5), ('c', 4)])
# from collections import Counter
# import math
# from functools import reduce
#
# input関係の定義
fin = open('in_6.txt', 'r')
sys.stdin = fin
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())
# template

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

def iterate_tokens():
    for line in sys.stdin:
        for word in line.split():
            yield word


if __name__ == '__main__':

    tokens = iterate_tokens()
    s = list(next(tokens))  # type: str
    x = int(next(tokens))  # type: int
    y = int(next(tokens))  # type: int

    # write code
    x_axis = []
    y_axis = []
    i = 0
    cntT = 0
    cntF = 0
    for i in range(len(s)):
        if s[i] == 'T':
            if cntT % 2 == 0:
                x_axis.append(cntF)
            else:
                y_axis.append(cntF)
            cntT += 1
            cntF = 0
            continue
        else:
            cntF += 1
            continue
    if cntT % 2 == 0:
        x_axis.append(cntF)
    else:
        y_axis.append(cntF)


    x -= x_axis[0]
    del x_axis[0]
    # print(x_axis, y_axis)
    x_set = {x}
    y_set = {y}
    for num in x_axis:
        x_set = {i + k for i in x_set for k in {-num, num} if - 8000 <= i <= 8000}
    for num in y_axis:
        y_set = {i+k for i in y_set for k in {-num,num} if -8000<=i<=8000}

    # print(x_set, y_set)
    if 0 in x_set and 0 in y_set:
        print(YES)
    else:
        print(NO)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

s = input()
x, y = map(int, input().split())
a = 0
t = [[0], [0]]
n = 0
for i in range(len(s)):
    if s[i] == 'F':
        t[n][-1] += 1
    else:
        n = 1 - n
        t[n].append(0)
flag = [False, False]
z = [x, y]
for k in range(2):
    tt = t[k]
    zk = z[k]
    p = sum(tt)
    if zk > p or zk < -p:
        flag = False
        break
    dp = defaultdict(bool)
    i = 1
    if k == 0:
        dp[tt[0]] = True
    else:
        dp[tt[0]] = True
        dp[-tt[0]] = True
    if len(tt) > 1:
        dp2 = defaultdict(bool)
        for tj in tt[1:]:
            for xj in dp.keys():
                dp2[xj + tj] = True
                dp2[xj - tj] = True
            dp = dp2
    flag = dp[zk]
    if not flag:
        break
if flag:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
s = input()
x, y = map(int, input().split())
a = 0
t = [[0], [0]]
n = 0
for i in range(len(s)):
    if s[i] == 'F':
        t[n][-1] += 1
    else:
        n = 1 - n
        t[n].append(0)
flag = [False, False]
z = [x, y]
for k in range(2):
    tt = t[k]
    zk = z[k]
    p = sum(tt)
    dp = [[False for _ in range(2 * p + 1)] for i in range(2)]
    i = 1
    if k == 0:
        dp[0][p + tt[0]] = True
    else:
        dp[0][p + tt[0]] = True
        dp[0][p - tt[0]] = True
    if len(tt) > 1:
        for tj in tt[1:]:
            for j in range(2 * p + 1):
                h = False
                if j >= tj:
                    h = h or dp[1 - i][j - tj]
                elif j <= 2 * p - tj:
                    h = h or dp[1 - i][j + tj]
                dp[i][j] = h
            i = 1 - i
    if dp[1 - i][zk + p]:
        flag = True
    else:
        flag = False
        break
if flag:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def yes():
    print("Yes")  # type: str


def no():
    print("No")  # type: str


def solve(s: str, x: int, y: int):
    s = [len(FF) for FF in s.split("T")]
    sX = s[0::2]                # 空となるケースに注意
    sY = s[1::2]                # 空となるケースに注意
    # print(sX, sY)

    # sXを足すか引くかして、xにたどり着くことができるか
    N = len(s)
    DPx = [False]*(2*N+1)  # [-len(s):len(s)]
    DPx[0] = True
    for c in sX:
        DPnext = [False]*(2*N+1)
        for i in range(-N, N+1):
            DPnext[i] = DPx[i-c]
            if i+c < N+1:
                DPnext[i] = DPnext[i] or DPx[i+c]
        DPx = DPnext
        # print(DPx)

    DPy = [False]*(2*N+1)
    DPy[0] = True
    for c in sY:
        DPnext = [False]*(2*N+1)
        for i in range(-N, N+1):
            DPnext[i] = DPy[i-c]
            if i+c < N+1:
                DPnext[i] = DPnext[i] or DPy[i+c]
        DPy = DPnext
    # print(DPy)

    if DPx[x] and DPy[y]:
        yes()
    else:
        no()
    return


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

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


lr = []
tb = []
cnt = 0
even = 0
for c in s:
    if c == 'F':
        cnt += 1
    if c == 'T':
        even += 1
        if even == 1:
            x -= cnt
            continue
        if even %2 == 0:
            tb += [cnt]
            cnt = 0
        else:
            lr += [cnt]
            cnt = 0
even += 1
if even %2 == 0:
    tb += [cnt]
    cnt = 0
else:
    lr += [cnt]
    cnt = 0    
lrdif = sum(lr) - x
tbdif = sum(tb) - y
lr.sort(reverse = True)
tb.sort(reverse = True)
while lrdif != 0:

Submission status: Runtime Error

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

x_forwards = [0]
y_forwards = []
flag = 'x'
for i in range(len(s)):
    if s[i] == 'F':
        if flag == 'x':
            x_forwards[-1] += 1
        else:
            y_forwards[-1] += 1
    else:
        if flag == 'x':
            flag = 'y'
            y_forwards.append(0)
        else:
            flag = 'x'
            x_forwards.append(0)

import numpy as np
if len(x_forwards) != 1:
    t = sum(x_forwards)
    dp_x = np.zeros((len(x_forwards), 2 * t + 1))
    dp_x[0, t + x_forwards[0]] = 1
    for i in range(1, len(x_forwards)):
        for j in range(2 * t + 1):
            if j + x_forwards[i] >= 2 * t + 1:
                dp_x[i, j] = dp_x[i - 1, j - x_forwards[i]]
            elif j - x_forwards[i] < 0:
                dp_x[i, j] = dp_x[i - 1, j + x_forwards[i]]
            else:
                dp_x[i, j] = min(dp_x[i - 1, j-x_forwards[i]] + dp_x[i-1, j+x_forwards[i]], 1)

    if dp_x[-1, x + t]:
        flag_x = True
    else:
        flag_x = False
else:
    if x_forwards[0] == x:
        flag_x = True
    else:
        flag_x = False

if len(y_forwards) != 0:
    t = sum(y_forwards)
    dp_y = np.zeros((len(y_forwards), 2 * t + 1))
    dp_y[0, t + y_forwards[0]] = 1
    dp_y[0, t - y_forwards[0]] = 1
    for i in range(1, len(y_forwards)):
        for j in range(2 * t + 1):
            if j + y_forwards[i] >= 2 * t + 1:
                dp_y[i, j] = dp_y[i - 1, j - y_forwards[i]]
            elif j - y_forwards[i] < 0:
                dp_y[i, j] = dp_y[i - 1, j + y_forwards[i]]
            else:
                dp_y[i, j] = min(dp_y[i - 1, j-y_forwards[i]] + dp_y[i-1, j+y_forwards[i]], 1)

    if dp_y[-1, y + t]:
        flag_y = True
    else:
        flag_y = False
else:
    if y == 0:
        flag_y = True
    else:
        flag_y = False

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

Submission status: Runtime Error

Solution code:
from collections import  defaultdict

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

# 0: x-direction, 1: y-direction
dist = {0: [], 1: []}
current = 0
temp = 0
for move in s:
    if move == 'F':
        temp += 1
    else:
        dist[current].append(temp)
        temp = 0
        current ^= 1
if temp > 0:
    dist[current].append(temp)

xdp = {i: defaultdict(bool) for i in range(len(dist[0]))}
ydp = {i: defaultdict(bool) for i in range(len(dist[1]))}

xdp[0][dist[0][0]] = True
for i in range(1, len(dist[0])):
    d = dist[0][i]
    for j in range(-N, N+1):
        xdp[i][j] = xdp[i-1][j-d] | xdp[i-1][j+d] 

ydp[0][dist[1][0]] = True
ydp[0][-dist[1][0]] = True
for i in range(1, len(dist[1])):
    d = dist[1][i]
    for j in range(-N, N+1):
        ydp[i][j] = ydp[i-1][j-d] | ydp[i-1][j+d] 

if xdp[len(dist[0])-1][x] & ydp[len(dist[1])-1][y]:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
from collections import  defaultdict

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

# 0: x-direction, 1: y-direction
dist = {0: [], 1: []}
current = 0
temp = 0
for move in s:
    if move == 'F':
        temp += 1
    else:
        dist[current].append(temp)
        n_move[current] += 1
        temp = 0
        current ^= 1
if temp > 0:
    dist[current].append(temp)
    n_move[current] += 1
        
xdp = {i: defaultdict(bool) for i in range(len(dist[0]))}
ydp = {i: defaultdict(bool) for i in range(len(dist[1]))}

xdp[0][dist[0][0]] = True
for i in range(1, len(dist[0])):
    d = dist[0][i]
    for j in range(-N, N+1):
        xdp[i][j] = xdp[i-1][j-d] | xdp[i-1][j+d] 

ydp[0][dist[1][0]] = True
ydp[0][-dist[1][0]] = True
for i in range(1, len(dist[1])):
    d = dist[1][i]
    for j in range(-N, N+1):
        ydp[i][j] = ydp[i-1][j-d] | ydp[i-1][j+d] 

if xdp[len(dist[0])-1][x] & ydp[len(dist[1])-1][y]:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
import copy

s=list(input())
s.append("T")
A,B=map(int,input().split())

H=[]
W=[]
cnt=0
flag="w"
for i in range(len(s)):
    if s[i]=="F":
        cnt+=1
    else:
        if flag=="w":
            W.append(cnt)
            cnt=0
            flag="h"
        else:
            H.append(cnt)
            cnt=0
            flag="w"

S=W[0]
del W[0]

#print(H)
#print(W)

X=[0 for i in range(sum(W)*2+1)]
X[sum(W)]=1
ZX=[0 for i in range(sum(W)*2+1)]
for i in W:
    x=copy.deepcopy(ZX)
    for j in range(sum(W)*2+1):
        if X[j]==1:
            x[j-i]=1
            x[j+i]=1
    X=copy.deepcopy(x)
#print(X)
Y=[0 for i in range(sum(H)*2+1)]
Y[sum(H)]=1
ZY=[0 for i in range(sum(H)*2+1)]
for i in H:
    y=copy.deepcopy(ZY)
    for j in range(sum(H)*2+1):
        if Y[j]==1:
            y[j-i]=1
            y[j+i]=1
    Y=copy.deepcopy(y)
#print(Y)
if sum(W)<(A-S) or sum(H)<B:
    print("No")
    exit()
if X[sum(W)+A-S]==1 and Y[sum(H)+B]==1:
    print("Yes")
else:
    print("No")
#print(sum(W)+A-S)

Submission status: Runtime Error

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


def dfs(x, y, si, f):
    if len(s) == si:
        if (x, y) == (gx, gy):
            return True
        return False
    ans = False
    if s[si] == 'T':
        return dfs(x, y, si + 1, not f)
    if f:
        for xx, yy in [(x + 1, y), (x - 1, y)]:
            ans = ans or dfs(xx, yy, si + 1, f)
    else:
        for xx, yy in [(x, y + 1), (x, y - 1)]:
            ans = ans or dfs(xx, yy, si + 1, f)
    return ans


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


main()

Submission status: Runtime Error

Solution code:
def dfs(x, y, gx, gy, s, f):
    if len(s) == 0:
        if (x, y) == (gx, gy):
            return True
        return False
    ans = False
    if s[0] == 'T':
        if f:
            ans = ans or dfs(x, y, gx, gy, s[1:], not f)
        else:
            ans = ans or dfs(x, y, gx, gy, s[1:], not f)
    else:
        if f:
            for xx, yy in [(x + 1, y), (x - 1, y)]:
                ans = ans or dfs(xx, yy, gx, gy, s[1:], f)
        else:
            for xx, yy in [(x, y + 1), (x, y - 1)]:
                ans = ans or dfs(xx, yy, gx, gy, s[1:], f)
    return ans


def main():
    s = input()
    x, y = map(int, input().split())
    if dfs(0, 0, x, y, s, True):
        print('Yes')
    else:
        print('No')


main()

Submission status: Runtime Error

Solution code:
def dfs(x, y, gx, gy, s, f):
    if len(s) == 0:
        if (x, y) == (gx, gy):
            return True
        return False
    ans = False
    if s[0] == 'T':
        if f:
            for xx, yy in [(x, y + 1), (x, y - 1)]:
                ans = ans or dfs(xx, yy, gx, gy, s[1:], f)
        else:
            for xx, yy in [(x + 1, y), (x - 1, y)]:
                ans = ans or dfs(xx, yy, gx, gy, s[1:], f)
    else:
        if f:
            for xx, yy in [(x + 1, y), (x - 1, y)]:
                ans = ans or dfs(xx, yy, gx, gy, s[1:], f)
        else:
            for xx, yy in [(x, y + 1), (x, y - 1)]:
                ans = ans or dfs(xx, yy, gx, gy, s[1:], f)
    return ans


def main():
    s = input()
    x, y = map(int, input().split())
    if dfs(0, 0, x, y, s, True):
        print('Yes')
    else:
        print('No')


main()