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:
import sys

sys.setrecursionlimit(10**6)
input = sys.stdin.readline

def main():
    order_F = []
    order_F = input().rstrip().split('T')
    x, y = map(int, input().split())
    vert_DP = [False] * 8001
    side_DP = [False] * 8001
    vert_DP[0] = True
    side_DP[0] = True
    vert_sum = 0
    side_sum = 0
    distance = [len(order_F[i]) for i in range(len(order_F))]
    order_len = len(order_F)
    x -= len(order_F[0])
    vert_last = 0
    side_last = 0
    for i in range(1, order_len):
        if i % 2 == 0:
            side_sum += distance[i]
            for j in reversed(range(side_last+1)):
                if side_DP[j]:
                    side_DP[j + distance[i]] = True
            side_last += distance[i]
        else:
            vert_sum += distance[i]
            for j in reversed(range(vert_last+1)):
                if vert_DP[j]:
                    vert_DP[j + distance[i]] = True
            vert_last += distance[i]
    if side_sum < x or vert_sum < y:
        print("No")
    elif (side_sum - x) % 2 == 1 or (vert_sum - y) % 2 == 1:
        print("No")
    else:
        if side_DP[(side_sum - x)//2] and vert_DP[(vert_sum - y)//2]:
            print("Yes")
        else:
            print("No")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
s=list(input())
x,y = map(int, input().split())
y=abs(y)
s.append('T')
x1=[]
y1=[]

count=0
u=0
for i in range(len(s)):
    if s[i]=='F':
        count+=1
    else:
        if u%2==0:
            x1.append(count)
            u+=1
            count=0
        else:
            y1.append(count)
            u+=1
            count=0
v=sum(x1)
w=sum(y1)
v-=x1[0]
x-=x1[0]
x1.pop(0)
x=abs(x)
if v<x or w<y:
    print('No')
    exit()

dpx=[[0 for i in range(v+1)] for j in range(len(x1)+1)]
dpx[0][-1]=1

for i in range(len(x1)):
    for j in range(v+1):
        if dpx[i][j]==1:
            if j-2*x1[i]>=0:
                dpx[i+1][j-2*x1[i]]=1
            dpx[i+1][j]=1

if len(y1)==0:
    y1.append(0)
dpy=[[0 for i in range(w+1)] for j in range(len(y1)+1)]
dpy[0][-1]=1

for i in range(len(y1)):
    for j in range(w+1):
        if dpy[i][j]==1:
            if j-2*y1[i]>=0:
                dpy[i+1][j-2*y1[i]]=1
            dpy[i+1][j]=1

if dpy[-1][y]==1 and dpx[-1][x]==1:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
s = input()
n = len(s)
xy = list(map(int, input().split()))

right = 0
updown = []
rightleft = []

direction = 0
for i in range(n):
    if s[i]=='F':
        if direction == 0:
            right+=1
        elif direction == 1:
            updown[-1]+=1
        else:
            rightleft[-1]+=1
    else:
        if direction == 0 or direction == 2:
            direction = 1
            updown.append(0)
        else:
            direction = 2
            rightleft.append(0)

updown = [x for x in updown if x!=0]
rightleft = [x for x in rightleft if x!=0]

# 最初はx方向にしか進めないので補正
xy[0]-=right

# updownでyに行けるか？rightleftでxに行けるかを考えれば良い
dpy = [set() for i in range(len(updown)+1)]
dpy[0].add(0)
for i in range(len(updown)):
    for y in dpy[i]:
        dpy[i+1].add(y+updown[i])
        dpy[i+1].add(y-updown[i])

dpx = [set() for i in range(len(rightleft)+1)]
dpx[0].add(0)
for i in range(len(rightleft)):
    for x in dpx[i]:
        dpx[i+1].add(x+rightleft[i])
        dpx[i+1].add(x-rightleft[i])

if xy[1] in dpy[-1] and xy[0] in dpx[-1]:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
s = input()

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

now = 0
angle = 0 #0なら水平方向,1なら垂直方向
length = 0 

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


xlst = []
ylst = []

length = 0 

while now < len(s): #最初にTから始まる状態にしておく
    while now < len(s) and s[now] == "T":
        angle = 1 - angle
        now += 1
    while now < len(s) and s[now] == "F":
        length += 1
        now += 1
    if angle == 0:
        xlst.append(length)
    else:
        ylst.append(length)
    length = 0

now_x = 0
now_y = 0
xlst.sort(reverse=True)
ylst.sort(reverse=True)
for i in xlst:
    if x - now_x >= 0:
        now_x += i
    else:
        now_x -= i

for i in ylst:
    if y - now_y >= 0:
        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:
s = input().strip()
x,y = map(int,input().split())
C = {}
cur = 0
cnt = 0
for i in range(len(s)):
    if s[i]=="F":
        cnt += 1
    else:
        C[cur] = cnt
        cur += 1
        cnt = 0
C[cur] = cnt
AX = []
AY = []
for a in C:
    if a>0 and a%2==0 and C[a]>0:
        AX.append(C[a])
    elif a%2==1 and C[a]>0:
        AY.append(C[a])
cntx = sum(AX)
dpx = [[0 for _ in range(2*cntx+1)] for _ in range(len(AX)+1)]
dpx[0][cntx] = 1
if cntx>0:
    dpx[1][cntx-AX[0]] = 1
    dpx[1][cntx+AX[0]] = 1
    for i in range(2,len(AX)+1):
        for j in range(-cntx,cntx+1):
            if -cntx<=j-AX[i-1]:
                dpx[i][j+cntx] = dpx[i-1][j-AX[i-1]+cntx]
                if j+AX[i-1]<=cntx:
                    dpx[i][j+cntx] = max(dpx[i][j+cntx],dpx[i-1][j+AX[i-1]+cntx])
            elif j+AX[i-1]<=cntx:
                dpx[i][j+cntx] = dpx[i-1][j+AX[i-1]+cntx]
cnty = sum(AY)
dpy = [[0 for _ in range(2*cnty+1)] for _ in range(len(AY)+1)]
dpy[0][cnty] = 1
if cnty>0:
    dpy[1][cnty-AY[0]] = 1
    dpy[1][cnty+AY[0]] = 1
    for i in range(2,len(AY)+1):
        for j in range(-cnty,cnty+1):
            if -cnty<=j-AY[i-1]:
                dpy[i][j+cnty] = dpy[i-1][j-AY[i-1]+cnty]
                if j+AY[i-1]<=cnty:
                    dpy[i][j+cnty] = max(dpy[i][j+cnty],dpy[i-1][j+AY[i-1]+cnty])
            elif j+AY[i-1]<=cnty:
                dpy[i][j+cnty] = dpy[i-1][j+AY[i-1]+cnty]
if C[0]-cntx<=x<=C[0]+cntx and dpx[len(AX)][x-C[0]+cntx]==1 and -cnty<=y<=cnty and dpy[len(AY)][y+cnty]==1:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
S = input()
x, y = map(int, input().split())
M = S.count("F")
F = list(map(len, S.split("T")))
F1 = F[::2]
B1 = 1 << sum(F1)
X = B1
X = X << x if x > 0 else X >> -x
B1 = B1 << F1[0]
for f in F1[1:]:
    B1 = (B1 << f) | (B1 >> f)
b1 = B1 & X
F2 = F[1::2]
B2 = 1 << sum(F2)
Y = B2
Y = Y << y if y > 0 else Y >> -y
for f in F2:
    B2 = (B2 << f) | (B2 >> f)
b2 = B2 & Y
print("YNeos"[not(b1 and b2)::2])

Submission status: Accepted

Solution code:
import datetime
s = input()
x, y = map(int, input().split())
#print(datetime.datetime.now())
 
num_of_f = 0
for c in s:
    if c == 'F':
        num_of_f += 1
dp = [[[False] * 32001 for _ in range(2)] for _ in range(2)]
 
dp[0][0][0] = True
dp[0][1][0] = True
# print(dp)
index = 0
while index < len(s) and s[index] == 'F':
    index += 1
x -= index
xy_axis = 1
dp_index = 1
i = index + 1
num_of_f = index
while i < len(s):
    num = 0
    while i < len(s) and s[i] == 'F':
        num += 1
        i += 1
    num_of_f += num
    for j in range(-1 * num_of_f, num_of_f + 1):
        dp[dp_index][xy_axis][j] = dp[(dp_index + 1) % 2][xy_axis][j - num] or dp[(dp_index + 1) % 2][xy_axis][j + num]
    xy_axis = (xy_axis + 1) % 2
    if xy_axis == 1:
        dp_index = (dp_index + 1) % 2
    i += 1
 
# print(dp)
 
if dp[(dp_index + 1) % 2][0][x] and dp[(dp_index + xy_axis) % 2][1][y]:
    print('Yes')
else:
    print('No')
#print(datetime.datetime.now())

Submission status: Accepted

Solution code:
s = input()
x, y = map(int, input().split())
a = []
b = []
c = 0
n = 0
for i in range(len(s)):
  if s[i]=='F':
    n += 1
  else:
    if c%2==0:
      a += [n]
    else:
      b += [n]
    c += 1
    n = 0
if s[-1]=='F':
  if c%2==0:
    a += [n]
  else:
    b += [n]
sa = sum(a)
sb = sum(b)
dpa1 = [0]*(2*sa+1)
dpa2 = [0]*(2*sa+1)
dpb1= [0]*(2*sb+1)
dpb2 = [0]*(2*sb+1)
dpa1[sa] = 1
dpb1[sb] = 1
if len(a)>0:
  if sa-a[0]<abs(x-a[0]) or sb<abs(y):
    print('No')
    import sys
    sys.exit()
else:
  if sa<abs(x) or sb<abs(y):
    print('No')
    import sys
    sys.exit()

if len(a)%2==0:
  A = dpa1
else:
  A = dpa2
if len(b)%2==0:
  B = dpb1
else:
  B = dpb2

for i in range(len(a)):
  c = a[i]
  if i%2==0:
    for j in range(2*sa+1):
      if dpa1[j]==1:
        dpa1[j] = 0
        dpa2[j-c] = 1
        dpa2[j+c] = 1
  else:
    for j in range(2*sa+1):
      if dpa2[j]==1:
        dpa2[j] = 0
        dpa1[j-c] = 1
        dpa1[j+c] = 1
for i in range(len(b)):
  c = b[i]
  if i%2==0:
    for j in range(2*sb+1):
      if dpb1[j]==1:
        dpb1[j] = 0
        dpb2[j-c] = 1
        dpb2[j+c] = 1
  else:
    for j in range(2*sb+1):
      if dpb2[j]==1:
        dpb2[j] = 0
        dpb1[j-c] = 1
        dpb1[j+c] = 1

if A[x+sa]==1 and B[y+sb]==1:
  print('Yes')
else:
  print('No')

Submission status: Accepted

Solution code:
from pprint import pprint
# 盤面座標は8000x8000(x 2)
N = 8000

s = input()
target_x, target_y = map(int, input().split())

# "FTTFF" -> [1, 0, 2]と変換。'F1TF0TF2T'のこと
dat_f = [len(Fcount) for Fcount in s.split("T")]

# x, yの閾値判定するのが面倒なので倍用意する(index error対策)
# マイナスを含むxとyのDP用配列を作る +1するのは原点用
curmap_x = [False] * ((N * 2 * 2) + 1)
curmap_y = [False] * ((N * 2 * 2) + 1)
nextmap_x = [False] * ((N * 2 * 2) + 1)
nextmap_y = [False] * ((N * 2 * 2) + 1)

# 初期座標 0,0
if s[0] == "F":
    start = 1
    curmap_x[N * 2 + dat_f[0]] = True
    direction_x = False
else:
    start = 0
    curmap_x[N * 2] = True
    direction_x = True
curmap_y[N * 2] = True

# 今がx軸かの判定

for i in range(start, len(dat_f)):
    step = dat_f[i]
    if direction_x:
        for j in range(N, N + (N * 2) + 1):
            nextmap_x[j] = True if curmap_x[j - step] or curmap_x[j + step] else False
        curmap_x, nextmap_x = nextmap_x, curmap_x
    else:
        for j in range(N, N + (N * 2) + 1):
            nextmap_y[j] = True if curmap_y[j - step] or curmap_y[j + step] else False
        curmap_y, nextmap_y = nextmap_y, curmap_y

    direction_x = not direction_x

print("Yes" if curmap_x[N * 2 + target_x] and curmap_y[N * 2 + target_y] else "No")

Submission status: Accepted

Solution code:
s=input()
X,Y=map(int,input().split())
from collections import defaultdict
def solve(a,dp,G):
    for i in range(len(a)):
        tmp=set()
        for j in dp:
            tmp.add(j-a[i])
            tmp.add(j+a[i])
        dp=tmp
    if G in dp:
        return True
    else:
        return False

d=[len(x) for x in s.split("T")]
#print(d)
x=d[2::2]
dpx={d[0]}
y=d[1::2]
dpy={0}
if solve(x,dpx,X) and solve(y,dpy,Y):
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
s=input()
X,Y=map(int,input().split())
from collections import defaultdict
def solve(da,dp,G):
    for i in range(len(da)):
        tmp=set()
        for j in dp:
            tmp.add(j-da[i])
            tmp.add(j+da[i])
        dp=tmp
    if G in dp:
        return True
    else:
        return False
    """amax=0
    for i in a:
        amax+=abs(i)
    if amax==0:
        if G==0:
            return True
        else:
            return False
    if a[0]==G:
        return True
    dp=[[0]*2*amax for _ in range(3)]
    dp[0][a[0]]=1
    for i in range(1,len(a)):
        p=a[i]
        for j in range(-amax,amax):
            dp[i%3][j-p]+=dp[(i-1)%3][j]
            dp[i%3][j+p]+=dp[(i-1)%3][j]
            #print(dp)
        if dp[i%3][G]>=1:
            return True
    return False"""
    """#print(a)
    dp=set()
    dp.add(a[0])
    dp2=set()
    for i in range(1,len(a)):
        for j in dp:
            dp2.add(j-a[i])
            dp2.add(j+a[i])
        dp=copy.deepcopy(dp2)
    #print(dp)
    if G in dp:
        return True
    return False"""

d=[len(x) for x in s.split("T")]

dx=d[2::2]
dy=d[1::2]

if solve(dx,{d[0]},X) and solve(dy,{0},Y):
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))  # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignores trailing space

S = list(rs())
X, Y = rl()

xy = [[], []]
dir = 0 # x軸方向を0とし、y軸方向を1とする
cur = 0
for s in S:
    if s == 'F':
        cur += 1
    else:
        xy[dir].append(cur)
        cur = 0
        dir = 1 - dir
xy[dir].append(cur)
# x軸方向が達成可能かどうか
xmax = sum(xy[0])
dif_x = xmax - X
ymax = sum(xy[1])
dif_y = ymax - Y
if dif_x&1 == 1 or dif_y&1 == 1:
    print('No')
    exit()
dif_x //= 2
dif_y //= 2
xset = set([0])
for x in xy[0][1:]:
    new_set = set()
    for z in xset:
        new_set.add(z+x)
    xset = xset.union(new_set)
yset = set([0])
for y in xy[1]:
    new_set = set()
    for z in yset:
        new_set.add(z+y)
    yset = yset.union(new_set)

if dif_x in xset and dif_y in yset:
    print('Yes')
else:
    print('No')
# 31

Submission status: Accepted

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

xmove = []
ymove = []

tmp = 0
flag = True
for i in range(len(S)):
    if S[i]=='F':
        tmp += 1
    elif flag and S[i]=='T':
        xmove.append(tmp*2)
        tmp = 0
        flag = False
    else:
        ymove.append(tmp*2)
        tmp = 0
        flag = True
        
if xmove:
    x0 = xmove.pop(0)//2
else:
    x0 = 0

xgoal = sum(xmove)//2+X-x0
ygoal = sum(ymove)//2+Y

def subsetsum(arr,s):
    if not arr or s < 0:
        if s==0:
            return True
        else:
            return False
    dp = [[0 for j in range(s+1)] for i in range(len(arr)+1)]
    dp[0][0] = 1
    for i in range(1,len(arr)+1):
        for j in range(s+1):
            if j-arr[i-1]>=0:
                dp[i][j] = (dp[i-1][j-arr[i-1]] | dp[i-1][j])
            else:
                dp[i][j] = dp[i-1][j]
    return dp[-1][s]
    
if subsetsum(xmove,xgoal) and subsetsum(ymove,ygoal):
    print('Yes')
else:
    print('No')

Submission status: Accepted

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

x_axis = S[::2]
y_axis = S[1::2]

const = 8000
x_pos = 1 << const
y_pos = 1 << const
first = True
for s in x_axis:
    step = len(s)
    if first:
        x_pos <<= step
        first = False
    else:
        x_pos = x_pos << step | x_pos >> step

for s in y_axis:
    step = len(s)
    y_pos = y_pos << step | y_pos >> step

if x_pos >> (const+x) & 1 and y_pos >> (const+y) & 1:
    print('Yes')
else:
    print('No')

Submission status: Accepted

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

x_axis = 1 << 8000
y_axis = 1 << 8000
first = True
axis = 0  # 0 = x, 1 = y
count = 0

for s in S:
    if s == 'T':
        if axis == 0:
            if first:
                x_axis = x_axis << count
            else:
                x_axis = (x_axis << count) | (x_axis >> count)
        else:
            y_axis = (y_axis << count) | (y_axis >> count)

        axis = 1 - axis  # 軸が変わる
        first = False
        count = 0

    else:
        count += 1

if count:
    if axis == 0:
        if first:
            x_axis = x_axis << count
        else:
            x_axis = (x_axis << count) | (x_axis >> count)
    else:
        y_axis = (y_axis << count) | (y_axis >> count)


if (x_axis >> (8000+x)) & 1 and (y_axis >> (8000+y)) & 1:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
import sys


s = sys.stdin.readline().rstrip()
x, y = map(int, sys.stdin.readline().split())

def reachable(deltas, goal):
    if not deltas:
        if goal == 0:
            return True
        else:
            return False
    
    s = sum(deltas)
    if goal < -s or s < goal:
        return False

    if (s - goal) & 1:
        return False

    a = (s - goal) // 2

    res = [False] * (a + 1)
    res[0] = True
    for d in deltas:
        for i in range(a, -1, -1):
            if res[i] and i + d <= a:
                res[i+d] = True
    return res[a]

def main():
    gx = x; gy = y
    dx = []
    dy = []
    direction = 0

    origin_x = 0
    for i in s:
        if i == 'T':
            break
        origin_x += 1
    gx -= origin_x

    tmp = 0
    for i in s[origin_x:] + 'T':
        if i == 'T':
            if direction == 0:
                if tmp != 0:
                    dx.append(tmp)
                direction = 1
            else:
                if tmp != 0:
                    dy.append(tmp)
                direction = 0
            tmp = 0
        else:
            tmp += 1

    return 'Yes' if reachable(dx, gx) & reachable(dy, gy) else 'No'
    
if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

Solution code:
import sys

s = sys.stdin.readline().rstrip()
x, y = map(int, sys.stdin.readline().split())

def reachable(deltas, goal):
    if not deltas:
        if goal == 0:
            return True
        else:
            return False
    
    s = sum(deltas)
    if goal < -s or s < goal:
        return False

    res = [False] * (s * 2 + 1) 
    res[s] = True
    
    for d in deltas:
        prev = res.copy()
        res = [False] * (s * 2 + 1)
        for i in range(s*2+1):
            if prev[i]:
                res[i+d] = res[i-d] = True

    return res[s + goal]


def main():
    gx = x; gy = y
    dx = []
    dy = []
    direction = 0

    origin_x = 0
    for i in s:
        if i == 'T':
            break
        origin_x += 1
    gx -= origin_x

    tmp = 0
    for i in s[origin_x:] + 'T':
        if i == 'T':
            if direction == 0:
                if tmp != 0:
                    dx.append(tmp)
                direction = 1
            else:
                if tmp != 0:
                    dy.append(tmp)
                direction = 0
            tmp = 0
        else:
            tmp += 1

    return 'Yes' if reachable(dx, gx) & reachable(dy, gy) else 'No'
    
if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[ ]:


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


# In[17]:


# s = 'FTFFTFFF'


# In[18]:


s = s + 'x'


# In[19]:


s


# In[54]:


renzoku = 1
xyres = [1 << 8000, 1 << 8000]
xy = 0
M = (1 << 16001) - 1
first = s[0] == 'F'
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        renzoku += 1
    else:
#         print(s[i], renzoku)
        if first:
            xyres[0] = 1 << (8000 + renzoku)
            first = False
        else:
            if s[i] == 'F':
                xyres[xy] = (xyres[xy] << renzoku) | (xyres[xy] >> renzoku) & M
            elif renzoku % 2 == 1:
                xy = 1 - xy
        renzoku = 1
        


# In[58]:


if xyres[0] & 1 << (8000 + x) and xyres[1] & 1 << (8000 + y):
    print('Yes')
else:
    print('No')


# In[ ]:

Submission status: Accepted

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

SIZE = 10 ** 4

M = [0] if S[0] == "T" else []
for i in range(1, len(S)):
  if S[i] != S[i-1]:
    M.append(i)
M.append(len(S))
M = [0] + M
#print("M:", M)

L = []
for i in range(1, len(M)):
  L.append(M[i] - M[i - 1])
#print("L:", L)

for i in range(3, len(L), 2):
  L[i] += L[i - 2]
L = [0] + L
#print("L:", L)

x = 1 << SIZE
y = 1 << SIZE
#print("x:", bin(x)[2::])
#print("y:", bin(y)[2::])

for i in range(1, len(L), 2):
  dist = L[i]
  dirc = L[i - 1]
  #print("dist, dirc:", dist, dirc)
  if i == 1:
    x = x << dist
  else:
    if dirc % 2 == 0:
      x = (x << dist) | (x >> dist)
    else:
      y = (y << dist) | (y >> dist)
      
if (x >> SIZE + X) & 1 == 1 and (y >> SIZE + Y) & 1 == 1:
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

S = input()
N = len(S)
X, Y = MAP()

# ランレングス圧縮
A1, A2 = [], []
cnt = d = 0
for s in S:
    if s == 'T':
        if d == 0:
            A1.append(cnt)
        else:
            A2.append(cnt)
        cnt = 0
        d ^= 1
    else:
        cnt += 1
if d == 0:
    A1.append(cnt)
else:
    A2.append(cnt)
# 最初だけ右固定なので別処理
X -= A1[0]
A1.pop(0)

# 縦横独立に、到達可の場所を確認する
M1 = len(A1)
M2 = len(A2)
# 集合管理をbitset高速化
dpx = [0] * (M1+1)
dpy = [0] * (M2+1)
# 負数を考慮してずらしてから始める
offset = 20000
dpx[0] = 1 << offset
dpy[0] = 1 << offset
for i, a in enumerate(A1):
    dpx[i+1] |= dpx[i] << a
    dpx[i+1] |= dpx[i] >> a
for i, a in enumerate(A2):
    dpy[i+1] |= dpy[i] << a
    dpy[i+1] |= dpy[i] >> a

if dpx[M1] & 1<<(X + offset) and dpy[M2] & 1<<(Y + offset):
    Yes()
else:
    No()

Submission status: Accepted

Solution code:
S = input()
X,Y = map(int,input().split())
T0 = 0
XList = []
YList = []
N = len(S)
t = 0
f = 0
for i in range(N):
    c = S[i]
    if c == 'F':
        f += 1
    else:
        if t == 0:
            T0 = f
        else:
            if t % 2 == 0:
                if f > 0:
                    XList.append(f)
            else:
                if f > 0:
                    YList.append(f)
        f = 0
        t += 1
if f > 0:
    if t == 0:
        T0 = f
    else:
        if t % 2 == 0:
            XList.append(f)
        else:
            YList.append(f)

xset = set()
yset = set()
tset = set()

xset.add( T0 )
for f in XList:
    tset.clear()
    for x in xset:
        tset.add(x+f)
        tset.add(x-f)
    xset.clear()
    for x in tset:
        xset.add(x)

yset.add( 0 )
for f in YList:
    tset.clear()
    for y in yset:
        tset.add(y+f)
        tset.add(y-f)
    yset.clear()
    for y in tset:
        yset.add(y)

if X in xset and Y in yset:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
s = list(input().split("T"))
x, y = map(int, input().split())
s2 = [len(x) for x in s]
LR = [s2[i] for i in range(len(s2)) if i % 2 == 0]
UD = [s2[i] for i in range(len(s2)) if i % 2 == 1]

flag = False
tmp = 1
tmp = tmp << sum(LR)
for i, d in enumerate(LR):
    if i != 0:
        tmp = tmp << d | tmp >> d
    else:
        tmp = tmp << d

if x >= 0:
    tmp = tmp >> sum(LR)
    if (tmp >> x) % 2 == 1:
        flag = True
else:
    if sum(LR) + x >= 0:
        if (tmp >> (sum(LR) + x)) % 2 == 1:
            flag = True
if flag:

    tmp = 1
    tmp = tmp << sum(UD)
    flag = False
    for d in UD:
        tmp = tmp << d | tmp >> d
    if y >= 0:
        tmp = tmp >> sum(UD)
        if (tmp >> y) % 2 == 1:
            flag = True
    else:
        if sum(UD) + y >= 0:
            if (tmp >> (sum(UD) + y)) % 2 == 1:
                flag = True

if flag:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
      #FT robot
s=input()
gx,gy=map(int,input().split())
lists=list(s.split("T"))
xlist=[]
ylist=[]
if s[0]=="T":
    for i in range(len(lists)):
        if i%2==0:
            xlist.append(len(lists[i]))
        else:
            ylist.append(len(lists[i]))
elif s[0]=="F":
    for i in range(len(lists)):
        if i%2==0:
            xlist.append(len(lists[i]))
        else:
            ylist.append(len(lists[i]))
    gx-=xlist[0]
    del xlist[0]
Xsum=sum(xlist)
Ysum=sum(ylist)


if (Xsum+gx)%2!=0 or (Ysum+gy)%2!=0:
    print("No")   
else:
    gx1=(Xsum+gx)//2
    gx2=(Xsum-gx)//2
    gy1=(Ysum+gy)//2
    gy2=(Ysum-gy)//2
    dpx=[[False for i in range(Xsum+2)] for j in range(len(xlist)+2)]
    dpy=[[False for i in range(Ysum+2)] for j in range(len(ylist)+2)]
    #dpx[i][j]でi番目のカードまでを足して、値ｊを取れるか判定する
    dpx[0][0]=True
    dpy[0][0]=True
    for i in range(len(xlist)):
        for j in range(Xsum+2):
            dpx[i+1][j]=dpx[i][j-xlist[i]] or dpx[i][j]
    for i in range(len(ylist)):
        for j in range(Ysum+2):
            dpy[i+1][j]=dpy[i][j-ylist[i]] or dpy[i][j]    
    #ここまではあってそうなことがわかった
    flag1=False
    if 0<= gy1<= Ysum:   
        if dpy[len(ylist)][gy1]:
            flag1=True
    flag2=False
    if 0<=gy2<= Ysum :
        if dpy[len(ylist)][gy2]:
            flag2=True
    flag3=False
    if 0<=gx1<= Xsum:
        if dpx[len(xlist)][gx1]:
            flag3=True
    flag4=False
    if 0<=gx2<= Xsum:
        if dpx[len(xlist)][gx2]:
            flag4=True
    if (flag1 or flag2) and (flag3 or flag4):
        print("Yes")
    else:
        print("No")

Submission status: Accepted

Solution code:
import copy

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

X = []
Y = []
l = 0
f = -1
xinit = 0
A = s + 'T'
for a in A:
  if a == 'T':
    if f == -1:
      xinit = l
      f = 1
    elif f == 0:
      X.append(l)
      f = 1
    else:
      Y.append(l)
      f = 0
    l = 0
  else:
    l += 1

def pattern(P,init=0):
  Q = set()
  Q.add(init)
  for p in P:
    QQ = set()
    for q in Q:
      QQ.add(q+p)
      QQ.add(q-p)
    Q = copy.copy(QQ)
  return Q

XX = pattern(X,xinit)
YY = pattern(Y)
#print(XX,YY)
if x in XX and y in YY:
  print('Yes')
else:
  print('No')

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

s = input()
x, y = MAP()

x_list = []
y_list = []
for i, s_ in enumerate(s.split('T')):
	if i == 0:
		x -= len(s_)
	elif i % 2 == 0:
		x_list.append(len(s_))
	else:
		y_list.append(len(s_))

def check(d, x):
  se =set([0])
  for dx in d:
    se={a+b for a,b in product(se,[-dx,dx])}  # 直積としてDPを計算
  return (x in se)

print('Yes' if check(x_list, x) and check(y_list, y) else 'No')

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


s = S() + 'T'
x, y = LI()
t_flag = 0
# 0の時横移動、1の時縦移動。
flag = 0
y_now = x_now = 1 << 8000
f_cnt = 0
for j in s:
    if j == 'T':
        if not flag:
            flag = 1
            x_now >>= f_cnt
        elif t_flag:
            y_now = y_now >> f_cnt | y_now << f_cnt
        else:
            x_now = x_now >> f_cnt | x_now << f_cnt
        t_flag ^= 1
        f_cnt = 0
    else:
        f_cnt += 1


if 1 << 8000 + y & y_now and 1 << 8000 - x & x_now:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
from pprint import pprint
from copy import deepcopy
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
from pprint import pprint


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


s = S() + 'T'
x, y = LI()
t_flag = 0
# 0の時横移動、1の時縦移動。
flag = 0
y_now = x_now = 1 << 8000
f_cnt = 0
for j in s:
    if j == 'T':
        if not flag:
            flag = 1
            x_now >>= f_cnt
        elif t_flag:
            y_now = y_now >> f_cnt | y_now << f_cnt
        else:
            x_now = x_now >> f_cnt | x_now << f_cnt
        t_flag ^= 1
        f_cnt = 0
    else:
        f_cnt += 1


if (1 << 8000 + y) & y_now and (1 << 8000 - x) & x_now:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
s = input()
x, y = map(int, input().split())
totalx = 0
totaly = 0
shift_x = []
shift_y = []
index = 0
while s[index] != "T" and index +1< len(s):
    totalx += 1
    index += 1
flag = 1
count = 0
for i in range(index, len(s)):
    if s[i] == "T":
        if flag == 1:
            shift_x.append(count)
        else:
            shift_y.append(count)
        flag *= -1
        count = 0
    else:
        count += 1
if flag == 1:
    shift_x.append(count)
else:
    shift_y.append(count)
shift_x.sort()
shift_x.reverse()
shift_y.sort()
shift_y.reverse()
for i in range(len(shift_x)):
    if totalx < x:
        totalx += shift_x[i]
    else:
        totalx -= shift_x[i]
for i in range(len(shift_y)):
    if totaly < y:
        totaly += shift_y[i]
    else:
        totaly -= shift_y[i]
if totalx == x and totaly == y:
    print("Yes")
else:
    print("No")