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:
s = input()
x, y = map(int, input().split())
cnt = 0
xm = []
ym = []
flag = True

for i in range(len(s)):
    if s[i]=='F':
        cnt += 1
    else:
        if flag:
            xm.append(cnt)
            flag = False
        else:
            ym.append(cnt)
            flag = True
        
        cnt = 0

if flag:
    xm.append(cnt)
else:
    ym.append(cnt)
    
s = {0}

for i in range(len(xm)):
    ns = set()
    
    if i==0:
        for si in s:
            ns.add(si+xm[i])
    else:
        for si in s:
            ns.add(si-xm[i])
            ns.add(si+xm[i])
    
    s = ns

if x not in ns:
    print('No')
    exit()

s = {0}

for i in range(len(ym)):
    ns = set()
    
    for si in s:
        ns.add(si-ym[i])
        ns.add(si+ym[i])
    
    s = ns
    
if y not in s:
    print('No')
    exit()

print('Yes')

Submission status: Accepted

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

D = map(len, S.split("T"))
X, Y = {next(D)}, {0}
for i, d in enumerate(D, 1):
    if i % 2 == 0:
        X = {x + d for x in X} | {x - d for x in X}
    else:
        Y = {y + d for y in Y} | {y - d for y in Y}

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

Submission status: Accepted

Solution code:
def f(m,l):
    tmpm=set()
    for x in m:
        tmpm.add(x+l);tmpm.add(x-l)
    return tmpm

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

while s and s[0]=="F":
    x-=1
    s=s[1:]
ss=[{0},{0}]
mode=0
l=0
for i in range(len(s)):
    if s[i]=="F": l+=1
    else:
        ss[mode]=f(ss[mode],l)    
        mode^=1
        l=0

print("Yes" if x in ss[0] and y in ss[1] else "No")

Submission status: Accepted

Solution code:
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

S = sys.stdin.buffer.readline().decode().rstrip()
X, Y = list(map(int, sys.stdin.buffer.readline().split()))

# X 軸と Y 軸は独立に考えられる。
d = 0
cnt = 0
xy = [[], []]
for c in S:
    if c == 'F':
        cnt += 1
    else:
        xy[d].append(cnt)
        cnt = 0
        d = d + 1 & 1
xy[d].append(cnt)
X -= xy[0].pop(0)


def can_make(moves, obj):
    if obj < -8000:
        return False
    obj = 1 << (8000 + obj)
    dp = 1 << 8000
    for mv in moves:
        if mv == 0:
            continue
        dp = (dp << mv) | (dp >> mv)
    return (dp & obj) > 0


ok = can_make(xy[0], X) and can_make(xy[1], Y)
if ok:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
s=list(input())
x,y=map(int,input().split())
f=0
t=0
s.append('T')
n=len(s)
xl=[]
yl=[]
for i in range(n):
    if s[i]=='F':
        f+=1
    else:
        if f>0:
            if t%2==0:
                if t==0:
                    x-=f
                else:
                    xl.append(f)
            else:
                yl.append(f)
            f=0
        t+=1
def solve(x,l):
    n=len(l)
    k=2*8000+1
    dp=[[-1 for p in range(k)] for q in range(n+1)]
    dp[0][k//2]=0
    for i in range(1,n+1):
        for j in range(k):
            if dp[i-1][j]>=0:
                dp[i][j-l[i-1]]=1
                dp[i][j+l[i-1]]=1
                #print(j)
    if dp[-1][x+k//2]>=0:
        return True
    else:
        return False
if solve(x,xl) and solve(y,yl):
    print('Yes')
else:
    print('No')
#print(xl,yl)

Submission status: Accepted

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

n = len(s)

temp = 0
xys = [[],[]]

xy = 0

first = True


for i in range(n):
    if(s[i] == 'F'):
        temp += 1
    else:
        if(temp!=0):
            if(first):
                x -= temp
            else:
                xys[xy].append(temp)
            temp = 0
        xy = (xy + 1) % 2
        first = False
        
if(temp!=0):
    if(first):
        x -= temp
    else:
        xys[xy].append(temp)
        temp = 0

for xy,target in zip([0,1], [x,y]):
    nums = xys[xy]
    len_ = len(nums)
    
    if(len_ == 0):
        if(target == 0):
            continue
        else:
            print('No')
            exit()
    
    target = abs(target)
    sum_ = sum(nums)
    
    if((sum_ - target) % 2 == 1) | (target > sum_):
        print('No')
        exit()
        
    max = 1 + (sum_ - target)//2
    
    dp = [[0] * max for _ in range(len_ + 1)]
    dp[0][0] = 1
    
    for i in range(1, len_ + 1):
        dp[i][0] = 1
        temp = nums[i-1]
        for j in range(temp, max):
            dp[i][j] = dp[i-1][j-temp] | dp[i-1][j]
        
        if(dp[i][-1] == 1):
            break
            
        if(i == len_):
            print('No')
            exit()
    

print('Yes')

Submission status: Accepted

Solution code:
s = input().strip()
Xs = list(map(int, input().split()))
bs = [[],[]]
bb=t=0
for c in s:
    if c == 'T':
        bs[bb].append(t)
        bb=1-bb
        t = 0
    else:
        t += 1
bs[bb].append(t)
rr, bs[0] = bs[0][0],bs[0][1:]
for i in range(2):
    t = 1<<8000
    if i == 0:
        t = t<<rr
    for x in bs[i]:
        t = (t<<x) | (t>>x)
    if not (t>>(Xs[i]+8000))%2:
        print("No")
        break
else:
    print("Yes")

Submission status: Accepted

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

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

x = sum(x_list) - x
y = sum(y_list) - y
if x % 2 != 0 or y % 2 != 0:
    print('No')
    exit()

if x < 0 or y < 0:
    print('No')
    exit()

x //= 2
y //= 2

if s[0] == 'F':
    x_list.reverse()
    x_list.pop()

dpx = [[0] * (x+1) for _ in range(len(x_list)+1)]
dpx[0][0] = 1
dpy = [[0] * (y+1) for _ in range(len(y_list)+1)]
dpy[0][0] = 1
for i in range(len(x_list)):
    for j in range(x+1):
        if dpx[i][j]:
            dpx[i+1][j] = 1
            if j + x_list[i] <= x:
                dpx[i+1][j+x_list[i]] = 1
for i in range(len(y_list)):
    for j in range(y+1):
        if dpy[i][j]:
            dpy[i+1][j] = 1
            if j + y_list[i] <= y:
                dpy[i+1][j+y_list[i]] = 1

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

Submission status: Accepted

Solution code:
import sys
s=input()
x,y=map(int,input().split())
s=s+'T'
dp=[[] for i in range(2)]
t=0
buf=0
for j in range(len(s)):
    if s[j]=='T':
        break

for i in range(j,len(s)):
    if s[i]=='F':
        buf+=1
    else:
        if buf!=0:
            dp[t].append(buf)
            buf=0
        t=1-t
#print(s)  
#print(dp)
xb=1<<(len(s)+j)
yb=1<<(len(s))

for i in range(len(dp[0])):
    xb=(xb<<dp[0][i])|(xb>>dp[0][i])
if xb & 1<<(len(s)+x)==0:
    print('No')
    sys.exit()
for i in range(len(dp[1])):
    yb=(yb<<dp[1][i])|(yb>>dp[1][i])

if yb & 1<<(len(s)+y)==0:
    print('No')
    sys.exit()
print('Yes')

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def no():
    print("No")
    exit()

def main():
    def ok(dir):
        goal = abs(xy[dir])
        over=total[dir]-goal
        if over<0 or over%2:
            return False
        if over==0:
            return True
        over//=2
        dp = [False] * (over + 1)
        dp[0] = True
        for d in dd[dir]:
            for i in range(d, over + 1):
                dp[i] |= dp[i - d]
            if dp[-1]: return True
        return False

    s = input()[:-1]
    xy = list(map(int, input().split()))

    dd = [[], []]
    total=[0,0]
    dir = 0
    cnt = 0
    first = True
    for c in s:
        if c == "F":
            cnt += 1
        if c == "T":
            if first:
                xy[0] -= cnt
                first = False
            else:
                if cnt: dd[dir].append(cnt)
                total[dir]+=cnt
            dir = 1 - dir
            cnt = 0
    if first:
        xy[0] -= cnt
    else:
        if cnt: dd[dir].append(cnt)
        total[dir] += cnt
    #print(xy, dd)

    for dir in range(2):
        if not ok(dir): no()
    print("Yes")

main()

Submission status: Accepted

Solution code:

S = [len(x) for x in input().split('T')]

x,y = map(int,input().split(" "))
xstart = 0
dir = 0 # 0→ x方向 1 →y方向
first = True

temp = 0
spin_count = 0
x_array = []
y_array = []

xstart = S[0]
for c in S[1:]:
    if c != 0:
        dir += 1
        dir %= 2
        temp = c
        if dir == 0:
            x_array.append(temp)
        else:
            y_array.append(temp)
    else:
        dir += 1






dpx ={}
dpx[xstart] = 1


for i in x_array:
    temp = []
    remove = []
    for k,v  in dpx.items():
        if v != -1:
            temp.append(k + i)
            temp.append(k - i)
            remove.append(k)
    for k in remove:
        dpx[k] = -1
    for k in temp:
        dpx[k] = 1

dpy ={}
dpy[0] = 1
for i in y_array:
    temp = []
    remove = []

    for k,v  in dpy.items():
        if v != -1:
            temp.append(k + i)
            temp.append(k - i)
            remove.append(k)
    for k in remove:
        dpy[k] = -1
    for k in temp:
        dpy[k] = 1

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

Submission status: Accepted

Solution code:
def main():

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

    x1, y1 = 0, 0
    i = 0
    while i < n and s[i] == "F":
        x1 += 1
        i += 1

    count = 0
    UD = []
    LR = []
    while i < n:
        if s[i] == "T":
            count += 1
            i += 1
        else:
            d = 0
            while i < n and s[i] == "F":
                d += 1
                i += 1
            if count % 2 == 1: UD.append(d)
            else: LR.append(d)

    s1 = set([x1])
    for v in LR:
        temp = set()
        for u in s1:
            temp.add(u+v)
            temp.add(u-v)
        s1 = temp
    if x not in s1:
        return "No"
    s2 = set([0])
    for v in UD:
        temp = set()
        for u in s2:
            temp.add(u+v)
            temp.add(u-v)
        s2 = temp
    if y not in s2:
        return "No"

    return "Yes"




if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
s=list(input())
x,y=map(int,input().split())
P="A"
A=[]
X=[0]
Y=[0]

cnt=0
for i in range(len(s)):
    if P=="A":
        if s[i]=="F":
            cnt+=1
        else:
            A.append(cnt)
            cnt=0
            P="Y"
    elif P=="X":
        if s[i]=="F":
            cnt+=1
        else:
            X.append(cnt)
            cnt=0
            P="Y"
    else:
        if s[i]=="F":
            cnt+=1
        else:
            Y.append(cnt)
            cnt=0
            P="X"
if P=="A":
    A.append(cnt)
elif P=="X":
    X.append(cnt)
else:
    Y.append(cnt)
#print(A,X,Y)

DX1={}
DX1[0]=1
DX2={}
for i in X:
    for j in DX1:
        DX2[j+i]=1
        DX2[j-i]=1
    DX1=DX2
    DX2={}
#print(DX1)
if x-A[0] in DX1:
    flagx=1
else:
    flagx=0

DY1={}
DY1[0]=1
DY2={}
for i in Y:
    for j in DY1:
        DY2[j+i]=1
        DY2[j-i]=1
    DY1=DY2
    DY2={}
if y in DY1:
    flagy=1
else:
    flagy=0

if flagx*flagy==1:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
from collections import defaultdict
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve():
    S = input()[:-1]
    GX, GY = map(int, input().split())

    # 連続するFをまとめる ex.)FF->2
    # 連続するTをまとめる ex.)TTT->T
    S_parsed = []
    prev = S[0]
    seq = 1
    for c in S[1:]:
        if c == "F":
            if c == prev:
                seq += 1
            else:
                prev = c
                if seq % 2 == 0:
                    pass
                else:
                    S_parsed.append("T")
                seq = 1
        elif c == "T":
            if c == prev:
                seq += 1
            else:
                S_parsed.append(seq)
                seq = 1
            prev = c
    else:
        if S[-1] == "F":
            S_parsed.append(seq)

    # x,yを分離して考える
    x_move = []
    y_move = []

    dir_x = True
    for c in S_parsed:
        if c == "T":
            dir_x = not dir_x
        else:
            if dir_x:
                x_move.append(c)
            else:
                y_move.append(c)

    # x,y分けて、GX,GYに到達できるかをdpで判定
    dp_x = set()
    if len(x_move) > 0:
        if S[0] != "T":
            # Fから始まる場合は、正の方向からスタート
            dp_x.add(x_move[0])
        else:
            # TTから始まってたら、どっち方向からでもスタートできる
            dp_x.add(x_move[0])
            dp_x.add(-x_move[0])
    else:
        dp_x.add(0)

    for x in x_move[1:]:
        dp_x = {key+x for key in dp_x} | {key-x for key in dp_x}

    dp_y = set()
    if len(y_move) == 0:
        dp_y.add(0)
    else:
        dp_y.add(y_move[0])
        dp_y.add(-y_move[0])
    for y in y_move[1:]:
        dp_y = {key+y for key in dp_y} | {key-y for key in dp_y}

    if GX in dp_x and GY in dp_y:
        print("Yes")
    else:
        print("No")


solve()

Submission status: Accepted

Solution code:

from collections import defaultdict
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve():
    S = input()[:-1]
    GX, GY = map(int, input().split())

    # 連続するFをまとめる ex.)FF->2
    # 連続するTをまとめる ex.)TTT->T
    S_parsed = []
    prev = S[0]
    seq = 1
    for c in S[1:]:
        if c == "F":
            if c == prev:
                seq += 1
            else:
                prev = c
                if seq % 2 == 0:
                    pass
                else:
                    S_parsed.append("T")
                seq = 1
        elif c == "T":
            if c == prev:
                seq += 1
            else:
                S_parsed.append(seq)
                seq = 1
            prev = c
    else:
        if S[-1] == "F":
            S_parsed.append(seq)

    # x,yを分離して考える
    x_move = []
    y_move = []

    dir_x = True
    for c in S_parsed:
        if c == "T":
            dir_x = not dir_x
        else:
            if dir_x:
                x_move.append(c)
            else:
                y_move.append(c)

    # x,y分けて、GX,GYに到達できるかをdpで判定
    dp_x = defaultdict(bool)
    if len(x_move) > 0:
        if S[0] != "T":
            dp_x[x_move[0]] = True
        else:
            dp_x[x_move[0]] = True
            dp_x[-x_move[0]] = True
    else:
        dp_x[0] = True

    for x in x_move[1:]:
        tmp = defaultdict(bool)
        for key, val in dp_x.items():
            tmp[key+x] = True
            tmp[key-x] = True
        dp_x = tmp

    dp_y = defaultdict(bool)
    if len(y_move) == 0:
        dp_y[0] = True
    else:
        dp_y[y_move[0]] = True
        dp_y[-y_move[0]] = True
    for y in y_move[1:]:
        tmp = defaultdict(bool)
        for key, val in dp_y.items():
            tmp[key+y] = True
            tmp[key-y] = True
        dp_y = tmp

    if dp_x[GX]and dp_y[GY]:
        print("Yes")
    else:
        print("No")


solve()

Submission status: Accepted

Solution code:
from collections import defaultdict
# -*- coding: utf-8 -*-
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve():
    S = input()[:-1]
    GX, GY = map(int, input().split())

    S_parsed = []
    prev = S[0]
    seq = 1
    for c in S[1:]:
        if c == "F":
            if c == prev:
                seq += 1
            else:
                prev = c
                if seq % 2 == 0:
                    pass
                else:
                    S_parsed.append("T")
                seq = 1
        elif c == "T":
            if c == prev:
                seq += 1
            else:
                S_parsed.append(seq)
                seq = 1
            prev = c
    else:
        if S[-1] == "F":
            S_parsed.append(seq)

    x_move = []
    y_move = []

    dir_x = True
    for c in S_parsed:
        if c == "T":
            dir_x = not dir_x
        else:
            if dir_x:
                x_move.append(c)
            else:
                y_move.append(c)

    dp_x = defaultdict(bool)
    if len(x_move) > 0:
        if S[0] != "T":
            dp_x[x_move[0]] = True
        else:
            dp_x[x_move[0]] = True
            dp_x[-x_move[0]] = True
    else:
        dp_x[0] = True

    for x in x_move[1:]:
        tmp = defaultdict(bool)
        for key, val in dp_x.items():
            tmp[key+x] = True
            tmp[key-x] = True
        dp_x = tmp

    dp_y = defaultdict(bool)
    if len(y_move) == 0:
        dp_y[0] = True
    else:
        dp_y[y_move[0]] = True
        dp_y[-y_move[0]] = True
    for y in y_move[1:]:
        tmp = defaultdict(bool)
        for key, val in dp_y.items():
            tmp[key+y] = True
            tmp[key-y] = True
        dp_y = tmp

    if dp_x[GX]and dp_y[GY]:
        print("Yes")
    else:
        print("No")


solve()

Submission status: Accepted

Solution code:
S=input()
S+="E"
x,y=map(int,input().split())
go=[[],[]]
idx=0
while S[idx]=="F":
    x-=1
    idx+=1
g=1
idx+=1
while idx<len(S):
    now=0
    while S[idx]=="F":
        now+=1
        idx+=1
    if now>0:
        go[g].append(now)
    g=(g+1)%2
    idx+=1
def check(L,m):
    dp={0}
    for x in L:
        dpc=dp.copy()
        dp=set()
        for now in dpc:
            dp|={now+x,now-x}
    if m in dp:
        return True
    return False
if check(go[0],x) and check(go[1],y):
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
s = input().split("T")
x,y = map(int,input().split())
xmove = []
ymove = []
for i in range(len(s)):
    if i % 2 == 1:
        ymove.append(s[i].count("F"))
    else:
        xmove.append(s[i].count("F"))
#リストで持つdpだとTLEしたのでメモ化再帰で書く

def solve(l,cor):
    dp = {0}
    for c in l:
        dp = {c+i for i in dp}|{i-c for i in dp}
    return cor in dp

if solve(xmove[1:],x-xmove[0]) and solve(ymove,y):
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
from functools import reduce

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

C = [len(fs) for fs in s.split('T')]


# 動的計画法により、各軸について到達可能な座標の集合を求め、目標の座標に到達可能か判定
def f(cs, t):
    return t in reduce(
        lambda acc, c: {i + c for i in acc} | {i - c for i in acc},
        cs,
        {0}
    )


ans = (
    'Yes' if f(C[2::2], x - C[0]) and f(C[1::2], y) else
    'No'
)
# 出力
print(ans)

Submission status: Accepted

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

C = [len(fs) for fs in s.split('T')]


# 動的計画法により、各軸について到達可能な座標の集合を求め、目標の座標に到達可能か判定
def f(cs, t):
    dp = {0}
    for c in cs:
        dp = {i + c for i in dp} | {i - c for i in dp}
    return t in dp


ans = (
    'Yes' if f(C[2::2], x - C[0]) and f(C[1::2], y) else
    'No'
)
# 出力
print(ans)

Submission status: Accepted

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

def main():
	s = raw_input()
	s2 = raw_input()
	x, y = [int(x) for x in s2.split()]
	# print s, x, y

	xt, yt = [], []
	d, step = 0, 0
	for c in s:
		if c == 'T':
			if d == 0:
				xt.append(step)
			else:
				yt.append(step)
			step = 0
			d = 1 - d
		else:
			step += 1
	if step > 0:
		if d == 0:
			xt.append(step)
		else:
			yt.append(step)

	x -= xt[0]
	xt = xt[1:]
	xd, yd = set([0]), set([0])
	for t in xt:
		xd2 = set()
		for i in xd:
			xd2.add(i + t)
			xd2.add(i - t)
		xd = xd2
	for t in yt:
		yd2 = set()
		for i in yd:
			yd2.add(i + t)
			yd2.add(i - t)
		yd = yd2

	if x in xd and y in yd:
		print 'Yes'
	else:
		print 'No'


if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
ai = lambda: list(map(int,input().split()))
ai_ = lambda: [int(x)-1 for x in input().split()]


s = input().split('T')
x,y = ai()

xx,yy = [],[]
c = 0
for i in range(len(s)):
    if i%2:
        yy.append(len(s[i]))
    else:
        xx.append(len(s[i]))

anx = set()
for i in range(len(xx)):
    if i == 0:
        anx = {xx[i]}
    else:
        aanx = set()
        for j in anx:
            aanx.add(j+xx[i])
            aanx.add(j-xx[i])
        anx = aanx
any = {0}
for i in range(len(yy)):
    aany = set()
    for j in any:
        aany.add(j+yy[i])
        aany.add(j-yy[i])
    any = aany

if x in anx and y in any:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
s=input()
x,y=map(int, input().split())
sl=[len(i) for i in s.split('T')]
a,b={0:1},{0:1}
for i in sl[2::2]:
    if i==0:continue
    a2={}
    for j in a.keys():
        a2[j+i]=1
        a2[j-i]=1
    a=a2
for i in sl[1::2]:
    if i==0:continue
    b2={}
    for j in b.keys():
        b2[j+i]=1
        b2[j-i]=1
    b=b2
if x-sl[0] in a and y in b:print('Yes')
else:print('No')

Submission status: Accepted

Solution code:
from collections import defaultdict

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

X = []
Y = []
judge = "x"
l = 0
for i in s:
	if i == "F":
		l += 1
	else:
		if judge == "x":
			if l != 0:
				X.append(l)
			judge = "y"
		else:
			if l != 0:
				Y.append(l)
			judge = "x"
		l = 0

if judge == "x":
	if l != 0:
		X.append(l)
else:
	if l != 0:
		Y.append(l)

jj = "bad"
if len(s) >= 2:
	if s[0] == "T":
		jj = "good"


dx = defaultdict(int)
if X != []:
	if jj == "good":
		dx[X[0]] = 1
		dx[-X[0]] = 1
	else:
		dx[X[0]] = 1
	X.remove(X[0])
else:
	dx[0] = 1

for i in range(len(X)):
	D = defaultdict(int)
	for j in dx:
		if dx[j] == 1:
			D[j + X[i]] = 1
			D[j - X[i]] = 1
	dx = D


dy = defaultdict(int)
if Y != []:
	dy[Y[0]] = 1
	dy[-Y[0]] = 1
	Y.remove(Y[0])
else:
	dy[0] = 1

for i in range(len(Y)):
	D = defaultdict(int)
	for j in dy:
		if dy[j] == 1:
			D[j + Y[i]] = 1
			D[j - Y[i]] = 1
	dy = D

if dx[x] == 1 and dy[y] == 1:
	print("Yes")
else:
	print("No")

Submission status: Accepted

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

MoveX, MoveY = [], []
dir = "X"
count = 0
for i in range(len(s)): 
    if s[i] == "T":
        if count:
            if dir == "X": MoveX.append(count)
            else: MoveY.append(count)
        count = 0
        dir = ("Y" if dir == "X" else "X")      
    else:
        count += 1
if s[-1] == "F":
    if dir == "X": MoveX.append(count)
    else: MoveY.append(count)

iniX = 0
if s[0] == "F":
    iniX = MoveX.pop(0)

def DP(i, z, ini, Move = list):
    canMove = set([ini])
    for j in range(i):
        temp = []
        for item in canMove:
            temp.append(item+Move[j])
            temp.append(item-Move[j])
        canMove = set(temp)
    return z in canMove

if DP(len(MoveX), x, iniX, MoveX) and DP(len(MoveY), y, 0, MoveY): print("Yes")
else: print("No")

Submission status: Accepted

Solution code:
from math import ceil
s=input()
x,y=map(int,input().split())
s2=s.split("T")
N=len(s2)
dp_x=[set() for i in range(ceil(N/2)+1)]
dp_x[0].add(0)
dp_y=[set() for i in range(N//2+1)]
dp_y[0].add(0)
for k in range(N):
  c=len(s2[k])
  if k==0:
    for a in dp_x[0]:
      dp_x[1].add(a+c)
  elif k%2==1:
    for b in dp_y[(k-1)//2]:
      dp_y[(k+1)//2].add(b+c)
      dp_y[(k+1)//2].add(b-c)
  elif k%2==0:
    for a in dp_x[k//2]:
      dp_x[(k+2)//2].add(a+c)
      dp_x[(k+2)//2].add(a-c)
      
      
if x in dp_x[-1] and y in dp_y[-1]:
  print("Yes")
else:
  print("No")

Submission status: Accepted

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

dire = True
xi = []
yi = []
ctr = 0
for i in range(len(s)):
    if s[i] == 'F':
        ctr += 1
        continue
    if dire:
        xi.append(ctr)
    else:
        yi.append(ctr)
    ctr = 0
    dire = not dire
ans = 'Yes'
X = (sum(xi)-x)//2
x0 = xi.pop(0)
Hx = {0: True}
for i in xi:
    for j in Hx.copy().keys():
        Hx[j+i] = True
Y = (sum(yi)-y)//2
Hy = {0: True}
for i in yi:
    for j in Hy.copy().keys():
        Hy[j+i] = True
        
if (x0+sum(xi)-x)%2 != 0 or (sum(yi)-y)%2 != 0:
    ans = 'No'
if not Hx.get(X, False) or not Hy.get(Y, False):
    ans = 'No'
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
s = input()+"T"
n = len(s)
x,y = map(int, input().split())

fcnt = 0
tcnt = 0
xd = set()
yd = set([0])
for i in range(n):
    if s[i]=="F":
        fcnt += 1
        continue

    if tcnt==0:
        xd.add(fcnt)
    elif tcnt%2==0:
        # x軸の移動
        tmp = set()
        for xx in xd:
            tmp.add(xx-fcnt)
            tmp.add(xx+fcnt)
        xd = tmp
    else:
        # y軸の移動
        tmp = set()
        for yy in yd:
            tmp.add(yy-fcnt)
            tmp.add(yy+fcnt)
        yd = tmp
    tcnt += 1
    fcnt = 0

if x in xd and y in yd:
    print("Yes")
else:
    print("No")

Submission status: Accepted

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

ini = s.find('T')
s=s[ini+1:]

is_y=1
point=[set([ini]),set([0])]

while len(s)>1:
    tmp=s.find('T')
    if tmp != 0:
        set_=set()
        for z in point[is_y]:
            set_.add(z+tmp)
            set_.add(z-tmp)
        point[is_y]=set_
    is_y+=1
    is_y%=2
    s=s[tmp+1:]

if x in point[0] and y in point[1]:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
s = input()
x, y = map(int, input().split())
m = list(map(len, s.split('T')))
# 最初は右に行くしかない
dp_x = {m[0]}
for i in m[2::2]:
    new_dp = set()
    for v in dp_x:
        new_dp |= {v + i, v - i}
    dp_x = new_dp

dp_y = {0}
for i in m[1::2]:
    new_dp = set()
    for v in dp_y:
        new_dp |= {v + i, v - i}
    dp_y = new_dp

print('Yes' if x in dp_x and y in dp_y else 'No')

Submission status: Accepted

Solution code:
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import *

def read():
  return int(input())
 
def reads():
  return [int(x) for x in input().split()]

S = input()
x, y = reads()
qs = [len(s) for s in S.split('T')]

def knapsack(xs, target):
  es = {0}
  for x in xs:
    es = {e - x for e in es} | {e + x for e in es}
  return target in es

ans = knapsack(qs[2::2], x-qs[0]) and knapsack(qs[1::2], y)
print("Yes" if ans else "No")

Submission status: Accepted

Solution code:
import sys
s=input()
A,B =map(int,input().split())
table=[[] for i in range(2)]#0が横1が縦
t = 0
k=0
for i in range(len(s)):
    if s[i]=='F':
        k+=1
    else:
        table[t].append(k)
        t=(t+1)%2
        k=0
table[t].append(k)
L=len(s)
Lx=[set() for i in range(len(table[0]) +1)]
Ly=[set() for i in range(len(table[1]) +1)]
Lx[0].add(0)
Ly[0].add(0)
for i in range(len(table[0])):
    x = table[0][i]
    if i==0:
        Lx[1].add(x)
        continue
    for a in Lx[i]:
        if -L<=a+x<=L:
            Lx[i+1].add(a+x)
        if -L<=a-x<=L:
            Lx[i+1].add(a-x) 
flag=True
for a in Lx[len(table[0])]:
    if a==A:
        flag =False
if flag:
    print('No')
    sys.exit()
    
for i in range(len(table[1])):
    x = table[1][i]
    for a in Ly[i]:
        if -L<=a+x<=L:
            Ly[i+1].add(a+x)
        if -L<=a-x<=L:
            Ly[i+1].add(a-x)
flag=True
for a in Ly[len(table[1])]:
    if a==B:
        flag =False
if flag:
    print('No')
    sys.exit()
    
print('Yes')

Submission status: Accepted

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys,collections,itertools,re,math,fractions,decimal,random,array,bisect,heapq

# decimal.getcontext().prec = 50
# sys.setrecursionlimit(10000)
# MOD = 10**9 + 7

def solve(f):
    s = f.read_str()
    x, y = f.read_int_list()

    l = s.split('T')
    x0 = len(l[0])

    ys = {0}
    for i in l[1::2]:
        ys2 = set()
        yi = len(i)
        for item in ys:
            ys2.add(item+yi)
            ys2.add(item-yi)
        ys = ys2

    if y not in ys: return 'No'

    xs = {x0}
    for i in l[2::2]:
        xs2 = set()
        xi = len(i)
        for item in xs:
            xs2.add(item+xi)
            xs2.add(item-xi)
        xs = xs2

    if x not in xs: return 'No'

    return 'Yes'


class Reader(object):
    def __init__(self, filename=None):
        self.file = open(filename) if filename is not None else None
        self.case = 1

    def __readline(self):
        return self.file.next().strip() if self.file else raw_input()

    def next_case(self):
        self.file.next()
        self.case += 1

    def read_int(self): return int(self.__readline())
    def read_float(self): return float(self.__readline())
    def read_long(self): return long(self.__readline())
    def read_decimal(self): return decimal.Decimal(self.__readline())
    def read_str(self): return self.__readline()

    def read_int_list(self): return map(int, self.__readline().split())
    def read_float_list(self): return map(float, self.__readline().split())
    def read_long_list(self): return map(long, self.__readline().split())
    def read_decimal_list(self): return map(decimal.Decimal, self.__readline().split())
    def read_str_list(self): return self.__readline().split()

if __name__ == '__main__':
    filename = sys.argv[1] if len(sys.argv) > 1 else None
    f = Reader(filename)
    if f.file:
        while True:
            print "Case #%d\n"%f.case, solve(f)
            try:
                f.next_case()
            except StopIteration:
                break
    else:
        print solve(f)

Submission status: Accepted

Solution code:
s = input().split("T")
x, y = map(int, input().split())
x -= len(s[0])
s.pop(0)

list_yoko = [len(s[i]) for i in range(1, len(s), 2)]
list_tate = [len(s[i]) for i in range(0, len(s), 2)]

if list_yoko == []:
	list_yoko = [0]

if list_tate == []:
	list_tate = [0]
	
set_yoko = {list_yoko[0], -list_yoko[0]}
set_tate = {list_tate[0], -list_tate[0]}

for i in list_yoko[1:]:
	sy = set()
	for j in set_yoko:
		sy |= {j + i, j - i}
	set_yoko = sy

for i in list_tate[1:]:
	st = set()
	for j in set_tate:
		st |= {j + i, j - i}
	set_tate = st

if (x in set_yoko) and (y in set_tate):
	print("Yes")
else:
	print("No")