Problem:


Score : 500 points

Problem Statement

A robot is put at the origin in a two-dimensional plane.
Initially, the robot is facing in the positive x-axis direction.

This robot will be given an instruction sequence s.
s consists of the following two kinds of letters, and will be executed in order from front to back.

F : Move in the current direction by distance 1.

T : Turn 90 degrees, either clockwise or counterclockwise.

The objective of the robot is to be at coordinates (x, y) after all the instructions are executed.
Determine whether this objective is achievable.

Constraints

s consists of F and T.

1 \leq |s| \leq 8 000

x and y are integers.

|x|, |y| \leq |s|

Input

Input is given from Standard Input in the following format:

s
x y

Output

If the objective is achievable, print Yes; if it is not, print No.

Sample Input 1

FTFFTFFF
4 2

Sample Output 1

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning clockwise in the second T.

Sample Input 2

FTFFTFFF
-2 -2

Sample Output 2

Yes

The objective can be achieved by, for example, turning clockwise in the first T and turning clockwise in the second T.

Sample Input 3

FF
1 0

Sample Output 3

No

Sample Input 4

TF
1 0

Sample Output 4

No

Sample Input 5

FFTTFF
0 0

Sample Output 5

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning counterclockwise in the second T.

Sample Input 6

TTTT
1 0

Sample Output 6

No



Sample input:
FTFFTFFF
4 2


Sample output:
Yes

Submission status: Accepted

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

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

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

Submission status: Accepted

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

t=[[s[0],1]]
for i in range(1,len(s)):
    if t[-1][0]==s[i]:
        t[-1][1]+=1
    else:
        t.append([s[i],1])

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

Submission status: Accepted

Solution code:
from sys import exit
from itertools import groupby

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from sys import exit
s=input()
xy=list(map(int,input().split()))

def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2
s=groupby(s)
ab=[[],[]]
turn=True
#初めまっすぐの場合
if s[0][0]=="F":
    xy[0]-=s[0][1]
    s.pop(0)
for i in s:
    if i[0]=="F":
        ab[not turn].append(i[1])
    elif i[1]%2==1:
        turn=not turn
for i in range(2):
    ab[i].sort(reverse=True)
    for j in ab[i]:
        xy[i]=abs(xy[i])-abs(j)
    if xy[i]!=0:
        print("No")
        exit()
print("Yes")

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2
s=groupby(s)
a,b=[],[]
turn=True
if s[0][0]=="F":
    x-=s[0][1]
    s.pop(0)
for i in s:
    if i[0]=="F":
        if turn:
            a.append(i[1])
        else:
            b.append(i[1])
    else:
        if i[1]%2==1:
            turn=not turn
a.sort(reverse=True)
b.sort(reverse=True)
for i in a:
    if x>0:
        x-=i
    else:
        x+=i
for i in b:
    if y>0:
        y-=i
    else:
        y+=i
if x!=0 or y!=0:
    print("No")
else:
    print("Yes")

Submission status: Accepted

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

i = 0
while i < len(s) and s[i] =="F":
    i += 1
x -= i
s = s[i:]

m = [[],[]]
num = 0

for i in range(len(s)):
    if s[i] == "F":
        m[num][-1] += 1
    else:
        num += 1
        num %= 2
        m[num].append(0)

#print("m",m)

sumx = sum(m[0])

dpx = [False]*(2*sumx+3) #index(sumx+1)が座標0
dpx[sumx+1] = True
#print("dpx",dpx)

for p in m[0]:
    dpx_pre = dpx[:]
    dpx = [False]*(2*sumx+3)
    for i in range(2*sumx+3):
        if i-p >= 0:
            dpx[i-p] = dpx_pre[i] or dpx[i-p]
            #print("dpx",dpx)
        if i+p < 2*sumx+3:
            dpx[i+p] = dpx_pre[i] or dpx[i+p]
            #print("dpx",dpx)
    #print("dpx",dpx)

sumy = sum(m[1])
dpy = [False]*(2*sumy+3) #index(sumy+1)が座標0
dpy[sumy+1] = True
#print("dpy",dpy)

for p in m[1]:
    dpy_pre = dpy[:]
    dpy = [False]*(2*sumy+3)
    for i in range(2*sumy+3):
        if i-p >= 0:
            dpy[i-p] = dpy_pre[i] or dpy[i-p]
        if i+p < 2*sumy+3:
            dpy[i+p] = dpy_pre[i] or dpy[i+p]
    #print("dpy",dpy)

if abs(x) > sumx+1 or abs(y) > sumy+1:
    print("No")
elif dpx[x+(sumx+1)] and dpy[y+(sumy+1)]:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
import sys
INF = 10 ** 9
MOD = 10 ** 9 + 7
from collections import deque
sys.setrecursionlimit(100000000)

def main(): 
    s = input() + 'a'
    x,y = map(int,input().split())
    
    cnt_t = 0
    cnt_f = 0
    p = []
    q = []
    before = True
    for i in range(len(s)-1):
        if s[i] == 'T' :
            cnt_t += 1
        elif s[i] == 'F' and s[i+1] == 'F':
            cnt_f += 1
        else:
            cnt_f += 1
            if cnt_t == 0:
                x -= cnt_f
            elif cnt_t%2 == 0:
                if before:
                    p.append(cnt_f)
                else:
                    q.append(cnt_f)
            else:
                if before:
                    q.append(cnt_f)
                    before = False
                else:
                    p.append(cnt_f)
                    before = True
            cnt_t = 0
            cnt_f = 0
    
    
    p.sort(reverse=True)
    q.sort(reverse=True)

    s = 0
    for i in range(len(p)):
        if s <= x:
            s += p[i]
        else:
            s -= p[i]
    
    t = 0
    for i in range(len(q)):
        if t <= y:
            t += q[i]
        else:
            t -= q[i]
    
    if s == x and t == y:
        print('Yes')
    else:
        print('No')

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
s=input()
x,y=map(int,input().split())
lx=[]
ly=[]
t=0
k=0
for i in s:
    if i=="F":
        t+=1
    else:
        if k:
            ly.append(t)
            k=0
        else:
            lx.append(t)
            k=1
        t=0
if k:
    ly.append(t)
else:
    lx.append(t)
x-=lx.pop(0)
sx,sy={0},{0}
for i in lx:
    t=set()
    for j in sx:
        t.add(i+j)
        t.add(j-i)
    sx=t
for i in ly:
    t=set()
    for j in sy:
        t.add(i+j)
        t.add(j-i)
    sy=t
if x in sx and y in sy:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
s=input()
x,y=map(int,input().split())
lx=[]
ly=[]
t=0
k=0
for i in s:
    if i=="F":
        t+=1
    else:
        if k:
            ly.append(t)
            k=0
        else:
            lx.append(t)
            k=1
        t=0
if k:
    ly.append(t)
else:
    lx.append(t)
x-=lx.pop(0)
sx,sy=sum(lx),sum(ly)
dx=[0]*(sx*2+1)
dy=[0]*(sy*2+1)
dx[0],dy[0]=1,1
for i in lx:
    t=[0]*(sx*2+1)
    for j in range(-sx,sx+1):
        if dx[j]:
            t[i+j]=1
            t[j-i]=1
    dx=t
for i in ly:
    t=[0]*(sy*2+1)
    for j in range(-sy,sy+1):
        if dy[j]:
            t[i+j]=1
            t[j-i]=1
    dy=t
if sx>=abs(x) and sy>=abs(y) and dx[x] and dy[y]:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
"""from collections import *
from itertools import *
from bisect import *
from heapq import *

import math
from fractions import gcd"""
S=input()
x,y=map(int,input().split())

Xlst=[]
Ylst=[]
count=0
flag=0
for s in S:
    if s=="F":
        count+=1
    else:
        if flag:
            if count:
                Ylst.append(count)
            flag=0
        else:
            if count:
                Xlst.append(count)
            flag=1
        count=0
if flag:
    if count:
        Ylst.append(count)
else:
    if count:
        Xlst.append(count)

if S[0]=="T":
    numx=set([0])
else:
    numx=set([Xlst[0]])
nlst=set()
c=0
for i in range(0 if 0 in numx else 1,len(Xlst)):
    X=Xlst[i]
    if c%2==0:
        while numx:
            a=numx.pop()
            nlst.add(a+X)
            nlst.add(a-X)
    else:
        while nlst:
            a=nlst.pop()
            numx.add(a+X)
            numx.add(a-X)
    c+=1
if x in numx|nlst:
    numx=set([0])
    nlst=set()
    c=0
    for X in Ylst:
        if c%2==0:
            while numx:
                a=numx.pop()
                nlst.add(a+X)
                nlst.add(a-X)
        else:
            while nlst:
                a=nlst.pop()
                numx.add(a+X)
                numx.add(a-X)
        c+=1
    if y in numx|nlst:
        print("Yes")
    else:
        print("No")
else:
    print("No")

Submission status: Accepted

Solution code:
order = list(map(len, input().split("T")))
x, y = map(int, input().split())
ans = "No"
lon = [order[i] for i in range(len(order)) if i % 2 == 0]
lat = [order[i] for i in range(len(order)) if i % 2 == 1]
x -= lon[0]
lon = lon[1:]
def f(x, A):
  A.append(abs(x))
  A.sort(reverse=True)
  k = A[0]
  for p in A[1:]:
      if k > 0:
        k -= p
      else:
        k += p
  if k == 0:
    return True
  else:
    return False
  
#print(lon, lat)
if f(x, lon) == f(y, lat) == True:
  ans = "Yes"
  
print(ans)

Submission status: Accepted

Solution code:
s = input().split('T')
X, Y = map(int, input().split())

x = [len(i) for i in s[0::2]]
y = [len(i) for i in s[1::2]]

X = X - x[0]

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

while x:
    mx = x.pop()
    if X < 0:
        X += mx
    else:
        X -= mx

while y:
    my = y.pop()
    if Y < 0:
        Y += my
    else:
        Y -= my

print('Yes' if X == 0 and Y == 0 else 'No')

Submission status: Accepted

Solution code:
s = input()
x, y = map(int, input().split())
n = len(s)
S = [[], []]
d = 0
f = 0
for i in range(n):
  if s[i] == 'T':
    if f != 0:
      S[d].append(f)
    d += 1
    d %= 2
    f = 0
  elif s[i] == 'F':
    f += 1
if s[n-1] == 'F':
  S[d].append(f)
if S[0] == []:
  S[0].append(0)
if S[1] == []:
  S[1].append(0)
px, py = 0, 0
if s[0] == 'F':
  px += S[0][0]
  S[0] = S[0][1:]
for i in range(len(S[0])):
  px -= S[0][i]
  S[0][i] *= 2
for i in range(len(S[1])):
  py -= S[1][i]
  S[1][i] *= 2
mpx, mpy = x - px, y - py
X = S[0][:]
Y = S[1][:]
Dx = {0: 1}
Dy = {0: 1}
for i in X:
  L = [i for i in Dx]
  for j in L:
    if i + j <= mpx and i + j not in Dx:
      Dx[i+j] = 1
for i in Y:
  L = [i for i in Dy]
  for j in L:
    if i + j <= mpy and i + j not in Dy:
      Dy[i+j] = 1

if mpx in Dx and mpy in Dy:
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
s = input()[: -1]
t = s.split("T")
x, y = map(int, input().split())
x -= t[0].count("F")
xm = t[: : 2]
ym = t[1: : 2]
xl = 0
yl = 0
if "F" in t[0]: xm = xm[1: ]
for i in range(len(xm)): xl += len(xm[i])
for i in range(len(ym)): yl += len(ym[i])
if xl < abs(x) or (yl < abs(y)):
  print("No")
  exit(0)
#print(xm, ym)
dp = [[0] * (xl * 2 + 1) for _ in range(len(xm) + 1)]
dp[0][xl] = 1
for i in range(len(xm)):
  for j in range(xl * 2 + 1):
    z = len(xm[i])
    if (j + z) in range(xl * 2 + 1):
      dp[i + 1][j + z] |= dp[i][j]
    if (j - z) in range(xl * 2 + 1):
      dp[i + 1][j - z] |= dp[i][j]
if dp[-1][xl + x] == 0:
  print("No")
  exit(0)
dp = [[0] * (yl * 2 + 1) for _ in range(len(ym) + 1)]
dp[0][yl] = 1
for i in range(len(ym)):
  for j in range(yl * 2 + 1):
    z = len(ym[i])
    if (j + z) in range(yl * 2 + 1):
      dp[i + 1][j + z] |= dp[i][j]
    if (j - z) in range(yl * 2 + 1):
      dp[i + 1][j - z] |= dp[i][j]
if dp[-1][yl + y] == 0:
  print("No")
  exit(0)
print("Yes")

Submission status: Accepted

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

S = S.split("T")
X_move = [len(s) for s in S[0::2]]
Y_move = [len(s) for s in S[1::2]]

x -= X_move[0]
X_move = sorted(X_move[1:])
Y_move = sorted(Y_move)

while X_move:
    mx = X_move.pop()
    if x >= 0:
        x -= mx
    else:
        x += mx

while Y_move:
    my = Y_move.pop()
    if y >= 0:
        y -= my
    else:
        y += my

if x == 0 and y == 0:
    ans = "Yes"
else:
    ans = "No"

print(ans)

Submission status: Accepted

Solution code:
def main():
    order = [len(el) for el in list(map(str, input().rstrip().split("T")))]
    x_points = {order[0]}
    y_points = {0}
    for val in order[2::2]:
        plus_val = {x + val for x in list(x_points)}
        minus_val = {x - val for x in list(x_points)}
        x_points = plus_val | minus_val
    for val in order[1::2]:
        plus_val = {y + val for y in list(y_points)}
        minus_val = {y - val for y in list(y_points)}
        y_points = plus_val | minus_val
    target_x, target_y = map(int, input().split())
    if target_x in x_points and target_y in y_points:
        print("Yes")
    else:
        print("No")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s = input()
l = len(s)
s = 'F' + s
x,y = map(int,input().split())
raw = [1]
for i in range(1,l+1):
  if s[i] == 'F':
    if s[i-1] == 'F':
      raw[-1] += 1
    else:
      raw.append(1)
  if s[i] == 'T':
    if s[i-1] == 'T':
      raw.append(0)
l2 = len(raw)
a = []
b = []
for i in range(l2):
  if i == 0:
    x -= raw[i]-1
  elif i % 2 == 0:
    a.append(raw[i])
  else:
    b.append(raw[i])
#print(raw)
#print(a,x)
#print(b,y)
nowa = {0}
for aa in a:
  lasta = nowa
  nowa = set()
  for aaa in lasta:
    nowa.add(aaa+aa)
    nowa.add(aaa-aa)
nowb = {0}
for bb in b:
  lastb = nowb
  nowb = set()
  for bbb in lastb:
    nowb.add(bbb+bb)
    nowb.add(bbb-bb)
#print(nowa,nowb)
#print(x,y)
if x in nowa and y in nowb:
  print('Yes')
else:
  print('No')

Submission status: Accepted

Solution code:
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
mod=10**9+7

s=input()+"T"
N=len(s)
x,y=MI()

F=[[],[]]

temp=0
bit=0
for i in range(N):
    if s[i]=="F":
        temp+=1
    else:
        if temp!=0:
            F[bit].append(temp)
        bit=(bit+1)%2
        temp=0
        
XY=[set([0]), set([0])]
    
for bit in range(2):
    for i in range(len(F[bit])):
        temp=set([])
        a=F[bit][i]
        b=-a
        if bit==0 and i==0 and s[0]=="F":
            b=a
        for j in range(len(XY[bit])):
            aaa=XY[bit].pop()
            temp.add(aaa+a)
            temp.add(aaa+b)
        XY[bit]=temp
        
if (x in XY[0] ) and (y in XY[1] ):
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    s = input().strip("\n")
    s += "T"
    lens = len(s)
    x, y = map(int, input().split())
    direc = 0 #0はx軸方向、１はy軸方向への移動
    move = 0
    init = True
    xPossible = {0}
    yPossible = {0}
    for i in range(lens):
        if s[i] == "F": move += 1
        else:
            if direc == 0: 
                newSet = set()
                for item in xPossible:
                    if init:
                        newSet |= {item + move}
                    else:
                        newSet |= {item + move}
                        newSet |= {item - move}
                xPossible = newSet
                #print("X:", xPossible)
                
            else: 
                newSet = set()
                for item in yPossible:
                    newSet |= {item + move}
                    newSet |= {item - move}
                yPossible = newSet
                #print("Y:", yPossible)
            move = 0
            direc = (direc + 1) % 2
            if init: init = False

    if x in xPossible and y in yPossible: print("Yes")
    else: print("No")

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
s = input()
x,y = map(int,input().split())
#%%
mx = []
my = []
mx2 = []
my2 = []


cnt = 0
it = 0
firstFlag = (s[0]=='F')
for t in s:
    if t == 'T':
        
        
        if cnt:
            if it%2==0:
                mx.append(cnt)
                mx2.append(cnt*2)
            else:
                my.append(cnt)
                my2.append(cnt*2)
        it += 1
        cnt = 0
    else:
        cnt += 1
    
if it%2==0:
    mx.append(cnt)
    mx2.append(cnt*2)
else:
    my.append(cnt)
    my2.append(cnt*2)

if firstFlag:
    x -= mx[0]
    mx[0] = 0
    mx2[0] = 0
        
dx = sum(mx)-x
dy = sum(my)-y


# dp[i+1][j] : i個目までの要素の和でjを作れるか？

MAX = max(sum(mx2),sum(my2)) + 10
dpx = [[False]*MAX for _ in range(len(mx2)+1)]
dpy = [[False]*MAX for _ in range(len(my2)+1)]

dpx[0][0] = True
dpy[0][0] = True

for i in range(len(mx2)):
    for j in range(MAX):
        if j<mx2[i]:
            dpx[i+1][j] = dpx[i][j]
        else:
            dpx[i+1][j] = dpx[i][j-mx2[i]] or dpx[i][j]

for i in range(len(my2)):
    for j in range(MAX):
        if j<my2[i]:
            dpy[i+1][j] = dpy[i][j]
        else:
            dpy[i+1][j] = dpy[i][j-my2[i]] or dpy[i][j]



if dx<0 or dy<0:
    print('No')

elif dx>sum(mx2) or dy>sum(my2):
    print('No')

else:
    if dpx[len(mx2)][dx] and dpy[len(my2)][dy]:
        print('Yes')
    else:
        print('No')

Submission status: Accepted

Solution code:
def get_move(S):
    S = S.split("T")
    x_move = [len(x) for x in S[0::2]]
    y_move = [len(x) for x in S[1::2]]
    return x_move, y_move


def check(x, move):
    move.sort()
    while move:
        m = move.pop()
        if x >= 0:
            x -= m
        else:
            x += m
    return x == 0


if __name__ == "__main__":
    S = input()
    x, y = map(int, input().split())

    x_move, y_move = get_move(S)
    # 初手正方向に移動
    x -= x_move[0]

    if check(x, x_move[1:]) and check(y, y_move):
        ans = "Yes"
    else:
        ans = "No"

    print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline

######################################################

s = input().split('T')
x, y = [int(_) for _ in input().split()]


def m(dp, first_idx):
    for i in range(first_idx, len(s), 2):
        step = len(s[i])

        next_dp = set()
        for item in dp:
            next_dp.add(item + step)
            next_dp.add(item - step)
        dp = next_dp

    return dp


if len(s) > 0:
    xs = {len(s[0])}
    xs = m(xs, 2)
else:
    xs = {0}
if len(s) > 1:
    ys = m({0}, 1)
else:
    ys = {0}

if x in xs and y in ys:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
def solve():
    S = input()
    X,Y = map(int, input().split())
    
    S_F = S.split('T')
    move_x = list(map(len, S_F[::2]))
    move_y = list(map(len,S_F[1::2]))
    
    if move_x:
        X -= move_x[0]
        move_x = move_x[1:]
    
    # たどりつけない場合
    if sum(move_x) < abs(X) or sum(move_y) < abs(Y):
        print('No')
        return
    
 
    move_x_process = [mx for mx in move_x if mx > 0]
    move_y_process = [my for my in move_y if my > 0]
    X = abs(X); Y = abs(Y)
    len_move_x = len(move_x_process)
    sum_x = sum(move_x_process)
    len_move_y = len(move_y_process)
    sum_y = sum(move_y_process)
 
    dp_table = [[False for _ in range(sum_x+1)] for _ in range(len_move_x+1)]
    dp_table[0][0] = True
    
    for i in range(0,len_move_x):
        for j in range(0, sum_x+1):
            dp_table[i+1][j] = dp_table[i][abs(j-move_x_process[i])]
            if j+move_x_process[i] <= sum_x: 
                dp_table[i+1][j] = dp_table[i+1][j] or dp_table[i][j+move_x_process[i]]
    
    # print(dp_table)
    ret = dp_table[len_move_x][X]
    dp_table = [[False for _ in range(sum_y+1)] for _ in range(len_move_y+1)]
    dp_table[0][0] = True
    for i in range(0,len_move_y):
        for j in range(0, sum_y+1):
            # if j-move_x_process[i] >= 0:
            dp_table[i+1][j] = dp_table[i][abs(j-move_y_process[i])]
            if j+move_y_process[i] <= sum_y:
                dp_table[i+1][j] = dp_table[i+1][j] or dp_table[i][j+move_y_process[i]]
    
    ret = ret and dp_table[len_move_y][Y]
    print('Yes' if ret else 'No')
    
solve()

Submission status: Accepted

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

S = list(map(len, s.split("T")))
Sx = []
Sy = []

for i in range(len(S)):
  if i % 2 == 0:
    Sx.append(S[i])
  else:
    Sy.append(S[i])

maxX = sum(Sx)
maxY = sum(Sy)

setX = set([maxX])
flg = True
for i in Sx:
  if flg:
    #初手だけはXの正方向にしかすすめない
    flg = False
    continue
  li = list(setX)
  for a in li:
    setX.add(a - 2*i)
    
setY = set([maxY])
for i in Sy:
  li = list(setY)
  for a in li:
    setY.add(a - 2*i)
    
if x in setX and y in setY:
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
import numpy as np

s = input() + "T"
x, y = map(int, input().split())
A = []
B = []
now = 0
di = 1
for i in s:
  if i=="T":
    if di:
      A.append(now)
    else:
      B.append(now)
    now = 0
    di ^= 1
  else:
    now += 1

def test(L, dist):
  dist = abs(dist)
  su = sum(L)
  l = len(L)
  if su < dist or (su-dist)%2:
    return False
  can = (su-dist)//2
  dp = np.zeros(can+1, dtype=bool)
  dp[0] = True
  for i in L:
    if i <= can:
      dp[i:] |= dp[:can+1-i]
  return dp[-1]

if s[0] == "F":
  a = test(A[1:], x-A[0])
else:
  a = test(A, x)
b = test(B, y)
if a and b:
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
s = input()
x,y = map(int,input().split())
l = []
cnt = 0
for i in s:
    if i=='T':
        l.append(cnt)
        cnt = 0
    else:
        cnt+=1
l.append(cnt)
horizontal = l[2::2]
vartical = l[1::2]
horizontal.sort()
horizontal = horizontal[::-1]
vartical.sort()
vartical = vartical[::-1]
x-=l[0]
now_x = 0
now_y = 0
for i in horizontal:
    if now_x<x:
        now_x+=i
    else:
        now_x-=i
for i in vartical:
    if now_y<y:
        now_y+=i
    else:
        now_y-=i
if now_x==x and now_y==y:
    print('Yes')
else:
    print('No')

Submission status: Accepted

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

def main():
    s = input().split("T")
    x,y = map(int,input().split())
    l = len(s)
    dx,dy = [len(s[0])],[0]
    nx,ny = [],[]
    for i in range(1,l):
        d = len(s[i])
        if i%2 == 0:
            nx = []
            for num in dx:
                nx.append(num+d)
                nx.append(num-d)
            dx = list(set(nx))
        else:
            ny = []
            for num in dy:
                ny.append(num+d)
                ny.append(num-d)
            dy = list(set(ny))

    if (x in dx and y in dy):
        print("Yes")
    else:
        print("No")
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s = input().split('T')
dx = list(map(len, s[0::2]))
dy = list(map(len, s[1::2]))
x, y = map(int, input().split())

def check(start, ds, goal):
    cands = {start}
    for d in ds:
        new_cands = set()
        for c in cands:
            new_cands.add(c - d)
            new_cands.add(c + d)
        cands = new_cands
    return goal in cands

x_ok = check(dx[0], dx[1:], x)
y_ok = check(0, dy, y)

print('Yes' if x_ok and y_ok else 'No')

Submission status: Accepted

Solution code:
def f(start, goal, steps):
  res = set([start])
  for s in steps:
    next_res = set()
    for r in res:
      next_res.add(r + s)
      next_res.add(r - s)
    res = next_res
  return goal in res

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

ss = s.split("T")
xmove = [i.count("F") for i in ss[::2]]
xfirst = xmove[0] # xの１回目はプラス方向固定のため切り出す
xmove = xmove[1:] # ２回目以降でリスト再作成
ymove = [i.count("F") for i in ss[1::2]]

if f(xfirst, x, xmove) and f(0, y, ymove):
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
mod=pow(10,9)+7

s=input()
x,y=map(int,input().split())
lst=[[],[]]
cnt=0
tmp=0
for i in range(len(s)):
    if s[i]=="F":
        tmp+=1
    else:
        if cnt==0:
            x-=tmp
        else:
            if tmp!=0:
                lst[cnt%2].append(tmp)
        tmp=0
        cnt+=1
if cnt==0:
    x-=tmp
elif tmp!=0:
    lst[cnt%2].append(tmp)

sumx=sum(lst[0])
sumy=sum(lst[1])
# print(x,sumx)
if sumx<x or x<-sumx:
    print("No")
    exit()
if sumy<y or y<-sumy:
    print("No")
    exit()
dpx=[[0]*(sumx*2+1) for i in range(len(lst[0])+1)]
dpy=[[0]*(sumy*2+1) for i in range(len(lst[1])+1)]
dpx[0][sumx]=1
dpy[0][sumy]=1

for i in range(len(lst[0])):
    for j in range(sumx*2+1):
        if dpx[i][j]!=0:
            dpx[i+1][j+lst[0][i]]+=1
            dpx[i+1][j-lst[0][i]]+=1

for i in range(len(lst[1])):
    for j in range(sumy*2+1):
        if dpy[i][j]!=0:
            dpy[i+1][j+lst[1][i]]+=1
            dpy[i+1][j-lst[1][i]]+=1

# print(dpx)
# print(dpy)
if dpx[len(lst[0])][sumx+x]!=0 and dpy[len(lst[1])][sumy+y]!=0:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
S = input()
X, Y = map(int, input().split())

A = [0]
for s in S:
    if s == 'T':
        A.append(0)
    else:
        A[-1] += 1

B = [n for n in A[1:: 2]]
X -= A[0]
A = [n for n in A[2:: 2]]

def canGo(nums, g):
    if not nums:
        return g == 0

    bias = sum(nums) + 10
    g += bias
    if g < 0:
        return False

    dp = (1 << bias)

    for n in nums:
        dp = (dp << n) | (dp >> n)
    return (dp & (1 << g)) > 0

if canGo(A, X) and canGo(B, Y):
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
S = input()
X, Y = map(int, input().split())

def run_length(s) -> list:
    res = [[s[0], 1]]
    for i in range(1, len(s)):
        if res[-1][0] == s[i]:
            res[-1][1] += 1
        else:
            res.append([s[i], 1])
    return res

RL = run_length(S)
if RL[0][0] == 'T':
    RL = [['F', 0]] + RL

SIZE = len(S)
nx = 1 << SIZE
ny = 1 << SIZE

x_flag = 1
for i in range(0, len(RL), 2):
    s, l = RL[i]
    if i == 0:
        nx = nx << l
    else:
        x_flag = (x_flag + RL[i - 1][1]) % 2
        if x_flag:
            nx = (nx << l) | (nx >> l)
        else:
            ny = (ny << l) | (ny >> l)

if ((nx >> (SIZE + X)) & 1) & ((ny >> (SIZE + Y)) & 1):
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

def foo(fe,x,ev):
    k = sum(fe)+1
    n = len(fe)
    d = [ [False]*(2*k+1) for i in range(n+1) ]
    d[0][k] = True
    if len(fe)>0:
        d[1][fe[0]+k] = True
        if not ev:
            d[1][-fe[0]+k] = True
    for i in range(1,n):
        for j in range(-k,k+1):
            if d[i][j+k]:
                d[i+1][j+fe[i]+k] = True
                d[i+1][j-fe[i]+k] = True
    return -k<=x<=k and d[n][x+k]

s = ins()
x,y = inm()
f = [ [], [] ]
strk = 0
turn = 0
n = len(s)
for i in range(n):
    if s[i]=='T':
        f[turn].append(strk)
        turn = 1 - turn
        strk = 0
    else:
        strk += 1
f[turn].append(strk)
if foo(f[0],x,True) and foo(f[1],y,False):
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
def check(arr, x):
    pos = set([0])
    for dx in arr:
        npos = set()
        for p in pos:
            npos.add(p+dx)
            npos.add(p-dx)
        pos = npos
    return x in pos


s = input().split('T')
x, y = map(int, input().split())
d = [len(ss) for ss in s]
dx = d[2::2]
dy = d[1::2]
x -= d[0]
if check(dx, x) and check(dy, y):
    print('Yes')
else:
    print('No')