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:
#!/usr/bin python3
# -*- coding: utf-8 -*-

def main():
    S = input()
    X, Y = map(int, input().split())
    Dxy = [[] for _ in range(2)]
    cnt = 0
    direc = 0
    for x in S:
        if x == 'F':
            cnt += 1
        else:
            Dxy[direc].append(cnt)
            cnt=0
            direc = 1-direc
    Dxy[direc].append(cnt)

    Dx = Dxy[0][1:]
    Dy = Dxy[1]
    dpx=[set([Dxy[0][0]]),set([])]
    dpy=[set([0]),set([])]
    i=1
    for dx in Dx:
        dpx[i]=set([])
        for x in dpx[1-i]:
            dpx[i].add(x+dx)
            dpx[i].add(x-dx)
        i = 1-i
    j=1
    for dy in Dy:
        dpy[j]=set([])
        for y in dpy[1-j]:
            dpy[j].add(y+dy)
            dpy[j].add(y-dy)
        j = 1-j

    if X in dpx[1-i] and Y in dpy[1-j]:
        print('Yes')
    else:
        print('No')

if __name__ == '__main__':
    main()

Submission status: Accepted

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

import sys


def main():
    def input(): return sys.stdin.readline().rstrip()
    def mi():    return map(int, input().split())


    def preprocess(s):
        x_command, y_command = [], []
        L = s.split('T')
        n = len(L)
        for i in range(n):
            if i % 2 == 0 and L[i]:
                x_command.append(L[i].count("F"))
            elif i % 2 == 1 and L[i]:
                y_command.append(L[i].count("F"))
        return x_command, y_command
    

    def valid_command(command, delta):
        # print(f"{command} {delta}")
        if not command:
            return delta == 0
        possible_num_set = set()
        n = len(command)
        for i in range(n):
            if i == 0:
                possible_num_set.add(command[0])
                possible_num_set.add(-command[0])
            else:
                num = command[i]
                new_set = set()
                for candidate in possible_num_set:
                    new_set.add(candidate + num)
                    new_set.add(candidate - num)
                possible_num_set = new_set
        # print(f"{command} {possible_num_set}")
        return delta in possible_num_set

    s = input()
    s.rstrip('T')
    n = len(s)
    x, y = mi()
    dx, dy = x, y
    tail_of_front_F = -1
    for i in range(n):
        if s[i] == 'F':
            tail_of_front_F = i
        else:
            break
    s = s[tail_of_front_F+1:]
    if tail_of_front_F >= 0:
        dx -= (tail_of_front_F + 1)
    

    x_command, y_command = preprocess(s)
    # print(x_command)
    # print(y_command)
    if valid_command(x_command, dx) and valid_command(y_command, dy):
        print("Yes")
    else:
        print("No")

    


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import groupby
from sys import setrecursionlimit
setrecursionlimit(10 ** 7)

s = input()

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

d = [len(i) for i in s.split('T')]

x_li = d[2::2]
y_li = d[1::2]

now_x = [0]
now_y = [0]

x -= d[0]

for i in x_li:
    nx = set()
    for j in now_x:
        nx.add(j-i)
        nx.add(j+i)
    now_x = nx

for i in y_li:
    ny = set()
    for j in now_y:
        ny.add(j-i)
        ny.add(j+i)
    now_y = ny

if x in now_x and y in now_y:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc082/submissions/13655878
# D - FT Robot
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    s = input()
    gx, gy = map(int, input().split())

    spl = list(map(len, s.split("T")))
    init_x = spl[0]
    is_x = 0
    to_x, to_y = [], []
    for i in range(1, len(spl)):
        if spl[i]:
            to_x.append(spl[i]) if is_x else to_y.append(spl[i])
        is_x ^= 1

    now_x = [init_x]
    for move in to_x:
        nx = set()
        for x in now_x:
            nx.add(x + move)
            nx.add(x - move)
        now_x = nx

    now_y = [0]
    for move in to_y:
        ny = set()
        for y in now_y:
            ny.add(y + move)
            ny.add(y - move)
        now_y = ny

    print("Yes" if gx in now_x and gy in now_y else "No")


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    s = input()
    gx, gy = map(int, input().split())

    spl = list(map(len, s.split("T")))
    init_x = spl[0]
    is_x = 0
    to_x, to_y = [], []
    for i in range(1, len(spl)):
        to_x.append(spl[i]) if is_x else to_y.append(spl[i])
        is_x ^= 1

    now_x = [init_x]
    for move in to_x:
        nx = set()
        for x in now_x:
            nx.add(x + move)
            nx.add(x - move)
        now_x = nx

    now_y = [0]
    for move in to_y:
        ny = set()
        for y in now_y:
            ny.add(y + move)
            ny.add(y - move)
        now_y = ny

    print("Yes" if gx in now_x and gy in now_y else "No")


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    s=readline().strip()
    x,y=map(int,readline().split())
    n=len(s)

    now_x={0}
    now_y={0}
    i=0
    first=True
    flag="x"
    cnt=0
    while i<n:
        if s[i]=="F":
            cnt+=1
            i+=1
            if i==n:
                nex=set()
                if flag=="x":
                    l=len(now_x)
                else:
                    l=len(now_y)
                for _ in range(l):
                    if flag=="x":
                        now=now_x.pop()
                    else:
                        now=now_y.pop()
                    nex.add(now+cnt)
                    if first==False:
                        nex.add(now-cnt)
                if flag=="x":
                    now_x=nex
                    flag="y"
                else:
                    now_y=nex
                    flag="x"
        else:
            nex=set()
            if flag=="x":
                l=len(now_x)
            else:
                l=len(now_y)
            for _ in range(l):
                if flag=="x":
                    now=now_x.pop()
                else:
                    now=now_y.pop()
                nex.add(now+cnt)
                if first==False:
                    nex.add(now-cnt)
            if flag=="x":
                now_x=nex
                flag="y"
            else:
                now_y=nex
                flag="x"
            if first:
                first=False
            i+=1
            cnt=0

    if x in now_x and y in now_y:
        print("Yes")
    else:
        print("No")

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
S = input().split('T')
S = [len(s) for s in S]
X = S[::2]
Y = S[1::2]

from collections import defaultdict,deque

dpx = defaultdict(int)# 初期値を0にした辞書
dpy = defaultdict(int)# 初期値を0にした辞書

dpx[sum(X)] = 1
dpy[sum(Y)] = 1
que = deque()
for i,x in enumerate(X):
    if i == 0:
        continue
    for k,v in dpx.items():
        if v:
            que.append(k-2*x)
    while que:
        q = que.popleft()
        dpx[q] = 1

for i,y in enumerate(Y):
    for k,v in dpy.items():
        if v:
            que.append(k-2*y)
    while que:
        q = que.popleft()
        dpy[q] = 1


x,y = map(int,input().split())
print('Yes' if dpx[x] and dpy[y] else 'No')

Submission status: Accepted

Solution code:
S = input().split('T')

N = len(S)
X = []
Y = []

for i in range(N):
    if i%2:
        Y.append(len(S[i]))
    else:
        X.append(len(S[i]))

Xsum = sum(X)
Ysum = sum(Y)

from collections import defaultdict,deque

dpx = defaultdict(int)# 初期値を0にした辞書
dpy = defaultdict(int)# 初期値を0にした辞書

dpx[Xsum] = 1
dpy[Ysum] = 1
que = deque()
for i,x in enumerate(X):
    if i == 0:
        continue
    for k,v in dpx.items():
        if v:
            que.append(k-2*x)
    while que:
        q = que.popleft()
        dpx[q] = 1

for i,y in enumerate(Y):
    for k,v in dpy.items():
        if v:
            que.append(k-2*y)
    while que:
        q = que.popleft()
        dpy[q] = 1


x,y = map(int,input().split())
print('Yes' if dpx[x] and dpy[y] else 'No')

Submission status: Accepted

Solution code:
def main():
    # import sys
    # # input = sys.stdin.readline
    # sys.setrecursionlimit(10 ** 9)
    # MOD = 10 ** 9 + 7


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

    N = 8010

    X = [0]
    Y = [0]
    flag = True
    for s in S:
        if s == 'F':
            if flag:
                X[-1] += 1
            else:
                Y[-1] += 1
        else:
            if flag:
                if Y[-1] != 0:
                    Y.append(0)
            else:
                if X[-1] != 0:
                    X.append(0)
            flag = not flag

    dp = [[0] * (2 * N) for _ in range(2)]
    if S[0] == 'F':
        dp[0][X[0] + N] = 1
    else:
        dp[0][X[0] + N] = 1
        dp[0][-X[0] + N] = 1

    for i in range(1, len(X)):
        x_ = X[i]
        for j in range(2 * N):
            dp[i % 2][j] = 0
            if j >= x_:
                if dp[(i - 1) % 2][j - x_]:
                    dp[i % 2][j] = 1
            if j + x_ < 2 * N:
                if dp[(i - 1) % 2][j + x_]:
                    dp[i % 2][j] = 1

    # print (dp)
    if dp[(len(X) - 1) % 2][x + N] == 0:
        print ('No')
        exit()

    dp = [[0] * (2 * N) for _ in range(2)]
    dp[0][Y[0] + N] = 1
    dp[0][-Y[0] + N] = 1

    for i in range(1, len(Y)):
        y_ = Y[i]
        for j in range(2 * N):
            dp[i % 2][j] = 0
            if j >= y_:
                if dp[(i - 1) % 2][j - y_]:
                    dp[i % 2][j] = 1
            if j + y_ < 2 * N:
                if dp[(i - 1) % 2][j + y_]:
                    dp[i % 2][j] = 1

    if dp[(len(Y)-1) % 2][y + N] == 0:
        print ('No')
        exit()

    print ('Yes')

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S=input()
x,y=map(int, input().split())
N=len(S)
now=0
xs=[]
ys=[]
count=0
for i in range(N):
    if S[i]=="F":
        now+=1
    else:
        if count%2==0:
            xs.append(now)
        else:
            ys.append(now)
        count+=1
        now=0
    if S[i]=="F" and i==N-1:
        if count%2==0:
            xs.append(now)
        else:
            ys.append(now)
#print(xs,ys)
from collections import defaultdict
D=defaultdict(int)

xnow=xs.pop(0)
D[xnow]=1
for i in range(len(xs)):
    dx=xs[i]
    ds=D.keys()
    D=defaultdict(int)
    for v in ds:
        D[v+dx]=1
        D[v-dx]=1
D=dict(D)
#print(D)
xok=x in D


D=defaultdict(int)
D[0]=1
for i in range(len(ys)):
    dy=ys[i]
    ds=D.keys()
    D=defaultdict(int)
    for v in ds:
        D[v+dy]=1
        D[v-dy]=1
D=dict(D)
#print(D)
yok=y in D


if xok and yok:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
S = input().split('T')

N = len(S)
X = []
Y = []

for i in range(N):
    if i%2:
        Y.append(len(S[i]))
    else:
        X.append(len(S[i]))

Xsum = sum(X)
Ysum = sum(Y)

from collections import defaultdict

dpx = defaultdict(int)# 初期値を0にした辞書
dpy = defaultdict(int)# 初期値を0にした辞書

dpx[Xsum] = 1
dpy[Ysum] = 1

for k in range(1,len(X)):
    x = X[k]
    for i in range(-Xsum,Xsum+1):
        if dpx[i]:
            dpx[i-2*x] = 1

for y in Y:
    for i in range(-Ysum,Ysum+1):
        if dpy[i]:
            dpy[i-2*y] = 1

x,y = map(int,input().split())
print('Yes' if dpx[x] and dpy[y] else 'No')

Submission status: Accepted

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

def check(dz, z, base=8001, first_fixed=True):
  a = dz[0]
  if first_fixed:
    state = 1 << base + a
  else:
    state = 1 << (base + a) | 1 << (base - a) 
  for a in dz[1:]:
    state = (state<<a | state>>a)
  return (state>>(base+z)) & 1

if check(dx, x) and check(dy, y, first_fixed=False):
  print('Yes')
else:
  print('No')

Submission status: Accepted

Solution code:
def parse(s):
    first_val = None
    verticals, horizontals = list(), list()
    seq = 0
    is_horizontal = True
    is_first = True
    for c in s:
        if c == 'T':
            if is_first:
                first_val = seq
                is_first = False
            elif is_horizontal:
                horizontals.append(seq)
            else:
                verticals.append(seq)
            is_horizontal = not is_horizontal
            seq = 0
        else:
            seq += 1
    if seq > 0:
        if is_first:
            first_val = seq
        elif is_horizontal:
            horizontals.append(seq)
        else:
            verticals.append(seq)
    return first_val, verticals, horizontals


def check(vals, target):
    val_set = {0}
    for v in vals:
        tmp_set = set()
        for sv in val_set:
            tmp_set.add(sv + v)
            tmp_set.add(sv - v)
        val_set = tmp_set
    return target in val_set


def main():
    S = input()
    X, Y = list(map(int, input().split(' ')))
    fv, vs, hs = parse(S)
    if check(hs, X - fv) and check(vs, Y):
        print('Yes')
    else:
        print('No')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input() + 'T'
n = len(s)
x, y = map(int, input().split())
X = [[], [0]]
xy = 0
cnt =0
for i in s:
    if i == 'F':
        cnt += 1
    else:
        X[xy].append(cnt)
        xy = 1 - xy
        cnt = 0

def f(l):
    if len(l) == 0:
        return [0]
    post = [l[0]]
    for i in range(1, len(l)):
        if l[i]:
            pre = sorted(post)
            post = []
            for j in range(len(pre)):
                if j == 0 or pre[j] != pre[j-1]:
                    post.append(pre[j]+l[i])
                    post.append(pre[j]-l[i])
    return post

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

Submission status: Accepted

Solution code:





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


op_x = []
op_y = []
cnt_turn = 0
cnt_f = 0
sx,sy = 0, 0
for s in S:
    if s == "F":
        cnt_f += 1
    else:
        if cnt_turn  == 0:
            sx = cnt_f
            cnt_turn += 1
            cnt_f = 0
        elif cnt_turn % 2 == 0:
            op_x.append(cnt_f)
            cnt_turn += 1
            cnt_f = 0
        else:
            op_y.append(cnt_f)
            cnt_turn += 1
            cnt_f = 0

if cnt_turn  == 0:
    sx = cnt_f
    cnt_turn += 1
    cnt_f = 0
elif cnt_turn % 2 == 0:
    op_x.append(cnt_f)
    cnt_turn += 1
    cnt_f = 0
else:
    op_y.append(cnt_f)
    cnt_turn += 1
    cnt_f = 0

X = set()
X.add(sx)
for dx in op_x:
    nxt = set()
    for x in X:
        nxt.add(x + dx)
        nxt.add(x - dx)
    X = nxt

Y = set()
Y.add(sy)
for dy in op_y:
    nxt = set()
    for y in Y:
        nxt.add(y + dy)
        nxt.add(y - dy)
    Y = nxt

print("Yes" if gx in X and gy in Y else "No")

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc082/tasks/arc087_b


def run_length_encoding(s):
    '''
    連長圧縮を行う
    s ... iterable object e.g. list, str
    return
    ----------
    s_composed,s_num,s_idx
    それぞれ、圧縮後の文字列、その文字数、その文字が始まるidx
    '''
    s_composed = []
    s_sum = []
    s_idx = [0]
    pre = s[0]
    cnt = 1
    for i, ss in enumerate(s[1:], start=1):
        if pre == ss:
            cnt += 1
        else:
            s_sum.append(cnt)
            s_composed.append(pre)
            s_idx.append(i)
            cnt = 1
            pre = ss
    s_sum.append(cnt)
    s_composed.append(pre)
    # assert len(s_sum) == len(s_composed)
    return s_composed, s_sum, s_idx

# データに関しては事前にランレングス圧縮しておくと楽かもね


s = input()
x, y = map(int, input().split())
s_composed, s_sum, _ = run_length_encoding(s)

if s_composed[0] == 'T':  # 必ずTから始まるようにしたい
    s_composed.insert(0, 'F')
    s_sum.insert(0, 0)
# 前処理 xの移動履歴とyの移動履歴を作る
is_x = True
hist_x = []
hist_y = []
for s, n in zip(s_composed, s_sum):
    if s == 'T':
        if n & 1:  # xyの状態を反転させる
            is_x = not is_x
    else:
        if is_x:
            hist_x.append(n)
        else:
            hist_y.append(n)

'''
dpx[i][j]...x座標がjであることが可能か？ x方向の移動についてi回目の移動のあとに。
dpy[i][j]...y座標がjであることが可能か？ y方向の移動についてi回目の移動のあとに。

更新則
xについての移動,距離d移動するとすると
dpx[i+1][j+d]=dpx[i+1][j+d] or dpx[i][j]
dpx[i+1][j-d]=dpx[i+1][j-d] or dpx[i][j]
(配るDP)で更新すると楽かな？
yも同様

初期条件
dpx[:][:]=False
dpx[0][d_x0]=True #xの0回目の移動。

dpy[:][:] = False
dpy[0][d_y0]=True 簡単のために
dpy[0][-d_y0]=True
'''

try:
    if hist_x:
        x_range = 2 * (sum(hist_x) + 1500)  # ここが0になるときがある
        shift = x_range // 2
        dpx = [[False] * x_range for _ in range(len(hist_x))]
        dpx[0][hist_x[0] + shift] = True
        for i in range(1, len(hist_x)):
            d = hist_x[i]
            for j in range(x_range):
                if j + d < x_range:
                    dpx[i][j] = dpx[i][j] or dpx[i - 1][j + d]
                if -1 < j - d:
                    dpx[i][j] = dpx[i][j] or dpx[i - 1][j - d]
        ansx = dpx[-1][x + shift]
    # else:
    #     ansx = x == 0

    if hist_y:
        y_range = 2 * (sum(hist_y) + 1500)  # ここが0になるときがある
        shift = y_range // 2
        dpy = [[False] * y_range for _ in range(len(hist_y))]
        dpy[0][hist_y[0] + shift] = True
        dpy[0][-hist_y[0] + shift] = True
        for i in range(1, len(hist_y)):
            d = hist_y[i]
            for j in range(y_range):
                if j + d < y_range:
                    dpy[i][j] = dpy[i][j] or dpy[i - 1][j + d]
                if -1 < j - d:
                    dpy[i][j] = dpy[i][j] or dpy[i - 1][j - d]
        ansy = dpy[-1][y + shift]
    else:
        ansy = y == 0

    print('Yes' if ansx and ansy else 'No')
except:
    print('No')

# print(ansx, ansy)
# print(*dpy, sep='\n')

Submission status: Accepted

Solution code:
import numpy as np

s = input()
x_goal, y_goal = map(int, input().split())

directed_x = None
steps = 0
xs = [0]
ys = [0]
for c in (s+'T'):   #sentinel
    if c == 'F':
        steps += 1
    else:
        if directed_x is None:
            x_goal -= steps
            directed_x = True
        elif steps != 0:
            if directed_x:
                xs.append(steps)
            else:
                ys.append(steps)
        steps = 0
        directed_x = not directed_x

dx_2 = sum(xs) - x_goal
if dx_2 % 2 == 1 or dx_2 < 0:
    ans = False
else:
    dx = dx_2 // 2
    possible_x = np.full(dx + 1, False)
    possible_x[0] = True
    for x in xs:
        possible_x[x:] = np.logical_or(possible_x[:len(possible_x)-x], possible_x[x:])
        if possible_x[dx]:
            ans = True
            break
    else:
        ans = False

if ans:
    dy_2 = sum(ys) - y_goal
    if dy_2 % 2 == 1 or dy_2 < 0:
        ans = False
    else:
        dy = dy_2 // 2
        possible_y = np.full(dy + 1, False)
        possible_y[0] = True
        for y in ys:
            possible_y[y:] = np.logical_or(possible_y[:len(possible_y)-y], possible_y[y:])
            if possible_y[dy]:
                ans = True
                break
        else:
            ans = False

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

Submission status: Accepted

Solution code:
s = input()
x,y = map(int, input().split())
start = 0
key = len(s)
way = 0
dist = 0
now = 0
now_x = set([0])
now_y = set([0])
for k,i in enumerate(s):
  if i == "T":
    key = k
    break
  start += 1
x -= start
for i in s[key:]:
  if i == "T":
    if now:
      now = 0
      if way:
        new_y = set()
        for i in now_y:
          new_y.add(i+dist)
          new_y.add(i-dist)
        now_y = new_y
      else:
        new_x = set()
        for i in now_x:
          new_x.add(i+dist)
          new_x.add(i-dist)
        now_x = new_x
      dist = 0
    way ^= 1
  else:
    now = 1
    dist += 1
if way:
  new_y = set()
  for i in now_y:
    new_y.add(i+dist)
    new_y.add(i-dist)
    now_y = new_y
else:
  new_x = set()
  for i in now_x:
    new_x.add(i+dist)
    new_x.add(i-dist)
    now_x = new_x
if (x in now_x) and (y in now_y):
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    s=list(input())+['T']
    x,y=map(int,input().split())
    x+=16000
    y+=16000
    ud=[]
    lr=[]
    f=0
    f2=0
    c=0
    for t in s:
        if t=='F':
            if not f:
                x-=1
            else:
                c+=1
        else:
            f=1
            if c:
                if f2:
                    ud.append(c)
                else:
                    lr.append(c)
            c=0
            f2=not f2
    for l,z in [(ud,y),(lr,x)]:
        N=len(l)
        dp=[0]*(N+1)
        dp[0]=1<<16000
        for i in range(N):
            dp[i+1]|=dp[i]<<l[i]|dp[i]>>l[i]
        if not (dp[-1]>>z)&1:
            print('No')
            break
    else:
        print('Yes')
        
    

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = str(input())
x,y = map(int,input().split())
x_list,y_list = [],[]
l = s.split('T')

for i in range(0,len(l),2):
  x_list.append(len(l[i]))
for i in range(1,len(l),2):
  y_list.append(len(l[i]))

ans_x,ans_y = set(),set()

if s[0]=='F':
  ans_x.add(x_list[0])
  x_list=x_list[1:]
else:
  ans_x.add(0)
ans_y.add(0)

for i in x_list:
  ans = set()
  for j in ans_x:
    ans.add(i+j)
    ans.add(j-i)
  ans_x = ans
for i in y_list:
  ans = set()
  for j in ans_y:
    ans.add(i+j)
    ans.add(j-i)
  ans_y = ans
if x in ans_x and y in ans_y:
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
from itertools import product

S=input()
x,y=map(int,input().split())
L=[len(i) for i in S.split("T")]

X={L[0]}
Y={0}
for i in L[2::2]:
    X={j+k for j,k in product(X,[-i,i])}
for i in L[1::2]:
    Y={j+k for j,k in product(Y,[-i,i])}

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

Submission status: Accepted

Solution code:



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

cnt_T = 0
cnt = 0

step_X = []
step_Y = []
sx = 0
sy = 0
for s in S:
    if s == "F":
        cnt += 1
    else:
        if cnt_T == 0:
            sx = cnt
            cnt = 0
            cnt_T += 1
        elif cnt_T % 2 == 0:
            step_X.append(cnt)
            cnt = 0
            cnt_T += 1
        else:
            step_Y.append(cnt)
            cnt = 0
            cnt_T += 1



if cnt_T == 0:
    sx = cnt
    cnt = 0
    cnt_T += 1
elif cnt_T % 2 == 0:
    step_X.append(cnt)
    cnt = 0
    cnt_T += 1
else:
    step_Y.append(cnt)
    cnt = 0
    cnt_T += 1


dp_X = set()
dp_X.add(sx)
for i in step_X:
    tmp = set()
    for x in dp_X:
        tmp.add(x + i)
        tmp.add(x - i)
    dp_X = tmp

dp_Y = set()
dp_Y.add(sy)
for i in step_Y:
    tmp = set()
    for y in dp_Y:
        tmp.add(y + i)
        tmp.add(y - i)
    dp_Y = tmp

if X in dp_X and Y in dp_Y:
    print("Yes")
else:
    print("No")

Submission status: Accepted

Solution code:
s = input()
x, y = map(int, input().split())
n = len(s)
#dp_x: +, -に向いてて行けるかどうか
dp_x = [[False, False] for i in range(2*n+2)]
dp_y = [[False, False] for i in range(2*n+2)]
dp_x[0][0] = True
dp_y[0][0] = True
dp_y[0][1] = True

flag = 0
for i in range(len(s)):
    if s[i] == "T":
        if flag == 0:
            flag = 2
            for j in range(-i-1, i+2):
                if dp_x[j][0]:
                    dp_x[j][1] = True

        elif flag == 2:
            flag = 1
            for j in range(-i-1, i+2):
                dp_x[j][0] = dp_x[j][1] or dp_x[j][0]
                dp_x[j][1] = dp_x[j][1] or dp_x[j][0]

        else:
            flag = 2
            for j in range(-i-1, i+2):
                dp_y[j][0] = dp_y[j][1] or dp_y[j][0]
                dp_y[j][1] = dp_y[j][1] or dp_y[j][0]


    else:
        if flag == 0:
            for j in range(i+1, -i-2, -1):
                dp_x[j][0] = dp_x[j-1][0]

        elif flag == 1:
            for j in range(i+1, -i-2, -1):
                dp_x[j][0] = dp_x[j-1][0]
                dp_x[-j][1] = dp_x[-j+1][1]
        else:
            for j in range(i+1, -i-2, -1):
                dp_y[j][0] = dp_y[j-1][0]
                dp_y[-j][1] = dp_y[-j+1][1]


if dp_x[x][0] or dp_x[x][1]:
    if dp_y[y][0] or dp_y[y][1]:
        print("Yes")
    else:
        print("No")
else:
    print("No")

Submission status: Accepted

Solution code:
from itertools import product
s = input()
x, y = map(int,input().split())
L = [len(i) for i in s.split('T')]
X = {L[0]}
Y = {0}
for i in L[2::2]:
    X = {j + k for j, k in product(X, [-i, i])}
for i in L[1::2]:
    Y = {j + k for j, k in product(Y, [-i, i])}
if x in X and y in Y:
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
def main():
    s = input()
    x, y = map(int, input().split())
    d = {}
    d[True] = {0}
    d[False] = {0}
    xf = True
    t = 0
    first = True
    for i in s:
        if i == 'F':
            t += 1
        else:
            if first:
                d[xf] = set()
                d[xf].add(t)
                first = False
            else:
                tmp = set()
                for j in d[xf]:
                    tmp.add(j-t)
                    tmp.add(j+t)
                d[xf] = tmp
            xf = not xf
            t = 0
    if t == len(s):
        d[True] = {t}
        d[False] = {0}
    elif t > 0:
        tmp = set()
        for j in d[xf]:
            tmp.add(j-t)
            tmp.add(j+t)
        d[xf] = tmp
    if x in d[True] and y in d[False]:
        return True
    return False
if main():
    print('Yes')
else:
    print('No')

Submission status: Accepted

Solution code:
def main():
    S = input()
    X, Y = map(int, input().split())
    d = 0
    L = [[0], [0]]
    for s in S:
        if s == 'T':
            d = 1 - d
            if L[d][-1] != 0:
                L[d].append(0)
        else:
            L[d][-1] += 1
    xs, ys = set([L[0][0] if S[0] == 'F' else 0]), set([0])
    for i in L[0][1 if S[0] == 'F' else 0:]:
        xs = set(x + i for x in xs) | set(x - i for x in xs)
    for i in L[1]:
        ys = set(y + i for y in ys) | set(y - i for y in ys)
    return X in xs and Y in ys

print('Yes' if main() else 'No')

Submission status: Accepted

Solution code:
def main():
    s = input()
    x, y = map(int, input().split())
    d = {}
    d[True] = []
    d[False] = []
    xf = True
    t = 0
    for i in s:
        if i == 'F':
            t += 1
        else:
            d[xf].append(t)
            xf = not xf
            t = 0
    if t > 0:
        d[xf].append(t)
    dpx = set()
    dpx.add(d[True][0])
    for i in d[True][1:]:
        dnew = set()
        for j in dpx:
            dnew.add(-i+j)
            dnew.add(i+j)
        dpx = dnew
    if x not in dpx:
        return False
    dpy = set()
    dpy.add(0)
    for i in d[False]:
        dnew = set()
        for j in dpy:
            dnew.add(-i+j)
            dnew.add(i+j)
        dpy = dnew
    if y not in dpy:
        return False
    return True
if main():
    print('Yes')
else:
    print('No')

Submission status: Accepted

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

# [文字,その文字が連続する数]
from collections import deque
sumS = deque()
import itertools
for key,g in itertools.groupby(S):
  sumS.append([key,len(list(g))])
  
startx = 0 # Fで始まる場合はX軸の正の方向にあらかじめ進めて置く
# 最初のTが出るまではX軸正の方向に進む
if sumS[0][0] == "F":
  startx = sumS[0][1]
  sumS.popleft()

xdir = True # X軸方向を向いている
dpXcur = set()
dpXcur.add(startx)
dpYcur = set()
dpYcur.add(0) # Y軸は必ず0から始まる
dpXnex = set()
dpYnex = set()
for i in range(len(sumS)):
  if sumS[i][0] == "F":
    steps = sumS[i][1] # 進む数
    if xdir: # X軸方向を向いている場合
      for x in dpXcur:
        dpXnex.add(x + steps)
        dpXnex.add(x - steps)
      dpXcur = dpXnex
      dpXnex = set()
    else:
      for y in dpYcur:
        dpYnex.add(y + steps)
        dpYnex.add(y - steps)
      dpYcur = dpYnex
      dpYnex = set()
  elif sumS[i][0] == "T":
    turns = sumS[i][1] # 方向転換する数
    if turns % 2 == 1:
      # 奇数回の場合はX/Y軸の切り替え (偶数回の場合は何も起こらない)
      xdir ^= True
      
if X in dpXcur and Y in dpYcur:
  print("Yes")
else:
  print("No")

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]
## math ##
MOD=10**9+7
def divc(x,y): return -(-x//y)
def divf(x,y): return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers"""
    is_prime = [True] * MAX_NUM
    is_prime[0] = False
    is_prime[1] = False
    primes = []
    for i in range(MAX_NUM):
        if is_prime[i]:
            primes.append(i)
            for j in range(2*i, MAX_NUM, i):
                is_prime[j] = False
    return primes
## libs ##
from itertools import accumulate as acc
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left

#======================================================#

def main():
    s = IS()
    x, y = MII()
    ff = s.split('T')
    ffx = [len(f) for f in ff[::2]]
    ffy = [len(f) for f in ff[1::2]]

    nx = ffx.pop(0)
    ny = 0

    ffx.sort(reverse=True)
    ffy.sort(reverse=True)

    for fx in ffx:
        if x >= nx: nx += fx
        else: nx -= fx
    for fy in ffy:
        if y >= ny: ny += fy
        else: ny -= fy

    if nx == x and ny == y: print('Yes')
    else: print('No')

if __name__ == '__main__':
    main()