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:
l = [len(c) for c in input().split("T")]
x, y = map(int, input().split())
bx = 1 << l.pop(0) + 8000
by = 1 << 8000
for i, d in enumerate(l):
	if i % 2:
		bx = (bx << d) | (bx >> d)
	else:
		by = (by << d) | (by >> d)
print("Yes" if bx & (1 << x + 8000) and by & (1 << y + 8000) else "No")

Submission status: Accepted

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

S = input().rstrip()
gx, gy = map(int, input().split())
L = len(S)

XY = [[], []]
d = 0
c = 0
begin = True
for s in S:
    if begin and s == "F":
        gx -= 1
    elif s == 'T':
        begin = False
        if c > 0:
            XY[d].append(c)
        d ^= 1
        c = 0
    else:
        c += 1
if c > 0:
    XY[d].append(c)

XY[0].append(abs(gx))
XY[1].append(abs(gy))

ans = True
for X in XY:
    G = sum(X)
    if G%2 != 0:
        ans = False
        break
    G //= 2
    ok = False
    W = len(X)
    dp = [False]*(G+1)
    dp[0] = True
    for i, x in enumerate(X):
        for g in reversed(range(G+1)):
            if g >= x:
                dp[g] = dp[g-x] or dp[g]
        if dp[G]:
            ok = True
            break
    ans = ok and ans
print("Yes" if ans else "No")

Submission status: Accepted

Solution code:
# D - FT Robot

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

f = [len(x) for x in s.split("T")]
x -= f[0]
fx = f[2::2]
fy = f[1::2]

x_possible_set = set([0])
for dx in fx:
    x_possible_set = set([xi + dx for xi in x_possible_set] + [xi - dx for xi in x_possible_set])

y_possible_set = set([0])
for dy in fy:
    y_possible_set = set([yi + dy for yi in y_possible_set] + [yi - dy for yi in y_possible_set])

print("Yes" if x in x_possible_set and y in y_possible_set else "No")

Submission status: Accepted

Solution code:
# D - FT Robot

import copy

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

f = [len(x) for x in s.split("T")]
x -= f[0]
fx = f[2::2]
fy = f[1::2]

x_possible_set = set([0])
for dx in fx:
    tmp = set()
    for current_x in x_possible_set:
        tmp.add(current_x + dx)
        tmp.add(current_x - dx)
    x_possible_set = tmp.copy()

y_possible_set = set([0])
for dy in fy:
    tmp = set()
    for current_y in y_possible_set:
        tmp.add(current_y + dy)
        tmp.add(current_y - dy)
    y_possible_set = tmp.copy()

print("Yes" if x in x_possible_set and y in y_possible_set else "No")

Submission status: Accepted

Solution code:
import itertools
S=str(input())
x,y=map(int,input().split())
Move=[len(s) for s in S.split('T')]#偶数番目がｘ軸の移動量、奇数番目がy軸の移動量
x -=Move[0]#初手のみ正の方向
Move_x=Move[2::2]
Move_y=Move[1::2]

def check(d,x):
  se =set([0])
  for dx in d:
    se={a+b for a,b in itertools.product(se,[-dx,dx])}
  return (x in se)

print('Yes' if check(Move_x,x) and check(Move_y,y) else 'No')

Submission status: Accepted

Solution code:
import sys
import re
s = input()
x, y = [int(i) for i in sys.stdin.readline().split()]
s = [len(i) for i in re.split("T", s)]
v_ls = s[1::2]
h_ls = s[::2]
x -= h_ls[0]
h_ls.pop(0)
v_sum = sum(v_ls)
h_sum = sum(h_ls)
# memo_v_ls[i][j] : i番目まででj - v_sumができるか
width = max(abs(y), v_sum)
memo_v_ls = [[0 for i in range(width * 2 + 1)] for j in range(len(v_ls) + 1)]
memo_v_ls[0][width] = 1
for j, v in enumerate(v_ls, 1):
    for i in range(width * 2 + 1):
        if i >= v and i <= 2 * width - v:
            memo_v_ls[j][i] = max(memo_v_ls[j - 1][i - v], memo_v_ls[j - 1][i + v])
        elif i >= v:
            memo_v_ls[j][i] = memo_v_ls[j - 1][i - v]
        else:
            memo_v_ls[j][i] = memo_v_ls[j - 1][i + v]

flg = memo_v_ls[-1][y + width] == 1

width = max(abs(x), h_sum)
memo_h_ls = [[0 for i in range(width * 2 + 1)] for j in range(len(h_ls) + 1)]
memo_h_ls[0][width] = 1
for j, h in enumerate(h_ls, 1):
    for i in range(width * 2 + 1):
        if i >= h and i <= 2 * width - h:
            memo_h_ls[j][i] = max(memo_h_ls[j - 1][i - h], memo_h_ls[j - 1][i + h])
        elif i >= h:
            memo_h_ls[j][i] = memo_h_ls[j - 1][i - h]
        else:
            memo_h_ls[j][i] = memo_h_ls[j - 1][i + h]

flg &= memo_h_ls[-1][x + width] == 1
if flg:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
sl = list(input())
x,y = map(int,input().split())
lrl = [[],[]]
mode = 0
stack = 0
flag = True
first_t = 0
for s in sl:
    if s == "F":
        stack += 1
    else:
        if flag:
            first_t = stack
            flag = False
        elif stack > 0:
            lrl[mode%2].append(stack)
        mode += 1
        stack = 0
else:
    if flag:
        first_t = stack
        flag = False
    elif stack > 0:
        lrl[mode%2].append(stack)

x = abs(x - first_t)
y = abs(y)
from collections import defaultdict
l = defaultdict(int)
r = defaultdict(int)
l[0] = 1
r[0] = 1
ol = x + sum(lrl[0])
for a in lrl[0]:
    kouho = []
    for ll in l:
        if ll + 2 * a <= ol:
            kouho.append(ll+2*a)
    for i in kouho:
        l[i] = 1

orr = y + sum(lrl[1])
for a in lrl[1]:
    kouho = []
    for rr in r:
        if rr + 2 * a <= orr:
            kouho.append(rr+2*a)
    for i in kouho:
        r[i] = 1


if l[ol] == 1 and r[orr] == 1:
    print("Yes")
    import sys
    sys.exit()

print("No")

Submission status: Accepted

Solution code:
s=[len(c) for c in list(input().split("T"))]
x,y=map(int,input().split())
XX=s[::2]
YY=s[1::2]
X=set([XX[0]])
Y=set([0])
XXX=[]
for i in range(1,len(XX)):
    for j in X:
        XXX.append(j+XX[i])
        XXX.append(j-XX[i])
    X=set(XXX)
    XXX=[]

YYY=[]
for i in range(len(YY)):
    for j in Y:
        YYY.append(j+YY[i])
        YYY.append(j-YY[i])
    Y=set(YYY)
    YYY=[]

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

Submission status: Accepted

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

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

def solve(s: str, x: int, y: int):
    s_list = list(map(len,s.split("T")))
    x_s_list = s_list[0::2]
    y_s_list = s_list[1::2]

    x_dp = [set() for _ in range(len(x_s_list))]
    for index,count in enumerate(x_s_list):
        ## 最初は
        if index == 0:
            x_dp[index].add(count)
            continue
        
        for j in x_dp[index-1]:
            x_dp[index].add(j-count)
            x_dp[index].add(j+count)


    y_dp = [set() for _ in range(len(y_s_list))]
    for index,count in enumerate(y_s_list):
        ## 最初は
        if index == 0:
            y_dp[index].add(count)
            y_dp[index].add(-count)  
            continue
        
        for j in y_dp[index-1]:
            y_dp[index].add(j-count)
            y_dp[index].add(j+count)
    
    if not y_dp:
        if y != 0:
            print(NO)
        else:
            if x in x_dp[-1]:
                print(YES)
            else:
                print(NO)
        return

    if not x_dp:
        if x != 0:
            print(NO)
        else:
            if y in y_dp[-1]:
                print(YES)
            else:
                print(NO)
        return

    if x in x_dp[-1] and y in y_dp[-1]:
        print(YES)
    else:
        print(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: Accepted

Solution code:
s = [len(x) for x in input().split('T')]
x, y = map(int, input().split())

X = set([s[0]])
Y = set([0])

for dx in s[2::2]:
    tX = set()
    for Xi in X:
        tX.add(Xi-dx)
        tX.add(Xi+dx)
    X = tX

for dy in s[1::2]:
    tY = set()
    for Yi in Y:
        tY.add(Yi-dy)
        tY.add(Yi+dy)
    Y = tY

if x in X and y in Y:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
s = input()
x, y = list(map(int, input().split()))
F = [len(fs) for fs in s.split('T')]
x -= F[0]
Fx = F[2::2]
Fy = F[1::2]

xt2 = sum(Fx)-x
yt2 = sum(Fy)-y
if xt2%2==1 or yt2%2==1 or xt2<0 or yt2<0:
    print('No')
    exit()

xt = xt2//2
yt = yt2//2

dpx = [False]*(xt+1)
dpx[0] = True
for f in Fx:
    for i in range(xt, f-1, -1): dpx[i] |= dpx[i-f]

dpy = [False]*(yt+1)
dpy[0] = True
for f in Fy:
    for i in range(yt, f-1, -1): dpy[i] |= dpy[i-f]

print('Yes' if dpx[xt] and dpy[yt] else 'No')

Submission status: Accepted

Solution code:

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

    lf = [0]
    tf = 0
    for i in range(len(s)):
        if s[i] =="F":
            lf[tf] += 1
        else:
            lf.append(0)
            tf += 1

    lx = [lf[i] for i in range(len(lf)) if i % 2 == 0]
    ly = [lf[i] for i in range(len(lf)) if i % 2 == 1]
    dpxcenter = sum(lx)
    if abs(x)>dpxcenter: return "No"
    dpxedge = dpxcenter*2+20
    dpx = [[False for _ in range(dpxedge)] for _ in range(len(lx)+1)]
    dpx[1][dpxcenter+lx[0]+10] = True
    for i in range(1,len(lx)):
        for j in range(dpxedge):
            if j-lx[i] >= 0:
                dpx[i+1][j] = dpx[i][j-lx[i]]
            if j+lx[i] < dpxedge:
                dpx[i+1][j] = dpx[i+1][j] or dpx[i][j+lx[i]]

    dpycenter = sum(ly)
    if abs(y)>dpycenter: return "No"
    dpyedge = dpycenter*2+20
    dpy = [[False for _ in range(dpyedge)] for _ in range(len(ly)+1)]
    dpy[0][dpycenter+10] = True
    for i in range(len(ly)):
        for j in range(dpyedge):
            if j-ly[i] >= 0:
                dpy[i+1][j] = dpy[i][j-ly[i]]
            if j+ly[i] < dpyedge:
                dpy[i+1][j] = dpy[i+1][j] or dpy[i][j+ly[i]]

    if dpx[-1][x+dpxcenter+10] and dpy[-1][y+dpycenter+10]:return "Yes"
    else:return "No"

print(main())

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()

from itertools import groupby
def resolve():
    S=input()
    X=[]; Y=[]
    dir=0
    for key,iter in groupby(S):
        h=len(list(iter))
        if(key=='T'): dir^=(h&1)
        else:
            if(dir&1==0): X.append(h)
            else: Y.append(h)

    # 最初が移動のときだけは自由度が無いため例外処理
    tx,ty=map(int,input().split())
    if(S[0]=='F'):
        tx-=X[0]
        del X[0]

    # あとはx,y独立に移動できるので、tx、tyが到達可能かを判定
    M=8100*2
    dpx=[0]*M; dpx[0]=1
    dpy=[0]*M; dpy[0]=1

    for x in X:
        ndpx=[0]*M
        for i in range(M):
            if(dpx[i]==0): continue
            ndpx[(i+x)%M]=1
            ndpx[(i-x)%M]=1
        dpx=ndpx

    for y in Y:
        ndpy=[0]*M
        for i in range(M):
            if(dpy[i]==0): continue
            ndpy[(i+y)%M]=1
            ndpy[(i-y)%M]=1
        dpy=ndpy

    print("Yes" if(dpx[tx%M] and dpy[ty%M]) else "No")
resolve()

Submission status: Accepted

Solution code:
def examD():
    St = S(); X, Y = LI()
    N = len(St)
    ans = "No"
    Fx = deque(); Fy = deque()
    ve = int(1); cur = int(0)
    for i in range(N):
        if St[i] == "F":
            cur += 1
        else:
            if ve == 1:
                Fx.append(cur)
                cur = int(0)
                ve *= -1
            else:
                Fy.append(cur)
                cur = int(0)
                ve *= -1
    if ve == 1:
        Fx.append(cur)
    else:
        Fy.append(cur)
    xfirst = Fx.popleft()
    X = X - xfirst
#    print(Fx)
#    print(Fy)
    xlen = len(Fx); ylen = len(Fy)
    xneed = -1; yneed = -1
    if (sum(Fx) - X) % 2 == 0:
        xneed = (sum(Fx) - X) // 2
    if (sum(Fy) - Y) % 2 == 0:
        yneed = (sum(Fy) - Y) // 2
#    print(xneed)
    dp = [[0] * (xneed + 1) for _ in range(xlen + 1)]
    if xneed > 0:
        dp[0][0] = 1
        for i in range(xlen):
            for j in range(max(xneed + 1, 0)):
                if dp[i][j] != 0:
                    if xneed + 1 - Fx[i] > j:
                        dp[i + 1][j + Fx[i]] = dp[i][j]
                    dp[i + 1][j] = dp[i][j]
    #    print(dp)
    #    print(dp[xlen][xneed])
    if (xneed > 0 and dp[xlen][xneed] == 1) or xneed == 0:
        dp = [[0] * (yneed + 1) for _ in range(ylen + 1)]
        if yneed>0:
            dp[0][0] = 1
            for i in range(ylen):
                for j in range(max(yneed + 1, 0)):
                    if dp[i][j] != 0:
                        if yneed + 1 - Fy[i] > j:
                            dp[i + 1][j + Fy[i]] = dp[i][j]
                        dp[i + 1][j] = dp[i][j]
        if (yneed > 0 and dp[ylen][yneed] == 1) or yneed == 0:
            ans = "Yes"
    print(ans)

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD()

Submission status: Accepted

Solution code:
from collections import defaultdict

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

arr = list(map(len, s.split('T')))
EW = [arr[ew] for ew in range(0, len(arr), 2)]
NS = [arr[ns] for ns in range(1, len(arr), 2)]

# dp[i][j] := i回目までの移動で座標jにいることができるか
dp = [defaultdict(bool) for i in range(len(EW))]
dp[0][EW[0]] = True
for i in range(1, len(EW)):
    for prev in dp[i - 1].keys():
        dp[i][prev + EW[i]] = True
        dp[i][prev - EW[i]] = True
if not dp[-1][x]:
    print('No')
    exit(0)

if len(NS) > 0:
    dp = [defaultdict(bool) for i in range(len(NS))]
    dp[0][NS[0]] = True
    dp[0][-NS[0]] = True
    for i in range(1, len(NS)):
        for prev in dp[i - 1].keys():
            dp[i][prev + NS[i]] = True
            dp[i][prev - NS[i]] = True
    if not dp[-1][y]:
        print('No')
        exit(0)

print('Yes')

Submission status: Accepted

Solution code:
import numpy as np
s = list(input())
x, y = map(int, input().split())

X = [0]
Y = [0]

X_append = X.append
Y_append = Y.append

flag = True
tmp = 0
for i in s:
    if i == 'F':
        tmp += 1
    else:
        if flag:
            X_append(tmp)
        else:
            Y_append(tmp)
        tmp = 0
        flag = not flag

if flag:
    X_append(tmp)
else:
    Y_append(tmp)

x -= X[1]
X[1] = 0

x = abs(x)
total_X = sum(X)

lst_X = np.zeros(total_X + 1, dtype = np.int32)
lst_X[0] = 1
for i in X:
    if i == 0:
        continue
    lst_X[i:] = np.maximum(lst_X[i:], lst_X[:-i])

if x > total_X or (total_X - x) % 2 == 1 or lst_X[(total_X - x) // 2] == 0:
    print ('No')
    exit()

y = abs(y)
total_Y = sum(Y)

lst_Y = np.zeros(total_Y + 1)
lst_Y[0] = 1
for i in Y:
    if i == 0:
        continue
    lst_Y[i:] = np.maximum(lst_Y[i:], lst_Y[:-i])

if  y <= total_Y and (total_Y - y) % 2 == 0 and int(lst_Y[(total_Y - y) // 2]) == 1:
    print ('Yes')
else:
    print ('No')

Submission status: Accepted

Solution code:
import numpy as np
s = list(input())
x, y = map(int, input().split())

X = [0]
Y = [0]

X_append = X.append
Y_append = Y.append

flag = True
tmp = 0
for i in s:
    if i == 'F':
        tmp += 1
    else:
        if flag:
            X_append(tmp)
        else:
            Y_append(tmp)
        tmp = 0
        flag = not flag

if flag:
    X_append(tmp)
else:
    Y_append(tmp)

x -= X[1]
X[1] = 0

x = abs(x)
total_X = sum(X)

lst_X = np.zeros(total_X + 1, dtype = np.int32)
lst_X[0] = 1
for i in X:
    if i == 0:
        continue
    lst_X[i:] = np.maximum(lst_X[i:], lst_X[:-i])

if x > total_X or (total_X - x) % 2 == 1 or lst_X[(total_X - x) // 2] == 0:
    print ('No')
    exit()

y = abs(y)
total_Y = sum(Y)

lst_Y = np.zeros(total_Y + 1)
lst_Y[0] = 1
for i in Y:
    if i == 0:
        continue
    lst_Y[i:] = np.maximum(lst_Y[i:], lst_Y[:-i])

if x > total_X or y > total_Y:
    print ('No')
    exit()

if  (total_Y - y) % 2 == 0 and int(lst_Y[(total_Y - y) // 2]) == 1:
    print ('Yes')
else:
    print ('No')

Submission status: Accepted

Solution code:
import numpy as np
s = list(input())
x, y = map(int, input().split())

X = [0]
Y = [0]

X_append = X.append
Y_append = Y.append

flag = True
tmp = 0
for i in s:
    if i == 'F':
        tmp += 1
    else:
        if flag:
            X_append(tmp)
        else:
            Y_append(tmp)
        tmp = 0
        flag = not flag

if flag:
    X_append(tmp)
else:
    Y_append(tmp)

# print (X)
# print (Y)

x -= X[1]
X[1] = 0

x = abs(x)
y = abs(y)

total_X = sum(X)
total_Y = sum(Y)

lst_X = np.zeros(total_X + 1, dtype = np.int32)
lst_X[0] = 1
for i in X:
    if i == 0:
        continue
    # print (lst_X)
    # print (lst_X[i:])
    # print (lst_X[:-i])
    # print (np.maximum(lst_X[i:], lst_X[:-i]))

    lst_X[i:] = np.maximum(lst_X[i:], lst_X[:-i])

# print (lst_X)

lst_Y = np.zeros(total_Y + 1)
lst_Y[0] = 1
for i in Y:
    if i == 0:
        continue
    lst_Y[i:] = np.maximum(lst_Y[i:], lst_Y[:-i])

if x > total_X or y > total_Y:
    print ('No')
    exit()

if (total_X - x) % 2 == (total_Y - y) % 2 == 0 and int(lst_X[(total_X - x) // 2]) == 1 and int(lst_Y[(total_Y - y) // 2]) == 1:
    print ('Yes')
else:
    print ('No')

Submission status: Accepted

Solution code:
import numpy as np
s = list(input())
x, y = map(int, input().split())

X = [0]
Y = [0]

flag = True
tmp = 0
for i in s:
    if i == 'F':
        tmp += 1
    else:
        if flag:
            X.append(tmp)
        else:
            Y.append(tmp)
        tmp = 0
        flag = not flag

if flag:
    X.append(tmp)
else:
    Y.append(tmp)

# print (X)
# print (Y)

x -= X[1]
X[1] = 0

x = abs(x)
y = abs(y)

total_X = sum(X)
total_Y = sum(Y)

lst_X = np.zeros(total_X + 1, dtype = np.int32)
lst_X[0] = 1
for i in X:
    if i == 0:
        continue
    # print (lst_X)
    # print (lst_X[i:])
    # print (lst_X[:-i])
    # print (np.maximum(lst_X[i:], lst_X[:-i]))

    lst_X[i:] = np.maximum(lst_X[i:], lst_X[:-i])

# print (lst_X)

lst_Y = np.zeros(total_Y + 1)
lst_Y[0] = 1
for i in Y:
    if i == 0:
        continue
    lst_Y[i:] = np.maximum(lst_Y[i:], lst_Y[:-i])

if x > total_X or y > total_Y:
    print ('No')
    exit()

if (total_X - x) % 2 == (total_Y - y) % 2 == 0 and int(lst_X[(total_X - x) // 2]) == 1 and int(lst_Y[(total_Y - y) // 2]) == 1:
    print ('Yes')
else:
    print ('No')

Submission status: Accepted

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

X = [0]
Y = [0]

flag = True
tmp = 0
for i in s:
    if i == 'F':
        tmp += 1
    else:
        if flag:
            X.append(tmp)
        else:
            Y.append(tmp)
        tmp = 0
        flag = not flag

if flag:
    X.append(tmp)
else:
    Y.append(tmp)

# print (X)
# print (Y)

x -= X[1]
X[1] = 0

x = abs(x)
y = abs(y)

total_X = sum(X)
total_Y = sum(Y)

lst_X = [0] * (total_X + 1)
lst_X[0] = 1
for i in X:
    if i == 0:
        continue
    for j in range(total_X - i, -1, -1):
        if lst_X[j] == 1:
            lst_X[j + i] = 1

# print (lst_X)

lst_Y = [0] * (total_Y + 1)
lst_Y[0] = 1
for i in Y:
    if i == 0:
        continue
    for j in range(total_Y - i, -1, -1):
        if lst_Y[j] == 1:
            lst_Y[j + i] = 1

# print (lst_Y)
if x > total_X or y > total_Y:
    print ('No')
    exit()

if (total_X - x) % 2 == (total_Y - y) % 2 == 0 and lst_X[(total_X - x) // 2] == 1 and lst_Y[(total_Y - y) // 2]:
    print ('Yes')
else:
    print ('No')

Submission status: Accepted

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

    # x軸方向の移動について
    # 1回目の移動はxの正方向のみ
    here_x = [len(s[0])]

    for i in s[2::2]:
        move = len(i)
        here_x = [i + move for i in here_x] + [i - move for i in here_x]
        here_x = set(here_x)

    # y軸方向の移動について
    here_y = [0]

    for i in s[1::2]:
        move = len(i)
        here_y = [i + move for i in here_y] + [i - move for i in here_y]
        here_y = set(here_y)

    if x in here_x and y in here_y:
        print('Yes')
    else:
        print('No')


if __name__ == '__main__':
    main()

Submission status: Accepted

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

# x軸方向の移動について
# 1回目の移動はxの正方向のみ
here_x = [len(s[0])]

for i in s[2::2]:
    move = len(i)
    here_x = [i + move for i in here_x] + [i - move for i in here_x]
    here_x = list(set(here_x))

# x軸方向の移動について
# 1回目の移動はxの正方向のみ
here_y = [0]

for i in s[1::2]:
    move = len(i)
    here_y = [i + move for i in here_y] + [i - move for i in here_y]
    here_y = list(set(here_y))

if x in here_x and y in here_y:
    print('Yes')
else:
    print('No')

Submission status: Accepted

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

s.append("last")
X = []
Y = []
f = "x"
cnt = 0
for i in range(len(s)):
    if s[i] == "F":
        cnt += 1
    elif f == "x":
        X.append(cnt)
        cnt = 0
        f = "y"
    else:
        Y.append(cnt)
        cnt = 0
        f = "x"

if X:
    x -= X[0]
    X.pop(0)

X.sort(reverse=True)
Y.sort(reverse=True)
for i in range(len(X)):
    if x >= 0:
        x -= X[i]
    else:
        x += X[i]
        
for i in range(len(Y)):
    if y >= 0:
        y -= Y[i]
    else:
        y += Y[i]
        
if x == 0 and y == 0:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

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

s = str(input())
x,y = II()

N = len(s)
yoko = []
tate = []
ziku = True
count = 0

for i in range(N):
    if s[i]=='F':
        count += 1
    else:
        if ziku:
            yoko.append(count)
            ziku = False
        else:
            tate.append(count)
            ziku = True
        count = 0

if count!=0:
    if ziku:
        yoko.append(count)
    else:
        tate.append(count)

yoko.append(0)
tate.append(0)

if s[0]=='F':
    fstart = True
else:
    fstart = False

sum1 = sum(yoko)
sum2 = sum(tate)

if abs(x)>sum1 or abs(y)>sum2:
    print('No')
    exit()

yoko_yes = [False]*(2*sum1+1)
yoko_yes[sum1] = True

for k,yk in enumerate(yoko):
    ok = []
    if k==0 and fstart:
        ok.append(sum1+yk)
    else:
        for i in range(2*sum1+1):
            if yoko_yes[i]:
                if 0<=i+yk<=2*sum1:
                    ok.append(i+yk)
                if 0<=i-yk<=2*sum1:
                    ok.append(i-yk)
    yoko_yes = [False]*(2*sum1+1)
    for o in ok:
        yoko_yes[o] = True

tate_yes = [False]*(2*sum2+1)
tate_yes[sum2] = True

for t in tate:
    ok = []
    for i in range(2*sum2+1):
        if tate_yes[i]:
            if 0<=i+t<=2*sum2:
                ok.append(i+t)
            if 0<=i-t<=2*sum2:
                ok.append(i-t)
    tate_yes = [False]*(2*sum2+1)
    for o in ok:
        tate_yes[o] = True

if yoko_yes[x+sum1] and tate_yes[y+sum2]:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
# https://ikatakos.com/pot/programming_algorithm/contest_history/atcoder/2017/1216_arc087
# https://pitsbuffersolution.com/compro/atcoder/arc087d.php

# 最初の移動以外は、どんな操作手順後であっても、正負いずれの方向にも動けるのがミソ

def solve(s, x, y):
    X = 0
    Y = 1

    def update(move):
        st = set()
        for p in reachable[st_ind]:
            st.add(p + move)
            st.add(abs(p - move))
        reachable[st_ind] = st
        # 絶対値の等しい正負の値を取りうるので、正だけ扱う

    n = len(s)
    spl = map(len, s.split('T'))
    x -= next(spl)
    # ここでxを減らすと、
    # 個々のmove後のx,yは、原点対象になるので、正の座標の到達点だけ扱えばよくなる

    reachable = [{0}, {0}]
    st_ind = 1
    for move in spl:
        update(move)
        st_ind ^= 1

    return (abs(x) in reachable[X]) and (abs(y) in reachable[Y])
    # x,yが負であっても、それに対応した正の値が集合に含まれていれば、到達可能


if __name__ == '__main__':
    s = input()
    x, y = map(int, input().split())
    print('Yes' if solve(s, x, y) else 'No')

Submission status: Accepted

Solution code:
import sys
s=input()
n=len(s)
x,y=map(int,input().split())
lst=list(s.split("T"))
if len(lst)==1:
    if x==len(s) and y==0:
        print("Yes")
    else:
        print("No")
    sys.exit()
v=[0]*(2*n+1)
u=[0]*(2*n+1)
a=len(lst[0])
b=len(lst[1])
v[n+a]=1
u[n+b]=1
u[n-b]=1
for i in range(2,len(lst)):
    l=len(lst[i])
    if i%2==0:
        h=[0]*(2*n+1)
        for i in range(2*n+1):
            if i-l>=0:
                if v[i-l]==1:
                    h[i]=1
            if i+l<=2*n:
                if v[i+l]==1:
                    h[i]=1
        v=h
    else:
        h=[0]*(2*n+1)
        for i in range(2*n+1):
            if i-l>=0:
                if u[i-l]==1:
                    h[i]=1
            if i+l<=2*n:
                if u[i+l]==1:
                    h[i]=1
        u=h
if v[n+x]==1 and u[n+y]==1:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
s=input()
x,y=map(int,input().split())
xx,yy=[],[]
w=[0,0]
ans=chk=f=0
for i in s:
    if f==1 and i=='F':
        chk+=1
    elif f==1 and i=='T':
        yy.append(chk)
        f=0
    elif f==0 and i=='F':
        chk+=1
    elif f==0 and i=='T':
        xx.append(chk)
        f=1
    if i=='T': chk=0
if chk and f==1:
    yy.append(chk)
elif chk and f==0:
    xx.append(chk)
if s[0]=='F':
    w[0]+=xx[0]
    xx[0]=0
xx.sort()
yy.sort()
for i in xx[::-1]:
    if w[0]<x:
        w[0]+=i
    else:
        w[0]-=i
for i in yy[::-1]:
    if w[1]<y:
        w[1]+=i
    else:
        w[1]-=i
print("Yes" if w[0]==x and w[1]==y else "No")

Submission status: Accepted

Solution code:
s=input()
x,y=map(int,input().split())
xx,yy=[],[]
w=[0,0]
ans=chk=0
f=0
for i in s:
    if f==0 and i=='F':
        w[0]+=1
    elif f==0 and i=='T':
        f=1
    elif f==1 and i=='F':
        chk+=1
    elif f==1 and i=='T':
        yy.append(chk)
        chk=0
        f=2
    elif f==2 and i=='F':
        chk+=1
    elif f==2 and i=='T':
        xx.append(chk)
        chk=0
        f=1
if chk and f==1:
    yy.append(chk)
elif chk and f==2:
    xx.append(chk)
xx.sort()
yy.sort()
for i in xx[::-1]:
    if w[0]<x:
        w[0]+=i
    else:
        w[0]-=i
for i in yy[::-1]:
    if w[1]<y:
        w[1]+=i
    else:
        w[1]-=i
print("Yes" if w[0]==x and w[1]==y else "No")

Submission status: Accepted

Solution code:
s=input()
x,y=map(int,input().split())
xx,yy=[],[]
w=[0,0]
ans=chk=0
f=0
for i in s:
    if f==0 and i=='F':
        w[0]+=1
    elif f==0 and i=='T':
        f=1
    elif f==1 and i=='F':
        chk+=1
    elif f==1 and i=='T':
        yy.append(chk)
        chk=0
        f=2
    elif f==2 and i=='F':
        chk+=1
    elif f==2 and i=='T':
        xx.append(chk)
        chk=0
        f=1
if chk and f==1:
    yy.append(chk)
elif chk and f==2:
    xx.append(chk)
xx.sort()
yy.sort()
for i in xx[::-1]:
    if w[0]<=x:
        w[0]+=i
    else:
        w[0]-=i
for i in yy[::-1]:
    if w[1]<=y:
        w[1]+=i
    else:
        w[1]-=i
print("Yes" if w[0]==x and w[1]==y else "No")

Submission status: Accepted

Solution code:
s = input().split("T")
x,y = map(int,input().split())
x -= len(s[0])
XY = [[],[]]
for i in range(1,len(s)):
  XY[i%2].append(len(s[i]))
X,Y = XY
X.sort(reverse = True)
Y.sort(reverse = True)
for i in X:
  if x > 0:
    x -= i
  else:
    x += i
for i in Y:
  if y > 0:
    y -= i
  else:
    y += i
if (x,y) == (0,0):
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
s = input()
dest_x, dest_y = map(int, input().split())

x_list = []
y_list = []
cnt = 0
flg = 1
init = 1 if s[0] == 'F' else 0
for si in s:
    if si == 'F':
        cnt += 1
    else:
        if flg == 1:
            x_list.append(cnt)
        else:
            y_list.append(cnt)
        cnt = 0
        flg *= -1
if flg == 1:
    x_list.append(cnt)
else:
    y_list.append(cnt)

if init:
    dest_x -= x_list[0]
    x_list[0] = 0

dest_x, dest_y = abs(dest_x), abs(dest_y)

x_max = sum(x_list)
if x_max < dest_x:
    print('No')
    exit()
dpx = [[0 for _ in range(x_max+1)] for _ in range(len(x_list)+1)]
dpx[0][-1] = 1
for i in range(len(x_list)):
    for j in range(x_max+1):
        x = x_list[i]
        if dpx[i][j] == 1:
            if j - 2 * x >= 0:
                dpx[i+1][j-2*x] = 1
            dpx[i+1][j] = 1
if dpx[-1][dest_x] == 0:
    print('No')
    exit()

y_max = sum(y_list)
if y_max < dest_y:
    print('No')
    exit()
dpy = [[0 for _ in range(y_max+1)] for _ in range(len(y_list)+1)]
dpy[0][-1] = 1
for i in range(len(y_list)):
    for j in range(y_max+1):
        y = y_list[i]
        if dpy[i][j] == 1:
            if j - 2 * y >= 0:
                dpy[i+1][j-2*y] = 1
            dpy[i+1][j] = 1
if dpy[-1][dest_y] == 0:
    print('No')
    exit()

print('Yes')