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

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


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

    X = []
    Y = []
    flag = 0
    for s in S:
        if flag == 0:
            X.append(len(s))
        else:
            Y.append(len(s))
        flag ^= 1

    A = set()
    A.add(X[0])
    for i in X[1:]:
        tmp = set()
        for a in A:
            tmp.add(a + i)
            tmp.add(a - i)
        A = tmp

    B = set()
    B.add(0)
    for j in Y:
        tmp = set()
        for b in B:
            tmp.add(b + j)
            tmp.add(b - j)
        B = tmp

    if x in A and y in B:
        print('Yes')
    else:
        print('No')


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


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

    X = []
    Y = []
    flag = 0
    for s in S:
        if flag == 0:
            X.append(len(s))
        else:
            Y.append(len(s))
        flag ^= 1

    A = set()
    A.add(X[0])
    for i in X[1:]:
        tmp = set()
        for a in A:
            tmp.add(a + i)
            tmp.add(a - i)
        A = tmp

    B = set()
    B.add(0)
    for j in Y:
        tmp = set()
        for b in B:
            tmp.add(b + j)
            tmp.add(b - j)
        B = tmp

    if x in A and y in B:
        print('Yes')
    else:
        print('No')


if __name__ == '__main__':
    main()

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


steps = []
cnt = 0
for i in range(len(s)):
    if s[i] == "F":
        cnt += 1
    elif s[i] == "T":
        steps.append(cnt)
        cnt = 0

if cnt > 0:
    steps.append(cnt)

step_X = steps[0::2]
step_Y = steps[1::2]


max_X = sum(step_X)



dp_X = [[0 for _ in range(2 * max_X + 1)] for _ in range(len(step_X) + 1)]
dp_X[0][max_X] = 1

if s[0] == "F":
    dp_X[1][max_X + step_X[0]] = 1
    for i in range(1, len(step_X)):
        for j in range(2 * max_X + 1):
            if dp_X[i][j] == 1:
                
                if j + step_X[i] <= 2 * max_X:
                    dp_X[i + 1][j + step_X[i]] = 1
                
                if 0 <= j - step_X[i]:
                    dp_X[i + 1][j - step_X[i]] = 1

else:
    for i in range(len(step_X)):
        for j in range(2 * max_X + 1):
            if dp_X[i][j] == 1:
                
                if j + step_X[i] <= 2 * max_X:
                    dp_X[i + 1][j + step_X[i]] = 1
                
                if 0 <= j - step_X[i]:
                    dp_X[i + 1][j - step_X[i]] = 1


max_Y = sum(step_Y)



dp_Y = [[0 for _ in range(2 * max_Y + 1)] for _ in range(len(step_Y) + 1)]
dp_Y[0][max_Y] = 1
for i in range(len(step_Y)):
    for j in range(2 * max_Y + 1):
        if dp_Y[i][j] == 1:
            
            if j + step_Y[i] <= 2 * max_Y:
                dp_Y[i + 1][j + step_Y[i]] = 1
            
            if 0 <= j - step_Y[i]:
                dp_Y[i + 1][j - step_Y[i]] = 1

if -max_X <= x <= max_X and -max_Y <= y <= max_Y:
    if dp_X[-1][x + max_X] == 1 and dp_Y[-1][y + max_Y] == 1:
        print("Yes")
        exit()
print("No")

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


steps = []
cnt = 0
for i in range(len(s)):
    if s[i] == "F":
        cnt += 1
    elif s[i] == "T":
        steps.append(cnt)
        cnt = 0

if cnt > 0:
    steps.append(cnt)
AX = steps[0::2]
AY = steps[1::2]


max_X = sum(AX)



dpX = [[0 for _ in range(2 * max_X + 1)] for _ in range(len(AX) + 1)]
dpX[0][max_X] = 1

if s[0] == "F":
    dpX[1][max_X + AX[0]] = 1
    for i in range(1, len(AX)):
        for j in range(2 * max_X + 1):
            if dpX[i][j] == 1:
                
                if j + AX[i] <= 2 * max_X:
                    dpX[i + 1][j + AX[i]] = 1
                
                if 0 <= j - AX[i]:
                    dpX[i + 1][j - AX[i]] = 1

else:
    for i in range(len(AX)):
        for j in range(2 * max_X + 1):
            if dpX[i][j] == 1:
                
                if j + AX[i] <= 2 * max_X:
                    dpX[i + 1][j + AX[i]] = 1
                
                if 0 <= j - AX[i]:
                    dpX[i + 1][j - AX[i]] = 1

max_Y = sum(AY)



dpY = [[0 for _ in range(2 * max_Y + 1)] for _ in range(len(AY) + 1)]
dpY[0][max_Y] = 1
for i in range(len(AY)):
    for j in range(2 * max_Y + 1):
        if dpY[i][j] == 1:
            
            if j + AY[i] <= 2 * max_Y:
                dpY[i + 1][j + AY[i]] = 1
            
            if 0 <= j - AY[i]:
                dpY[i + 1][j - AY[i]] = 1

if -max_X <= x <= max_X and -max_Y <= y <= max_Y:
    if dpX[-1][x + max_X] == 1 and dpY[-1][y + max_Y] == 1:
        print("Yes")
        exit()
print("No")

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


steps = []
cnt = 0
for i in range(len(s)):
    if s[i] == "F":
        cnt += 1
    elif s[i] == "T":
        steps.append(cnt)
        cnt = 0

if cnt > 0:
    steps.append(cnt)
AX = steps[0::2]
AY = steps[1::2]


max_X = sum(AX)



dpX = [[0 for _ in range(2 * max_X + 1)] for _ in range(len(AX) + 1)]
dpX[0][max_X] = 1

if s[0] == "F":
    dpX[1][max_X + AX[0]] = 1
    for i in range(1, len(AX)):
        for j in range(2 * max_X + 1):
            if dpX[i][j] == 1:
                
                if j + AX[i] <= 2 * max_X:
                    dpX[i + 1][j + AX[i]] = 1
                
                if 0 <= j - AX[i]:
                    dpX[i + 1][j - AX[i]] = 1

else:
    for i in range(len(AX)):
        for j in range(2 * max_X + 1):
            if dpX[i][j] == 1:
                
                if j + AX[i] <= 2 * max_X:
                    dpX[i + 1][j + AX[i]] = 1
                
                if 0 <= j - AX[i]:
                    dpX[i + 1][j - AX[i]] = 1

max_Y = sum(AY)



dpY = [[0 for _ in range(2 * max_Y + 1)] for _ in range(len(AY) + 1)]
dpY[0][max_Y] = 1
for i in range(len(AY)):
    for j in range(2 * max_Y + 1):
        if dpY[i][j] == 1:
            
            if j + AY[i] <= 2 * max_Y:
                dpY[i + 1][j + AY[i]] = 1
            
            if 0 <= j - AY[i]:
                dpY[i + 1][j - AY[i]] = 1

if -max_X <= x <= max_X and -max_Y <= y <= max_Y:
    if dpX[-1][x + max_X] == 1 and dpY[-1][y + max_Y] == 1:
        print("Yes")
        exit()
print("No")

Solution (Python):
def YesNo(b):
    print("Yes" if b else "No")


def solve(l, t0, t):
    s = {t0}
    for v in l:
        s, ps = set(), s
        for a in ps:
            s.add(a+v)
            s.add(a-v)
    return t in s


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

f = s.split('T')
x0 = len(f[0])
xl = [len(v) for v in f[2::2]]
yl = [len(v) for v in f[1::2]]

YesNo(solve(xl, x0, x) and solve(yl, 0, y))

Solution (Python):

def YesNo(b):
    print("Yes" if b else "No")


def solve(v, l, ini):
    ml = 8000*2+1
    dp = [[False]*ml for i in range(len(l)+1)]
    for i in range(len(ini)):
        dp[0][ini[i]+8000] = True
    for i in range(1, len(l)+1):
        for j in range(ml):
            if (j+l[i-1]) < ml and dp[i-1][j]:
                dp[i][j+l[i-1]] = True
            if (j-l[i-1]) >= 0 and dp[i-1][j]:
                dp[i][j-l[i-1]] = True
    return dp[len(l)][v+8000]


s = input()
x, y = map(int, input().split())
si = 0
while si < len(s) and s[si] == 'F':
    si += 1

s = s[si:]
if len(s) == 0:
    YesNo(x == si and y == 0)
else:
    f = s.split('T')[1:]
    xl, yl = [0], [0]
    for i in range(len(f)):
        if len(f[i]) == 0:
            continue
        if i % 2 == 0:
            yl.append(len(f[i]))
        else:
            xl.append(len(f[i]))

    YesNo(solve(x, xl, [si]) and solve(y, yl[1:], [-yl[0], yl[0]]))

Solution (Python):

import sys
sys.setrecursionlimit(10**6)

s = str(input())
obj_x, obj_y = list(map(int, input().split()))


x = []
y = []
flag = 0
num = 0
for i in range(len(s)):
    if s[i] == "T":
        if flag % 2 == 0:
            x.append(num)
        else:
            y.append(num)
        flag += 1
        num = 0
    else:
        num += 1

if num > 0:
    if flag % 2 == 0:
        x.append(num)
    else:
        y.append(num)


if x == []:
    x = [0]
if y == []:
    y = [0]


num_x = sum(x)
num_y = sum(y)

max_x = num_x*2+1
max_y = num_y*2+1

dp_x = [[0]*((len(x)+1)) for i in range(max_x)]
dp_y = [[0]*((len(y)+1)) for i in range(max_y)]

dp_x[num_x][0] = 1
dp_y[num_y][0] = 1


dp_x[num_x+x[0]][1]=1



for i in range(1, len(x)):
    num = x[i]
    for j in range(0, len(dp_x)):
        if j+num < max_x:
            dp_x[j+num][i+1] = max(dp_x[j+num][i+1], dp_x[j][i]*1)
        if j-num >= 0:
            dp_x[j-num][i+1] = max(dp_x[j-num][i+1], dp_x[j][i]*1)
        
        
for i in range(0, len(y)):
    num = y[i]
    for j in range(0, len(dp_y)):
        if j+num < max_y:
            dp_y[j+num][i+1] = max(dp_y[j+num][i+1], dp_y[j][i]*1)
        if j-num >= 0:
            dp_y[j-num][i+1] = max(dp_y[j-num][i+1], dp_y[j][i]*1)
        
        


if (num_x+obj_x)<len(dp_x) and (num_x+obj_x)>=0:
    ans_x = dp_x[num_x+obj_x][-1]
else:
    print("No")
    exit()
if (num_y+obj_y)<len(dp_y) and (num_y+obj_y)>=0:
    ans_y = dp_y[num_y+obj_y][-1]
else:
    print("No")
    exit()


if ans_x*ans_y == 1:
    print("Yes")
else:
    print("No")

Solution (Python):
s = input().strip()
x,y = map(int,input().split())
AX = []
AY = []
flag = 0
cnt = 0
for i in range(len(s)):
    if s[i]=="F":
        cnt += 1
    else:
        if flag==0:
            if cnt>0:
                AX.append(cnt)
                cnt = 0
            flag = 1
        else:
            if cnt>0:
                AY.append(cnt)
                cnt = 0
            flag = 0
if cnt>0 and flag==0:
    AX.append(cnt)
if cnt>0 and flag==1:
    AY.append(cnt)
Nx = sum(AX)
dpX = [[0 for _ in range(2*Nx+1)] for _ in range(len(AX)+1)]
dpX[0][Nx] = 1
if s[0]=="F":
    dpX[1][Nx+AX[0]] = 1
    for i in range(1,len(AX)):
        for j in range(2*Nx+1):
            if j+AX[i]<=2*Nx:
                dpX[i+1][j+AX[i]] = dpX[i+1][j+AX[i]] or dpX[i][j]
            if 0<=j-AX[i]:
                dpX[i+1][j-AX[i]] = dpX[i+1][j-AX[i]] or dpX[i][j]
else:
    for i in range(len(AX)):
        for j in range(2*Nx+1):
            if j+AX[i]<=2*Nx:
                dpX[i+1][j+AX[i]] = dpX[i+1][j+AX[i]] or dpX[i][j]
            if 0<=j-AX[i]:
                dpX[i+1][j-AX[i]] = dpX[i+1][j-AX[i]] or dpX[i][j]
Ny = sum(AY)
dpY = [[0 for _ in range(2*Ny+1)] for _ in range(len(AY)+1)]
dpY[0][Ny] = 1
for i in range(len(AY)):
    for j in range(2*Ny+1):
        if j+AY[i]<=2*Ny:
            dpY[i+1][j+AY[i]] = dpY[i+1][j+AY[i]] or dpY[i][j]
        if 0<=j-AY[i]:
            dpY[i+1][j-AY[i]] = dpY[i+1][j-AY[i]] or dpY[i][j]
if Nx==0 and x!=0 or Ny==0 and y!=0:
    print("No")
elif abs(x)>Nx or abs(y)>Ny:
    print("No")
elif dpX[len(AX)][Nx+x]==1 and dpY[len(AY)][Ny+y]==1:
    print("Yes")
else:
    print("No")

Solution (Python):
s = str(input()); n = len(s)
gx,gy = map(int,input().split())
Yoko = []; Tate = []
Flag = 0; cnt = 0
Max_x = 0; Max_y = 0
for i in range(n):
  if s[i] == "F":
    cnt += 1
    if i == n-1: 
      if Flag == 0:
        Max_x += cnt
        Yoko.append(cnt)
      else:
        Max_y += cnt
        Tate.append(cnt)      
  else: 
    if Flag == 0:
      Max_x += cnt
      Yoko.append(cnt)
    else:
      Max_y += cnt
      Tate.append(cnt)
    cnt = 0
    Flag = Flag^1

MAX_dpx = Max_x*2+1
dp = [0 for _ in range(MAX_dpx)]
dp[Max_x] = 1

for i,x in enumerate(Yoko):
  p = [0 for _ in range(MAX_dpx)]
  p,dp = dp,p
  for j in range(MAX_dpx):
    if i == 0: 
      if 0<=j+x<MAX_dpx:
        dp[j+x] = max(dp[j+x], p[j])
    else:
      if 0<=j+x<MAX_dpx:
        dp[j+x] = max(dp[j+x], p[j])
      if 0<=j-x<MAX_dpx:
        dp[j-x] = max(dp[j-x], p[j])
  


if gx+Max_x >= MAX_dpx or gx+Max_x < 0 or dp[gx+Max_x] == 0:
  print("No");exit()

MAX_dpy = Max_y*2+1
dp = [0 for _ in range(MAX_dpy)]
dp[Max_y] = 1

for i,y in enumerate(Tate):
  p = [0 for _ in range(MAX_dpy)]
  p,dp = dp,p
  for j in range(MAX_dpy):
    if 0<=j+y<MAX_dpy:
      dp[j+y] = max(dp[j+y], p[j])
    if 0<=j-y<MAX_dpy:
      dp[j-y] = max(dp[j-y], p[j])
  

if gy+Max_y >= MAX_dpy or gy+Max_y < 0 or dp[gy+Max_y] == 0:
  print("No")
else:
  print("Yes")

Solution (Python):
import sys
S = list(input())
x, y = map(int, input().split())
c = 0
p = 0
q = 0
now = 0
R = []
L = []
for i in range(len(S)):
    if S[i] == "T":
        if p != 0:
            R.append(p)
            p = 0
        elif q != 0:
            L.append(q)
            q = 0
        c += 1
    elif c == 0:
        now += 1
    elif c % 2 == 0:
        p += 1
    else:
        q += 1
if p != 0:
    R.append(p)
if q != 0:
    L.append(q)

A = 2*(sum(R)+now)
B = 2*(sum(L))
dp_x = [[0]*(A+2) for _ in range(len(R)+2)]
dp_y = [[0]*(B+2) for _ in range(len(L)+1)]
c = 0
dp_x[0][sum(R)+now] = 1
dp_y[0][sum(L)] = 1
for i in range(1, len(R)+2):
    for j in range(A+2):
        if i == 1:
            if j - now >= 0:
                if dp_x[i-1][j-now] == 1:
                    dp_x[i][j] = 1
        else:
            if j - R[i-2] >= 0:
                if dp_x[i-1][j-R[i-2]] == 1:
                    dp_x[i][j] = 1
            if j + R[i-2] <= A+1:
                if dp_x[i-1][j+R[i-2]] == 1:
                    dp_x[i][j] = 1

for i in range(1, len(L)+1):
    for j in range(B+2):
        if j - L[i-1] >= 0:
            if dp_y[i-1][j-L[i-1]] == 1:
                dp_y[i][j] = 1
        if j + L[i-1] <= B+1:
            if dp_y[i-1][j+L[i-1]] == 1:
                dp_y[i][j] = 1

if A + 1 < x+sum(R)+now or x+sum(R)+now < 0:
    print("No")
    sys.exit()

if B + 1 < y+sum(L) or y+sum(L) < 0:
    print("No")
    sys.exit()

if dp_x[len(R)+1][x+sum(R)+now] == 1 and dp_y[len(L)][y+sum(L)] == 1:
    print("Yes")
else:
    print("No")

Solution (Python):
import numpy as np
MOD = 10**9 + 7

def fft_convolve(f, g):
  Lf, Lg = f.shape[-1], g.shape[-1]
  L = Lf + Lg - 1
  fft_len = 1 << L.bit_length()
  fh, fl = f >> 15, f & (1 << 15) - 1
  gh, gl = g >> 15, g & (1 << 15) - 1
  def conv(f, g):
    Ff = np.fft.rfft(f, fft_len)
    Fg = np.fft.rfft(g, fft_len)
    h = np.fft.irfft(Ff * Fg)
    return np.rint(h)[..., :L].astype(np.int64) % MOD
  x = conv(fl, gl)
  z = conv(fh, gh)
  y = conv(fl + fh, gl + gh) - x - z
  return (x + (y << 15) + (z << 30)) % MOD

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

Rob = list(s.split("T"))

X = [len(r) for r in Rob[::2]]
Y = [len(r) for r in Rob[1::2]]

Xst = X[0]
X = X[1:]

if abs(x-Xst) <= sum(X) and abs(y) <= sum(Y):
  f = np.array([1], np.int64)
  for i in X:
    L = [0]*(2*i+1)
    L[0], L[-1] = 1, 1
    f = np.convolve(f, np.array(L, np.int64))
  FlagX = f[abs(x-Xst)+sum(X)]

  g = np.array([1], np.int64)
  for j in Y:
    L = [0]*(2*j+1)
    L[0], L[-1] = 1, 1
    g = np.convolve(g, np.array(L, np.int64))
  FlagY = g[abs(y)+sum(Y)]

  if FlagX and FlagY:
    print("Yes")
  else:
    print("No")
else:
  print("No")

Solution (Python):
import numpy as np
MOD = 10**9 + 7

def fft_convolve(f, g):
  Lf, Lg = f.shape[-1], g.shape[-1]
  L = Lf + Lg - 1
  fft_len = 1 << L.bit_length()
  fh, fl = f >> 15, f & (1 << 15) - 1
  gh, gl = g >> 15, g & (1 << 15) - 1
  def conv(f, g):
    Ff = np.fft.rfft(f, fft_len)
    Fg = np.fft.rfft(g, fft_len)
    h = np.fft.irfft(Ff * Fg)
    return np.rint(h)[..., :L].astype(np.int64) % MOD
  x = conv(fl, gl)
  z = conv(fh, gh)
  y = conv(fl + fh, gl + gh) - x - z
  return (x + (y << 15) + (z << 30)) % MOD

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

Rob = list(s.split("T"))

X = [len(r) for r in Rob[::2]]
Y = [len(r) for r in Rob[1::2]]

def solve():
  global X
  Xst = X[0]
  X = X[1:]
  if abs(x-Xst) <= sum(X) and abs(y) <= sum(Y):
    f = np.array([1], np.int64)
    for i in X:
      L = [0]*(2*i+1)
      L[0], L[-1] = 1, 1
      f = np.convolve(f, np.array(L, np.int64))
    FlagX = f[abs(x-Xst)+sum(X)]
    g = np.array([1], np.int64)
    for j in Y:
      L = [0]*(2*j+1)
      L[0], L[-1] = 1, 1
      g = np.convolve(g, np.array(L, np.int64))
    FlagY = g[abs(y)+sum(Y)]
    if FlagX and FlagY:
      print("Yes")
    else:
      print("No")
  else:
    print("No")

solve()

Solution (Python):
import numpy as np
MOD = 10**9 + 7

def fft_convolve(f, g):
  Lf, Lg = f.shape[-1], g.shape[-1]
  L = Lf + Lg - 1
  fft_len = 1 << L.bit_length()
  fh, fl = f >> 15, f & (1 << 15) - 1
  gh, gl = g >> 15, g & (1 << 15) - 1
  def conv(f, g):
    Ff = np.fft.rfft(f, fft_len)
    Fg = np.fft.rfft(g, fft_len)
    h = np.fft.irfft(Ff * Fg)
    return np.rint(h)[..., :L].astype(np.int64) % MOD
  x = conv(fl, gl)
  z = conv(fh, gh)
  y = conv(fl + fh, gl + gh) - x - z
  return (x + (y << 15) + (z << 30)) % MOD

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

Rob = list(s.split("T"))

X = [len(r) for r in Rob[::2]]
Y = [len(r) for r in Rob[1::2]]

if x < 0:
  y = abs(y)
  Xst = X[0]
  X = sorted(X[1:])
  if abs(x-Xst) <= sum(X) and y <= sum(Y):
    f = np.array([1], np.int64)
    for i in X:
      L = [0]*(2*i+1)
      L[0], L[-1] = 1, 1
      f = np.convolve(f, np.array(L, np.int64))
    FlagX = f[abs(x-Xst)+sum(X)]
    g = np.array([1], np.int64)
    for j in Y:
      L = [0]*(2*j+1)
      L[0], L[-1] = 1, 1
      g = np.convolve(g, np.array(L, np.int64))
    FlagY = g[y+sum(Y)]
    if FlagX and FlagY:
      print("Yes")
    else:
      print("No")
  else:
    print("No")
else:
  x, y = abs(x), abs(y)
  if x <= sum(X) and y <= sum(Y):
    f = np.array([1], np.int64)
    for i in X:
      L = [0]*(2*i+1)
      L[0], L[-1] = 1, 1
      f = np.convolve(f, np.array(L, np.int64))
    FlagX = f[x+sum(X)]
    g = np.array([1], np.int64)
    for j in Y:
      L = [0]*(2*j+1)
      L[0], L[-1] = 1, 1
      g = np.convolve(g, np.array(L, np.int64))
    FlagY = g[y+sum(Y)]
    if FlagX and FlagY:
      print("Yes")
    else:
      print("No")
  else:
    print("No")

Solution (Python):
from collections import defaultdict
def main():
    S = input() + 'T'
    x, y = map(int, input().split())
    
    moveX, moveY = [], []
    if S[0] == 'T': moveX.append(0)
    move ={0:moveX, 1:moveY}
    direct = 0
    step = 0
    for s in S:
        if s == 'T':
            if step > 0:
                move[direct].append(step)
                step = 0
            direct ^= 1 
        else:
            step += 1
    
    if possible(x, moveX[1:], moveX[0]) and possible(y, moveY):
        print('Yes')
    else:
        print('No')


def possible(target, move, offset=0):
    l = len(move)
    dp = [defaultdict(lambda:False) for _ in range(l+1)]
    
    dp[0][0] = True
    for i,m in enumerate(move):
        for key in dp[i]:
            dp[i+1][key+m] = True
            dp[i+1][key-m] = True
    return dp[l][target-offset]


main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    S = readline().strip()
    x, y = map(int, readline().split())

    S = S.split('T')
    x0 = len(S[0])
    X = [len(s) for s in S[2::2]]
    Y = [len(s) for s in S[1::2]]

    def solve(vec, p0, p):
        s = {p0}
        for v in vec:
            s, s_prev = set(), s
            for w in s_prev:
                s.add(w + v)
                s.add(w - v)
        return p in s

    if solve(X, x0, x) and solve(Y, 0, y):
        print('Yes')
    else:
        print('No')

    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    S = readline().strip()
    x, y = map(int, readline().split())

    S = S.split('T')
    x0 = len(S[0])
    X = [len(s) for s in S[2::2]]
    Y = [len(s) for s in S[1::2]]

    def solve(vec, p0, p):
        s = {p0}
        for v in vec:
            s, s_prev = set(), s
            for w in s_prev:
                s.add(w + v)
                s.add(w - v)
        return p in s

    if solve(X, x0, x) and solve(Y, 0, y):
        print('Yes')
    else:
        print('No')

    return


if __name__ == '__main__':
    main()

Solution (Python):





































s = input().split('T')
a, b = map(int, input().split())

current_x = len(s[0])
current_y = 0


for i in sorted([len(j) for j in s[2::2]])[::-1]:
    if current_x < a:
        current_x += i
    else:
        current_x -= i
for i in sorted([len(j) for j in s[1::2]])[::-1]:
    if current_y < b:
        current_y += i
    else:
        current_y -= i

if current_x==a and current_y==b:
    print('Yes', flush=True)
else:
    print('No', flush=True)

Solution (Python):
s = list(input())
s.append('T')
X,Y =map(int, input().split())
curX = True
cnt = 0
X_s = []
Y_s = []
for i,c in enumerate(s):
    if c == 'T':
        if curX:
            X_s.append(cnt)
        else:
            Y_s.append(cnt)
        curX = not curX
        cnt=0
    else:
        cnt+=1

init = 0
if s[0] == 'F':
    init = X_s.pop(0)
X -= init

SETX = set([0])
SETY = set([0])

for TX in X_s:
    ADD = set()
    for SX in SETX:
        ADD.add(SX+TX)
    SETX.add(TX)
    SETX = ADD | SETX
for TY in Y_s:
    ADD = set()
    for SY in SETY:
        ADD.add(SY+TY)
    SETY.add(TY)
    SETY = ADD | SETY

MODX = sum(X_s)-X
MODY = sum(Y_s)-Y

if MODX % 2 == 0 and MODX//2 in SETX and MODY % 2 == 0 and MODY//2 in SETY:
    print('Yes')
else:
    print('No')

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

x = True 

Slist = []
import itertools
for key,g in itertools.groupby(S):
  num = len(list(g))
  Slist.append((key, num))
  
xdp = set()
ydp = set([0])
ind = 0

if Slist[0][0] == "F":
  xdp.add(Slist[0][1])
  ind += 1
else:
  xdp.add(0)
  
for i in range(ind, len(Slist)):
  if Slist[i][0] == "T":
    if Slist[i][1] % 2 == 1:
      x ^= True
    continue
  step = Slist[i][1]
  if x:
    next_dp = set()
    for xpos in xdp:
      next_dp.add(xpos + step)
      next_dp.add(xpos - step)
    xdp = next_dp
  else:
    next_dp = set()
    for ypos in ydp:
      next_dp.add(ypos + step)
      next_dp.add(ypos - step)
    ydp = next_dp
    
if X in xdp and Y in ydp:
  print("Yes")
else:
  print("No")

Solution (Python):
from collections import deque
s=list(input())
x,y=map(int,input().split())

i=0
xflag=1
flag=0
xax=deque()
yax=deque()
cnt=0
while i<len(s):
    if s[i]=="F":
        cnt=cnt+1
    else:
        if cnt!=0:
            if xflag==1:
                xax.append(cnt)
            else:
                yax.append(cnt)
        xflag=(xflag+1)%2
        cnt=0
    i=i+1
if cnt!=0:
    if xflag == 1:
        xax.append(cnt)
    else:
        yax.append(cnt)

xdeq=deque()
ydeq=deque()
if (len(xax)>0)and(s[0]=="F"):
    x0=xax.popleft()
    xdeq.append(x0)
else:
    xdeq.append(0)
ydeq.append(0)

while len(xax)>0:
    subadd=xax.popleft()
    xdeq2=[]
    while len(xdeq)>0:
        xd=xdeq.popleft()
        xdeq2.append(xd-subadd)
        xdeq2.append(xd+subadd)
    xdeq2=list(set(xdeq2))
    for i in xdeq2:
        xdeq.append(i)
    

while len(yax)>0:
    subadd=yax.popleft()
    ydeq2=[]
    while len(ydeq)>0:
        yd=ydeq.popleft()
        ydeq2.append(yd-subadd)
        ydeq2.append(yd+subadd)
    ydeq2=list(set(ydeq2))
    for i in ydeq2:
        ydeq.append(i)
    

xlist=list(xdeq)
ylist=list(ydeq)
for i in xlist:
    for j in ylist:
        if (i==x)and(j==y):
            flag=1
            break
    if flag==1:
        break
if flag==1:
    print("Yes")
else:
    print("No")

Solution (Python):

import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub, itemgetter, attrgetter


import sys


readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(readline())


def read_int_n():
    return list(map(int, readline().split()))


def read_float():
    return float(readline())


def read_float_n():
    return list(map(float, readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.perf_counter()
        ret = f(*args, **kwargs)
        e = time.perf_counter()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(S, X, Y):
    m = list(map(lambda x: len(x), S.split('T')))

    X -= m[0]
    ym = m[1::2]
    xm = m[2::2]

    memo = {0}
    for dx in xm:
        nm = set()
        for x in memo:
            nm.add(x+dx)
            nm.add(x-dx)
        memo = nm
    if X not in memo:
        return 'No'

    memo = {0}
    for dy in ym:
        nm = set()
        for y in memo:
            nm.add(y+dy)
            nm.add(y-dy)
        memo = nm
    if Y not in memo:
        return 'No'

    return 'Yes'




def main():
    S = read_str()
    X, Y = read_int_n()
    print(slv(S, X, Y))


if __name__ == '__main__':
    main()

Solution (Python):

import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub, itemgetter, attrgetter


import sys


readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(readline())


def read_int_n():
    return list(map(int, readline().split()))


def read_float():
    return float(readline())


def read_float_n():
    return list(map(float, readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.perf_counter()
        ret = f(*args, **kwargs)
        e = time.perf_counter()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(S, X, Y):
    n = 0
    m = []
    for c in S:
        if c == 'T':
            m.append(n)
            n = 0
        else:
            n += 1
    else:
        m.append(n)

    X -= m.pop(0)
    ym = m[::2]
    xm = m[1::2]

    memo = {0}
    for dx in xm:
        nm = set()
        for x in memo:
            nm.add(x+dx)
            nm.add(x-dx)
        memo = nm
    if X not in memo:
        return 'No'

    memo = {0}
    for dy in ym:
        nm = set()
        for y in memo:
            nm.add(y+dy)
            nm.add(y-dy)
        memo = nm
    if Y not in memo:
        return 'No'

    return 'Yes'




def main():
    S = read_str()
    X, Y = read_int_n()
    print(slv(S, X, Y))


if __name__ == '__main__':
    main()

Solution (Python):
def isok(f,p):
  s=0
  if not f and p==0:return 1
  for i,j in enumerate(f):
    s+=j
    if s>=p:break
  else:return 0
  for j in f[i+1:]:
    if s>=p:s-=j
    else:s+=j
  return s==p
s=input()
x,y=map(int,input().split())
f=list(map(len,s.split('T')))
x-=f[0]
x,y=abs(x),abs(y)
xf=sorted(f[2::2])[::-1]
yf=sorted(f[1::2])[::-1]
print(['No','Yes'][isok(xf,x) and isok(yf,y)])

Solution (Python):

import sys
from itertools import product
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
S = readline().decode().rstrip()
x,y = map(int,readline().split())
L = [len(i) for i in S.split('T')]
x -= L[0]
X = L[2::2]
Y = L[1::2]

dx = {0}
dy = {0}

for i in X:
    dx = {x+j for x,j in product(dx,[i,-i])}
for i in Y:
    dy = {y+j for y,j in product(dy,[i,-i])}

if x in dx and y in dy:
    print('Yes')
else:
    print('No')

Solution (Python):

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

S = readline().decode().rstrip()
N = len(S)
x,y = map(int,readline().split())
t_cnt = 0
cnt_x1 = 0
cnt_x2 = 0
cnt_y = 0
X = []
Y = []
for i in range(N):
    if S[i] == 'F':
        if t_cnt == 0:
            cnt_x1 += 1
        else:
            if t_cnt % 2 == 0:
                cnt_x2 += 1
            else:
                cnt_y += 1
    else:
        t_cnt += 1
        if cnt_x2 != 0:
            X.append(cnt_x2)
            cnt_x2 = 0
        if cnt_y != 0:
            Y.append(cnt_y)
            cnt_y = 0
            
if cnt_x2 != 0:
    X.append(cnt_x2)
    cnt_x2 = 0
if cnt_y != 0:
    Y.append(cnt_y)
    cnt_y = 0

xN = len(X)
yN = len(Y)    
sumX = sum(X)
sumY = sum(Y)
x -= cnt_x1

if sumX - x < 0 or (sumX - x) % 2 != 0:
    print('No')
    sys.exit()
else:  
    xx = (sumX - x) // 2
if sumY - y < 0 or (sumY - y) % 2 != 0:
    print('No')
    sys.exit()
else:  
    yy = (sumY - y) // 2
    
dp_x = [0]*(xx+1) 
dp_x[0] = 1
for i in range(xN):
    w = X[i]
    for j in range(xx,w-1,-1):
        dp_x[j] = dp_x[j] or dp_x[j-w]
        
dp_y = [0]*(yy+1) 
dp_y[0] = 1
for i in range(yN):
    w = Y[i]
    for j in range(yy,w-1,-1):
        dp_y[j] = dp_y[j] or dp_y[j-w]

if dp_x[xx] and dp_y[yy]:
    print('Yes')
else:
    print('No')

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

for n in range(1,len(s)):
  XY[n%2].append(len(s[n]))

X,Y = XY
X = sorted(X)[::-1]
Y = sorted(Y)[::-1]

for n in X:
  if 0<x:
    x-=n
  else:
    x+=n

for n in Y:
  if 0<y:
    y-=n
  else:
    y+=n

if (x,y)==(0,0):
  print("Yes")
else:
  print("No")