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: Wrong Answer

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

ver = []
hor = []
ini = 0
flag = 'h1'
tmp = 0

for i in range(len(s)):
    if s[i] == 'T':
        if flag == 'h':
            if tmp > 0:
                hor.append(tmp)
            tmp = 0
            flag = 'v'
        elif flag == 'v':
            if tmp > 0:
                ver.append(tmp)
            flag = 'h'
            tmp = 0
        elif flag == 'h1':
            if tmp > 0:
                ini = tmp
            flag = 'v'
            tmp = 0
    else:
        tmp += 1

if tmp > 0:
    if flag == 'h1':
        ini = tmp
    elif flag == 'h':
        hor.append(tmp)
    elif flag == 'v':
        ver.append(tmp)
        
ver_li = [0]*(8000*2+1)
hor_li = [0]*(8000*2+1)

hor_li[8000+ini] = 1

for i in range(len(hor)):
    c_hor = copy.deepcopy(hor_li)
    for j in range(8000*2+1):
        if hor_li[j] == 1:
            #print(hor[i])
            c_hor[hor[i]] = 0
            c_hor[j+hor[i]] = 1
            c_hor[j-hor[i]] = 1
    hor_li = c_hor

ver_li[8000] = 1

for i in range(len(ver)):
    c_ver = copy.deepcopy(ver_li)
    for j in range(8000*2+1):
        if ver_li[j] == 1:
            c_ver[ver[i]] = 0
            #print(hor[i])
            c_ver[j+ver[i]] = 1
            c_ver[j-ver[i]] = 1
    ver_li = c_ver
    
if hor_li[8000+x] == 1 and ver_li[8000+y] == 1:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

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

ver = []
hor = []
ini = 0
flag = 'h1'
tmp = 0

for i in range(len(s)):
    if s[i] == 'T':
        if flag == 'h':
            if tmp > 0:
                hor.append(tmp)
            tmp = 0
            flag = 'v'
        elif flag == 'v':
            if tmp > 0:
                ver.append(tmp)
            flag = 'h'
            tmp = 0
        elif flag == 'h1':
            if tmp > 0:
                ini = tmp
            flag = 'v'
            tmp = 0
    else:
        tmp += 1

if tmp > 0:
    if flag == 'h1':
        ini = tmp
    elif flag == 'h':
        hor.append(tmp)
    elif flag == 'v':
        ver.append(tmp)
        
ver_li = [0]*(8000*2+1)
hor_li = [0]*(8000*2+1)

hor_li[8000+ini] = 1

for i in range(len(hor)):
    c_hor = copy.deepcopy(hor_li)
    for j in range(8000*2+1):
        if hor_li[j] == 1:
            #print(hor[i])
            c_hor[j+hor[i]] = 1
            c_hor[j-hor[i]] = 1
    hor_li = c_hor

ver_li[8000] = 1

for i in range(len(ver)):
    c_ver = copy.deepcopy(ver_li)
    for j in range(8000*2+1):
        if ver_li[j] == 1:
            #print(hor[i])
            c_ver[j+ver[i]] = 1
            c_ver[j-ver[i]] = 1
    ver_li = c_ver
    
if hor_li[8000+x] == 1 and ver_li[8000+y] == 1:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
order = list(input())
X, Y = map(int, input().split())
 
Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
now_lis.append(cnt)

if order[0]=='T':
    x_start = 0
else:
    x_start = Xs[0]
    del Xs[0]

def dp(start, lis, goal):
    nodes = set([start])
    for x in lis:
        nx = set()
        L = list(nodes)
        for l in L:
            nx.add(l+x)
            nx.add(l-x)
        nodes = nx
    return goal in nodes
 
if dp(x_start, Xs, X) and dp(0, Ys, Y):
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
order = list(input())
X, Y = map(int, input().split())
 
Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        if not cnt==0:
            now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
if not cnt==0:
    now_lis.append(cnt)

if order[0]=='T':
    x_start = 0
else:
    x_start = Xs[0]
    del Xs[0]

def dp(start, lis, goal):
    M = sum(lis)+start
    length = len(lis)
    dp = [[0]*(M*2+1) for _ in range(length+1)]
    dp[0][M+start] = 1
    for i, x in enumerate(lis, 1):
        for j in range(M*2+1):
            if dp[i-1][j]==1:
                if not j+x>=M*2+1:
                    dp[i][j+x] = 1
                if not j-x<0:
                    dp[i][j-x] = 1
    if goal+M>=M*2+1 or goal+M-start<0:
        return 0
    return dp[-1][goal+M]
 
if dp(x_start, Xs, X) and dp(0, Ys, Y):
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
order = list(input())
X, Y = map(int, input().split())
 
Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        if not cnt==0:
            now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
if not cnt==0:
    now_lis.append(cnt)

if order[0]=='T':
    x_start = 0
else:
    x_start = Xs[0]
    del Xs[0]

def dp(start, lis, goal):
    M = sum(lis)+start
    length = len(lis)
    dp = [[0]*(M*2+1) for _ in range(length+1)]
    dp[0][M+start] = 1
    for i, x in enumerate(lis, 1):
        for j in range(M*2+1):
            if dp[i-1][j]==1:
                if not j+x>=M*2+1:
                    dp[i][j+x] = 1
                if not j-x<0:
                    dp[i][j-x] = 1
    if goal+M>=M*2+1 or goal+M<0:
        return 0
    return dp[-1][goal+M]
 
if dp(x_start, Xs, X) and dp(0, Ys, Y):
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
order = list(input())
X, Y = map(int, input().split())
 
Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        if not cnt==0:
            now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
if not cnt==0:
    now_lis.append(cnt)

def dp(length, lis, goal, isX):
    M = sum(lis)
    dp = [[0]*(M*2+1) for _ in range(length+1)]
    dp[0][M] = 1
    for i, x in enumerate(lis, 1):
        for j in range(M*2+1):
            if dp[i-1][j]==1:
                if not j+x>=M*2+1:
                    dp[i][j+x] = 1
                    if isX and i==1:
                        continue
                if not j-x<0:
                    dp[i][j-x] = 1
    if goal+M>=M*2+1 or goal+M<0:
        return 0
    return dp[-1][goal+M]
 
if dp(len(Xs), Xs, X, 1) and dp(len(Ys), Ys, Y, 0):
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
order = list(input())
X, Y = map(int, input().split())
 
Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        if not cnt==0:
            now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
if not cnt==0:
    now_lis.append(cnt)

def dp(length, lis, goal, isX):
    M = sum(lis)
    dp = [[0]*(M*2+1) for _ in range(length+1)]
    dp[0][M] = 1
    for i, x in enumerate(lis, 1):
        for j in range(M*2+1):
            if dp[i-1][j]==1:
                if not j+x>=M*2+1:
                    dp[i][j+x] = 1
                    if isX and i==1:
                        continue
                if j-x<0:
                    dp[i][j-x] = 1
    if goal+M>=M*2+1 or goal+M<0:
        return 0
    return dp[-1][goal+M]
 
if dp(len(Xs), Xs, X, 1) and dp(len(Ys), Ys, Y, 0):
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
order = list(input())
X, Y = map(int, input().split())
 
Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        if not cnt==0:
            now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
if not cnt==0:
    now_lis.append(cnt)

def dp(length, lis, goal, isX):
    M = sum(lis)
    dp = [[0]*(M*2+1) for _ in range(length+1)]
    dp[0][M] = 1
    for i, x in enumerate(lis, 1):
        for j in range(M*2):
            if dp[i-1][j]==1:
                if not (j+x>=M*2+1 or j+x<0):
                    dp[i][j+x] = 1
                    if isX and i==1:
                        continue
                if not (j-x<0 or j-x>=M*2+1):
                    dp[i][j-x] = 1
    if goal+M>=M*2+1 or goal+M<0:
        return 0
    return dp[-1][goal+M]
 
if dp(len(Xs), Xs, X, 1) and dp(len(Ys), Ys, Y, 0):
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
order = list(input())
X, Y = map(int, input().split())
 
Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        if not cnt==0:
            now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
if not cnt==0:
    now_lis.append(cnt)

def dp(length, lis, goal, isX):
    M = sum(lis)
    dp = [[0]*(M*2+1) for _ in range(length+1)]
    dp[0][M] = 1
    for i, x in enumerate(lis, 1):
        for j in range(M*2):
            if dp[i-1][j]==1:
                if not (j+x>=M*2+1 or j+x<0):
                    dp[i][j+x] = 1
                if not (j-x<0 or j-x>=M*2+1):
                    if not (isX and i==1):
                        dp[i][j-x] = 1
    if goal+M>=M*2+1 or goal+M<0:
        return 0
    return dp[-1][goal+M]
 
if dp(len(Xs), Xs, X, 1) and dp(len(Ys), Ys, Y, 0):
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
order = list(input())
X, Y = map(int, input().split())
 
Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        if not cnt==0:
            now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
if not cnt==0:
    now_lis.append(cnt)

def dp(length, lis, goal, isX):
    M = sum(lis)
    dp = [[0]*(M*2+1) for _ in range(length+1)]
    dp[0][M] = 1
    for i, x in enumerate(lis, 1):
        for j in range(M*2):
            if dp[i-1][j]==1:
                if not (j+x>=M*2+1 or j+x<0):
                    dp[i][j+x] = 1
                if not (j-x<0 or j-x>=M*2+1):
                    if not isX and i!=1:
                        dp[i][j-x] = 1
    if goal+M>=M*2+1 or goal+M<0:
        return 0
    return dp[-1][goal+M]
 
if dp(len(Xs), Xs, X, 1) and dp(len(Ys), Ys, Y, 0):
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
order = list(input())
X, Y = map(int, input().split())
 
Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        if not cnt==0:
            now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
if not cnt==0:
    now_lis.append(cnt)
lx = len(Xs)
ly = len(Ys)
 
def dp(length, lis, goal):
    M = sum(lis)*2
    dp = [[0]*(M*2+1) for _ in range(length+1)]
    dp[0][M] = 1
    for i, x in enumerate(lis, 1):
        for j in range(M*2+1):
            if dp[i-1][j]==1:
                if not (j+x>=M*2+1 or j+x<0):
                    dp[i][j+x] = 1
                if not (j-x<0 or j-x>=M*2+1):
                    dp[i][j-x] = 1
    if goal+M>=M*2+1 or goal+M<0:
        return 0
    return dp[-1][goal+M]
 
if dp(lx, Xs, X) and dp(ly, Ys, Y):
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
order = list(input())
X, Y = map(int, input().split())
 
Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        if not cnt==0:
            now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
if not cnt==0:
    now_lis.append(cnt)
lx = len(Xs)
ly = len(Ys)
 
def dp(length, lis, goal):
    M = sum(lis)
    dp = [[0]*(M*2+1) for _ in range(length+1)]
    dp[0][M] = 1
    for i, x in enumerate(lis, 1):
        for j in range(M*2+1):
            if dp[i-1][j]==1:
                if not (j+x>=M*2+1 or j+x<0):
                    dp[i][j+x] = 1
                if not (j-x<0 or j-x>=M*2+1):
                    dp[i][j-x] = 1
    if goal+M>=M*2+1 or goal+M<0:
        return 0
    return dp[-1][goal+M]
 
if dp(lx, Xs, X) and dp(ly, Ys, Y):
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
order = list(input())
X, Y = map(int, input().split())
 
Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        if not cnt==0:
            now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
if not cnt==0:
    now_lis.append(cnt)
lx = len(Xs)
ly = len(Ys)
 
def dp(length, lis, goal):
    M = sum(lis)
    dp = [[0]*(M*2+1) for _ in range(length+1)]
    dp[0][M] = 1
    for i, x in enumerate(lis, 1):
        for j in range(M*2):
            if dp[i-1][j]==1:
                if not (j+x>=M*2+1 or j+x<0):
                    dp[i][j+x] = 1
                if not (j-x<0 or j-x>=M*2+1):
                    dp[i][j-x] = 1
    if goal+M>=M*2+1 or goal+M<0:
        return 0
    return dp[-1][goal+M]
 
if dp(lx, Xs, X) and dp(ly, Ys, Y):
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

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

Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        if not cnt==0:
            now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
now_lis.append(cnt)
lx = len(Xs)
ly = len(Ys)

def dp(length, lis, goal):
    M = sum(lis)
    dp = [[0]*(M*2+1) for _ in range(length+1)]
    dp[0][M] = 1
    for i, x in enumerate(lis, 1):
        for j in range(M*2):
            if dp[i-1][j]==1:
                if not (j+x>=M*2+1 or j+x<0):
                    dp[i][j+x] = 1
                if not (j-x<0 or j-x>=M*2+1):
                    dp[i][j-x] = 1
    if goal+M>=M*2+1 or goal+M<0:
        return 0
    return dp[-1][goal+M]

if dp(lx, Xs, X) and dp(ly, Ys, Y):
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

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

Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
now_lis.append(cnt)
lx = len(Xs)
ly = len(Ys)

def dp(length, lis, goal):
    M = sum(lis)
    dp = [[0]*(M*2+1) for _ in range(length+1)]
    dp[0][M] = 1
    for i, x in enumerate(lis, 1):
        for j in range(M*2):
            if dp[i-1][j]==1:
                if not (j+x>=M*2+1 or j+x<0):
                    dp[i][j+x] = 1
                if not (j-x<0 or j-x>=M*2+1):
                    dp[i][j-x] = 1
    if goal+M>=M*2+1 or goal+M<0:
        return 0
    return dp[-1][goal+M]

if dp(lx, Xs, X) and dp(ly, Ys, Y):
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

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

Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
now_lis.append(cnt)
lx = len(Xs)
ly = len(Ys)
xdp = [[0]*16000 for _ in range(lx+1)]
ydp = [[0]*16000 for _ in range(ly+1)]
xdp[0][8000] = 1
ydp[0][8000] = 1

def dp(dp, lis, goal):
    for i, x in enumerate(lis, 1):
        for j in range(16000):
            if dp[i-1][j]==1:
                if not j+x>=16000:
                    dp[i][j+x] = 1
                if not j-x<0:
                    dp[i][j-x] = 1
    return dp[-1][goal+4000]

if dp(xdp, Xs, X) and dp(ydp, Ys, Y):
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

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

import sys
# import time
# import math
# import numpy as np
# import scipy.sparse.csgraph as cs            # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, return_predecessors=True), dijkstra, floyd_warshall
# import random                                # random, uniform, randint, randrange, shuffle, sample
# import string                                # ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from datetime import date, datetime          # date.today(), date(year,month,day) => date obj; datetime.now(), datetime(year,month,day,hour,second,microsecond) => datetime obj; subtraction => timedelta obj
# from datetime.datetime import strptime       # strptime('2019/01/01 10:05:20', '%Y/%m/%d/ %H:%M:%S') returns datetime obj
# from datetime import timedelta               # td.days, td.seconds, td.microseconds, td.total_seconds(). abs function is also available.
from copy import copy, deepcopy              # use deepcopy to copy multi-dimentional matrix without reference
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from fractions import gcd                    # for Python 3.4 (previous contest @AtCoder)



def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79...e+308
    # inf = 2 ** 64 - 1             # (for fast JIT compile in PyPy) 1.84...e+19
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())


    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]
                tmp = copy(possible_num_set)
                for candidate in tmp:
                    possible_num_set.add(candidate + num)
                    possible_num_set.add(candidate - num)
        # 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: Wrong Answer

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 = [d[0]]
now_y = [0]

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

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

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

Submission status: Wrong Answer

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]

x -= d[0]

now_x = [0]
now_y = [0]

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

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

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

Submission status: Wrong Answer

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

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

li = []

for key, value in groupby(s):
    li.append((key,len(list(value))))

x_li = []
y_li = []

now = 0
for i in li:
    if i[0] == "F":
        if now%2 == 0:
            x_li.append(i[1])
        else:
            y_li.append(i[1])
    else:
        now += i[1]

x_flag = False
y_flag = False

def possible(n,now,final,li):
    global x_flag,y_flag
    if n == len(li):
        if now == final:
            if li == x_li:
                x_flag = True
            else:
                y_flag = True
    else:
        return possible(n+1,now-li[n],final,li),possible(n+1,now+li[n],final,li)

possible(0,0,x,x_li)
possible(0,0,y,y_li)

if x_flag and y_flag:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

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

susumu = s.count("F")
if susumu % 2 != (x + y) % 2:
    print("No")
    exit()
if susumu < (x + y):
    print("No")
    exit()
x_houkou = True
cnt = 0
X = []
Y = []
for w in s:
    if w == "F":
        cnt += 1
    else:
        if x_houkou:
            X.append(cnt)
            cnt = 0
            x_houkou = False
        else:
            Y.append(cnt)
            cnt = 0
            x_houkou = True
if cnt > 0:
    if x_houkou:
        X.append(cnt)
    else:
        Y.append(cnt)

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


x_ok = False
y_ok = False
if not X:
    if x == 0:
        x_ok = True
    else:
        print("No")
        exit()
if not Y:
    if y == 0:
        y_ok = True
    else:
        print("No")
        exit()

# x = abs(x)
# y = abs(y)
x_num = len(X)
y_num = len(Y)
if not x_ok:
    dp1 = [0] * (2 * 8000 + 1)
    dp2 = [0] * (2 * 8000 + 1)
    dp1[8000] = 1
    for j in range(x_num):
        if j % 2 == 0:
            t = X[j]
            for i in range(2 * 8000, -1, -1):
                if dp1[i] == 1:
                    if i + t <= 2 * 8000:
                        dp2[i + t] = 1
                    if i - t >= 0:
                        dp2[i + t] = 1
        else:
            t = X[j]
            for i in range(2 * 8000, -1, -1):
                if dp2[i] == 1:
                    if i + t <= 2 * 8000:
                        dp1[i + t] = 1
                    if i - t >= 0:
                        dp1[i + t] = 1
    if x_num % 2 == 1:
        x_ok += dp2[8000 + x]
    else:
        x_ok += dp1[8000 + x]

if not y_ok:
    dp1 = [0] * (2 * 8000 + 1)
    dp2 = [0] * (2 * 8000 + 1)
    dp1[8000] = 1
    for j in range(y_num):
        if j % 2 == 0:
            t = Y[j]
            for i in range(2 * 8000, -1, -1):
                if dp1[i] == 1:
                    if i + t <= 2 * 8000:
                        dp2[i + t] = 1
                    if i - t >= 0:
                        dp2[i + t] = 1
        else:
            t = Y[j]
            for i in range(2 * 8000, -1, -1):
                if dp2[i] == 1:
                    if i + t <= 2 * 8000:
                        dp1[i + t] = 1
                    if i - t >= 0:
                        dp1[i + t] = 1
    if y_num % 2 == 1:
        y_ok += dp2[8000 + y]
    else:
        y_ok += dp1[8000 + y]

if x_ok and y_ok:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

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

# susumu = s.count("F")
# if susumu % 2 != (x + y) % 2:
#     print("No")
#     exit()
# if susumu < (x + y):
#     print("No")
#     exit()
x_houkou = True
cnt = 0
X = []
Y = []
for w in s:
    if w == "F":
        cnt += 1
    else:
        if x_houkou:
            X.append(cnt)
            cnt = 0
            x_houkou = False
        else:
            Y.append(cnt)
            cnt = 0
            x_houkou = True
if cnt > 0:
    if x_houkou:
        X.append(cnt)
    else:
        Y.append(cnt)

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


x_ok = False
y_ok = False
if not X:
    if x == 0:
        x_ok = True
    else:
        print("No")
        exit()
if not Y:
    if y == 0:
        y_ok = True
    else:
        print("No")
        exit()

# x = abs(x)
# y = abs(y)
x_num = len(X)
y_num = len(Y)
if not x_ok:
    dp1 = [0] * (2 * 8000 + 1)
    dp2 = [0] * (2 * 8000 + 1)
    dp1[8000] = 1
    for j in range(x_num):
        if j % 2 == 0:
            t = X[j]
            for i in range(2 * 8000, -1, -1):
                if dp1[i] == 1:
                    if i + t <= 2 * 8000:
                        dp2[i + t] = 1
                    if i - t >= 0:
                        dp2[i + t] = 1
        else:
            t = X[j]
            for i in range(2 * 8000, -1, -1):
                if dp2[i] == 1:
                    if i + t <= 2 * 8000:
                        dp1[i + t] = 1
                    if i - t >= 0:
                        dp1[i + t] = 1
    if x_num % 2 == 1:
        x_ok += dp2[8000 + x]
    else:
        x_ok += dp1[8000 + x]

if not y_ok:
    dp1 = [0] * (2 * 8000 + 1)
    dp2 = [0] * (2 * 8000 + 1)
    dp1[8000] = 1
    for j in range(y_num):
        if j % 2 == 0:
            t = Y[j]
            for i in range(2 * 8000, -1, -1):
                if dp1[i] == 1:
                    if i + t <= 2 * 8000:
                        dp2[i + t] = 1
                    if i - t >= 0:
                        dp2[i + t] = 1
        else:
            t = Y[j]
            for i in range(2 * 8000, -1, -1):
                if dp2[i] == 1:
                    if i + t <= 2 * 8000:
                        dp1[i + t] = 1
                    if i - t >= 0:
                        dp1[i + t] = 1
    if y_num % 2 == 1:
        y_ok += dp2[8000 + y]
    else:
        y_ok += dp1[8000 + y]

if x_ok and y_ok:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

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

# susumu = s.count("F")
# if susumu % 2 != (x + y) % 2:
#     print("No")
#     exit()
# if susumu < (x + y):
#     print("No")
#     exit()
x_houkou = True
cnt = 0
X = []
Y = []
for w in s:
    if w == "F":
        cnt += 1
    else:
        if x_houkou:
            X.append(cnt)
            cnt = 0
            x_houkou = False
        else:
            Y.append(cnt)
            cnt = 0
            x_houkou = True
if cnt > 0:
    if x_houkou:
        X.append(cnt)
    else:
        Y.append(cnt)

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


x_ok = False
y_ok = False
if not X:
    if x == 0:
        x_ok = True
    else:
        print("No")
        exit()
if not Y:
    if y == 0:
        y_ok = True
    else:
        print("No")
        exit()

x = abs(x)
y = abs(y)
x_num = len(X)
y_num = len(Y)
if not x_ok:
    dp1 = [0] * (2 * 8000 + 1)
    dp2 = [0] * (2 * 8000 + 1)
    dp1[8000] = 1
    for j in range(x_num):
        if j % 2 == 0:
            t = X[j]
            for i in range(2 * 8000, -1, -1):
                if dp1[i] == 1:
                    if i + t <= 2 * 8000:
                        dp2[i + t] = 1
                    if i - t >= 0:
                        dp2[i + t] = 1
        else:
            t = X[j]
            for i in range(2 * 8000, -1, -1):
                if dp2[i] == 1:
                    if i + t <= 2 * 8000:
                        dp1[i + t] = 1
                    if i - t >= 0:
                        dp1[i + t] = 1
    if x_num % 2 == 1:
        x_ok += dp2[8000 + x]
    else:
        x_ok += dp1[8000 + x]

if not y_ok:
    dp1 = [0] * (2 * 8000 + 1)
    dp2 = [0] * (2 * 8000 + 1)
    dp1[8000] = 1
    for j in range(y_num):
        if j % 2 == 0:
            t = Y[j]
            for i in range(2 * 8000, -1, -1):
                if dp1[i] == 1:
                    if i + t <= 2 * 8000:
                        dp2[i + t] = 1
                    if i - t >= 0:
                        dp2[i + t] = 1
        else:
            t = Y[j]
            for i in range(2 * 8000, -1, -1):
                if dp2[i] == 1:
                    if i + t <= 2 * 8000:
                        dp1[i + t] = 1
                    if i - t >= 0:
                        dp1[i + t] = 1
    if y_num % 2 == 1:
        y_ok += dp2[8000 + y]
    else:
        y_ok += dp1[8000 + y]

if x_ok and y_ok:
    print("Yes")
else:
    print("No")