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):
t,*s=input().split('T')
x,y=map(int,input().split())
dx,dy={len(t)},{0}
for a in map(len,s[1::2]):dx=set(t+a for t in dx)|set(t-a for t in dx)
for b in map(len,s[::2]):dy=set(t+b for t in dy)|set(t-b for t in dy)
if x in dx and y in dy:print('Yes')
else:print('No')

Solution (Python):
t,*s=input().split('T')
x,y=map(int,input().split())
dx,dy={len(t)},{0}
for a in map(len,s[1::2]):dx=set(t+a for t in dx)|set(t-a for t in dx)
for b in map(len,s[::2]):dy=set(t+b for t in dy)|set(t-b for t in dy)
if x in dx and y in dy:print('Yes')
else:print('No')

Solution (Python):
s=input().split('T')
x,y=map(int,input().split())
dx,dy={0},{0}
for i,a in enumerate(map(len,s[::2])):
    if i:dx=set(t+a for t in dx)|set(t-a for t in dx)
    else:dx={a}
for b in map(len,s[1::2]):dy=set(t+b for t in dy)|set(t-b for t in dy)
if x in dx and y in dy:print('Yes')
else:print('No')

Solution (Python):


def check(goal, dist_data, start):
    dp_now = [0 for i in range(17000)]
    dp_now[start + 8500] = 1
    dp_next = [0 for i in range(17000)]

    for i in range(len(dist_data)):
        for j in range(len(dp_now)):
            if dp_now[j]:
                dp_next[j + dist_data[i]] = 1
                dp_next[j - dist_data[i]] = 1
        dp_now = dp_next
        dp_next = [0 for i in range(17000)]

    return dp_now[goal + 8500]


def main():
    data = input()
    goal = list(map(int, input().split()))
    data = data.split('T')
    move_dist = [len(ele) for ele in data]

    x_data = move_dist[2::2]
    y_data = move_dist[1::2]

    if check(goal[0], x_data, move_dist[0]) and check(goal[1], y_data, 0):
        print('Yes')
    else:
        print('No')


if __name__ == '__main__':
    main()

Solution (Python):
from itertools import groupby
from sys import stdin
from numpy import logical_or, zeros


def solve(s, p, q):

    ss = [list(g) for k, g in groupby(s)]
    len_sss = [len(sss) for sss in ss]
    ss_and_len = list(zip(ss, len_sss))
    is_start_with_TT = True if ss[0][0] == 'T' and 0 < len_sss[0] else False
    dir = 0
    dx, dy = [], []

    for sss, l in ss_and_len:
        if sss[0] == 'T':
            dir += l
        elif sss[0] == 'F':
            if dir % 2 == 0:
                dx.append(l)
            else:
                dy.append(l)

    len_dx = len(dx)
    len_dy = len(dy)

    _n = max(sum(dx), sum(dy))
    __n = _n*2+1
    if _n < abs(p) or _n < abs(q):
        return 0

    if 0 < len_dx:
        dpx = zeros((len_dx, __n), dtype='uint64')
        if is_start_with_TT:
            dpx[0][_n-dx[0]] = 1
        dpx[0][_n+dx[0]] = 1

        for i in range(1, len_dx):
            s = dx[i]
            dpx[i][:s] = dpx[i-1][s:2*s]
            dpx[i][s:-s] = logical_or(dpx[i-1][:-2*s], dpx[i-1][2*s:])
            dpx[i][-s:] = dpx[i-1][-2*s:-s]
    else:
        dpx = [[0]*__n]
        dpx[0][_n] = 1

    if 0 < len_dy:
        dpy = zeros((len_dy, __n), dtype='uint64')
        dpy[0][_n+dy[0]] = 1
        dpy[0][_n-dy[0]] = 1

        for i in range(1, len_dy):
            s = dy[i]
            dpy[i][:s] = dpy[i-1][s:2*s]
            dpy[i][s:-s] = logical_or(dpy[i-1][:-2*s], dpy[i-1][2*s:])
            dpy[i][-s:] = dpy[i-1][-2*s:-s]
    else:
        dpy = [[0]*__n]
        dpy[0][_n] = 1
    
    

    return dpx[-1][_n+p] and dpy[-1][_n+q]


if __name__ == '__main__':
    s = stdin.readline()
    p, q = map(int, input().split())

    print('Yes' if solve(s, p, q) else 'No')

Solution (Python):
s=input()
x,y=map(int,input().split())
ct=0
XY=[]
for i in range(len(s)):
  if s[i]=="F":
    ct+=1
  else:
    XY.append(ct)
    ct=0
XY.append(ct)
x-=XY[0]
X=XY[2::2]
Y=XY[1::2]


def chk(Z,z):
  tot,nz=sum(Z),len(Z)
  if tot==0:
    if z==0:
      return True
    else:
      return False
  elif abs(z)>tot:
    return False
  else:
    DP=[[False]*(2*tot+1) for _ in range(nz+1)]
    DP[0][tot]=True
    for i in range(nz):
      zz=Z[i]
      for j in range(2*tot+1):
        if j+zz<2*tot+1 and DP[i][j+zz]==True:
          DP[i+1][j]=True
        if j-zz>=0 and DP[i][j-zz]==True:
          DP[i+1][j]=True
    return DP[nz][z+tot]

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

Solution (Python):
from collections import defaultdict
s = input() + 'T'
x, y = map(int, input().split())

si = 0
while s[si] == 'F':
    x -= 1
    si += 1
si += 1

d = [[], []]
dir = 1
z = 0
for i in range(si, len(s)):
    if s[i] == 'T':
        if z > 0:
            d[dir].append(z)
        dir = (dir + 1) % 2
        z = 0
    else:
        z += 1

d[0].sort()
d[1].sort()

def check(a, b):
    c1 = defaultdict(int)
    c2 = defaultdict(int)
    c1[0] += 1
    for i in range(len(a)):
        for c in c1:
            c2[c+a[i]] += 1
            c2[c-a[i]] += 1
        c1 = c2.copy()
        c2 = defaultdict(int)

    
    if b in c1:
        return True
    else:
        return False

if check(d[0], x) and check(d[1], y):
    print("Yes")
else:
    print("No")

Solution (Python):
S=list(map(len,input().split("T")))
x,y=map(int,input().split())
x=abs(x-S[0])
y=abs(y)
L=S[2::2]
R=S[1::2]
sumL=sum(L)
sumR=sum(R)
flag=0
if sumL>=x and sumR>=y and (sumL-x)%2==0 and (sumR-y)%2==0 and flag==0:
    setL1={0}
    setR1={0}
    gL=(sumL-x)//2
    gR=(sumR-y)//2
    if gL==0:
        flag=1
    else:
        for i in L:
            setL=setL1
            setL1=set()
            s=setL1.add
            for j in setL:
                k=i+j
                s(k)
                s(j)
                if k==gL:
                    flag=1
                    break
            if flag==1:
                break
    if flag==1:
        if gR==0:
            flag=2
        else:
            for i in R:
                setR=setR1
                setR1=set()
                s=setR1.add
                for j in setR:
                    k=i+j
                    s(k)
                    s(j)
                    if k==gR:
                        flag=2
                        break
                if flag==2:
                    break
if flag==2:
    print("Yes")
else:
    print("No")

Solution (Python):
from itertools import accumulate
S=list(map(len,input().split("T")))
x,y=map(int,input().split())
x=abs(x-S[0])
y=abs(y)
L=S[2::2]
R=S[1::2]
La=list(accumulate(L))
Ra=list(accumulate(R))
sumL=sum(L)
sumR=sum(R)
flag=0
if sumL>=x and sumR>=y and (sumL-x)%2==0 and (sumR-y)%2==0 and flag==0:
    setL1={0}
    setR1={0}
    gL=(sumL-x)//2
    gR=(sumR-y)//2
    if gL==0:
        flag=1
    else:
        for i in L:
            setL=setL1
            setL1=set()
            s=setL1.add
            for j in setL:
                k=i+j
                s(k)
                s(j)
                if k==gL:
                    flag=1
                    break
            if flag==1:
                break
    if flag==1:
        if gR==0:
            flag=2
        else:
            for i in R:
                setR=setR1
                setR1=set()
                s=setR1.add
                for j in setR:
                    k=i+j
                    s(k)
                    s(j)
                    if k==gR:
                        flag=2
                        break
                if flag==2:
                    break
if flag==2:
    print("Yes")
else:
    print("No")

Solution (Python):
from itertools import accumulate
S=list(map(len,input().split("T")))
x,y=map(int,input().split())
x=abs(x-S[0])
y=abs(y)
L=S[2::2]
R=S[1::2]
La=list(accumulate(L))
Ra=list(accumulate(R))
sumL=sum(L)
sumR=sum(R)
flag=0
if sumL>=x and sumR>=y and (sumL-x)%2==0 and (sumR-y)%2==0 and flag==0:
    setL1={0}
    setR1={0}
    gL=(sumL-x)//2
    gR=(sumR-y)//2
    if gL==0:
        flag=1
    else:
        for cnt,i in enumerate(L):
            setL=setL1
            setL1=set()
            s=setL1.add
            h=sumL-La[cnt]
            for j in setL:
                k=i+j
                
                s(k)
                
                s(j)
                if k==gL:
                    flag=1
                    break
            if flag==1:
                break
    if flag==1:
        if gR==0:
            flag=2
        else:
            for cnt,i in enumerate(R):
                setR=setR1
                setR1=set()
                s=setR1.add
                h=sumR-Ra[cnt]
                for j in setR:
                    k=i+j
                    
                    s(k)
                    
                    s(j)
                    if k==gR:
                        flag=2
                        break
                if flag==2:
                    break
if flag==2:
    print("Yes")
else:
    print("No")

Solution (Python):
s=input()
x,y=map(int,input().split())
h=[]
v=[]
num=0
m=1
for i in s:
    if i=='F':
        num+=1
    else:
        if num!=0:
            if m==1:
                h.append(num)
            else:
                v.append(num)
        m=-m
        num=0
if num!=0:
    if m==1:
        h.append(num)
    else:
        v.append(num)
if s[0]=='F':
    x=x-h[0]
    del h[0]
sumh=sum(h)
nh=len(h)
dph=[]
if nh!=0:
    for i in range(nh+1):
        sub_list=[False]*(1+2*sumh)
        dph.append(sub_list)
    dph[0][sumh]=True
    for i in range(1,nh+1):
        for j in range(1+2*sumh):
            if j-h[i-1]>=0:
                dph[i][j]=dph[i][j] or dph[i-1][j-h[i-1]]
            if j+h[i-1]<=2*sumh:
                dph[i][j]=dph[i][j] or dph[i-1][j+h[i-1]]
    if 0<=x+sumh<=2*sumh:
        sh=dph[nh][x+sumh]
    else:
        sh=False
else:
    if x==0:
        sh=True
    else:
        sh=False
sumv=sum(v)
nv=len(v)
dpv=[]
if nv!=0:
    for i in range(nv+1):
        sub_list=[False]*(1+2*sumv)
        dpv.append(sub_list)
    dpv[0][sumv]=True
    for i in range(1,nv+1):
        for j in range(1+2*sumv):
            if j-v[i-1]>=0:
                dpv[i][j]=dpv[i][j] or dpv[i-1][j-v[i-1]]
            if j+v[i-1]<=2*sumv:
                dpv[i][j]=dpv[i][j] or dpv[i-1][j+v[i-1]]
    if 0<=y+sumv<=2*sumv:
        sv=dpv[nv][y+sumv]
    else:
        sv=False
else:
    if y==0:
        sv=True
    else:
        sv=False
if sh and sv:
    print('Yes')
else:
    print('No')

Solution (Python):
import sys
import math
from itertools import accumulate as acc

def cul(x,X):
    dic={0:1}
    for i in range(len(x)):
        tmp={}
        for j in dic:
            tmp[j+x[i]]=1
            tmp[j-x[i]]=1
        dic=tmp
    if X in dic: return True
    else: return False

S=input()
s=list(S.split("T"))

X,Y=map(int,input().split())
x,y=[len(i) for i in s[::2]],[len(i) for i in s[1::2]]
if S[0]=="F": X=abs(X-x.pop(0))
Y=abs(Y)

print("Yes" if cul(x,X) and cul(y,Y) else "No")

Solution (Python):
def cul(x,X):
    dic={0:1}
    for i in range(len(x)):
        tmp={}
        for j in dic:
            tmp[j+x[i]]=1
            tmp[j-x[i]]=1
        dic=tmp
    if X in dic: return True
    else: return False

S=input()
s=list(S.split("T"))
X,Y=map(int,input().split())
x,y=[len(i) for i in s[::2]],[len(i) for i in s[1::2]]
if S[0]=="F": X=abs(X-x.pop(0))
Y=abs(Y)

dx=sorted(x,reverse=True)
dy=sorted(y,reverse=True)

print("Yes" if cul(dx,X) and cul(dy,Y) else "No")

Solution (Python):
def cul(x,X):
    dic={0:1}
    for i in range(len(x)):
        tmp={}
        for j in dic:
            tmp[j+x[i]]=1
            tmp[j-x[i]]=1
        dic=tmp
    if X in dic: return True
    else: return False

S=input()
s=list(S.split("T"))
X,Y=map(int,input().split())
x,y=[len(i) for i in s[::2]],[len(i) for i in s[1::2]]
if S[0]=="F": X=abs(X-x.pop(0))
Y=abs(Y)

x.sort(reverse=True)
y.sort(reverse=True)

print("Yes" if cul(x,X) and cul(y,Y) else "No")

Solution (Python):
def f(s):
    return abs(int(s))

def cul(x,X):
    dic={0:1}
    for i in range(len(x)):
        tmp={}
        for j in dic:
            tmp[j+x[i]]=1
            tmp[j-x[i]]=1
        dic=tmp
    if X in dic: return True
    else: return False

s=input()
X,Y=map(int,input().split())
x,y=[],[]
d,tmp=0,0
for i in range(len(s)):
    if s[i]=="T":
        if tmp!=0:
            if d==0: x.append(tmp)
            else: y.append(tmp)
        d^=1
        tmp=0
        
    else: tmp+=1
if tmp!=0:
    if d==0: x.append(tmp)
    else: y.append(tmp)


if s[0]=="F": X=abs(X-x.pop(0))
Y=abs(Y)


print("Yes" if cul(x,X) and cul(y,Y) else "No")

Solution (Python):
s = input()
x, y = list(map(int, input().split()))
Ans = ["No", "Yes"]
UD = []
RL = []
ud = 0
rl = 0
f = -1
cnt = 0
for i in s:
  if i == "F":
    cnt += 1
  else:
    if f == 0:
      UD.append(cnt)
      ud += cnt
      f = 1
    elif f == 1:
      RL.append(cnt)
      rl += cnt
      f = 0
    else:
      f = 0
      x -= cnt
    cnt = 0

if f == 0:
  UD.append(cnt)
  ud += cnt
elif f == 1:
  RL.append(cnt)
  rl += cnt
else:
  x -= cnt

if abs(y) > ud or abs(x) > rl:
  print(Ans[0])
  exit()

d = ud - abs(y)
l = rl - abs(x)

if d % 2 == 1 or l % 2 == 1:
  print(Ans[0])
  exit()

if d != 0:
  memo = [0]
  k = d // 2
  for i in UD:
    t = [0]
    for j in memo:
      if i + j == k:
        break
      if i + j < k:
        t.append(i + j)
    else:
      memo = t
      continue
    break
  else:
    print(Ans[0])
    exit()

if l != 0:
  memo = [0]
  k = l // 2
  for i in RL:
    t = [0]
    for j in memo:
      if i + j == k:
        break
      if i + j < k:
        t.append(i + j)
    else:
      memo = t
      continue
    break
  else:
    print(Ans[0])
    exit()

print(Ans[1])

Solution (Python):
import sys
from itertools import product as prd
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
MOD = 1000000007
def POW(x, y): return pow(x, y, MOD)
def INV(x, m=MOD): return pow(x, m - 2, m)
def DIV(x, y, m=MOD): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

S = input()
X,Y=LI()

while S and S[0] == 'F':
    S = S[1:]
    X -= 1
isX = True
cur = 0
xs = []
ys = []
for i in range(len(S)):
    if S[i] == 'T':
        if cur:
            if isX: xs.append(cur)
            else: ys.append(cur)
        isX = not isX
        cur = 0
    elif S[i] == 'F': cur += 1

if cur:
    if isX: xs.append(cur)
    else: ys.append(cur)
sumxs = sum(xs)
sumys = sum(ys)

if abs(X) > sumxs or abs(Y) > sumys:
    print('No')
    exit()

memo1 = [[0]*(sumxs*2+1) for i in range(len(xs)+1)]
memo2 = [[0]*(sumys*2+1) for i in range(len(ys)+1)]

memo1[0][0] = 1
memo2[0][0] = 1
for i, j in prd(range(1, len(xs)+1), range(-sumxs, sumxs+1)):
    memo1[i][j] = memo1[i-1][j+xs[i-1]] or memo1[i-1][j-xs[i-1]]

for i, j in prd(range(1, len(ys)+1), range(-sumys, sumys+1)):
    memo2[i][j] = memo2[i-1][j+ys[i-1]] or memo2[i-1][j-ys[i-1]]

print('Yes' if memo1[len(xs)][X] and memo2[len(ys)][Y] else 'No')

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
MOD = 1000000007
def POW(x, y): return pow(x, y, MOD)
def INV(x, m=MOD): return pow(x, m - 2, m)
def DIV(x, y, m=MOD): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

S = input()
X,Y=LI()

while S and S[0] == 'F':
    S = S[1:]
    X -= 1
isX = True
cur = 0
xs = []
ys = []
for i in range(len(S)):
    if S[i] == 'T':
        if cur:
            if isX:
                xs.append(cur)
            else:
                ys.append(cur)
        isX = not isX
        cur = 0
    elif S[i] == 'F':
        cur += 1

if cur:
    if isX:
        xs.append(cur)
    else:
        ys.append(cur)

sumxs = sum(xs)
sumys = sum(ys)

if abs(X) > sumxs or abs(Y) > sumys:
    print('No')
    exit()

memo1 = [[0]*(sumxs*2+1) for i in range(len(xs)+1)]
memo2 = [[0]*(sumys*2+1) for i in range(len(ys)+1)]

memo1[0][0] = 1
memo2[0][0] = 1
for i in range(1, len(xs)+1):
    for j in range(-sumxs, sumxs+1):
        memo1[i][j] = memo1[i-1][j+xs[i-1]] or memo1[i-1][j-xs[i-1]]

for i in range(1, len(ys)+1):
    for j in range(-sumys, sumys+1):
        memo2[i][j] = memo2[i-1][j+ys[i-1]] or memo2[i-1][j-ys[i-1]]

print('Yes' if memo1[len(xs)][X] and memo2[len(ys)][Y] else 'No')

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
MOD = 1000000007
def POW(x, y): return pow(x, y, MOD)
def INV(x, m=MOD): return pow(x, m - 2, m)
def DIV(x, y, m=MOD): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

S = input()
X,Y=LI()

while S and S[0] == 'F':
    S = S[1:]
    X -= 1

isX = True
cur = 0
xs = []
ys = []
for i in range(len(S)):
    if S[i] == 'T':
        if cur:
            if isX:
                xs.append(cur)
            else:
                ys.append(cur)
        isX = not isX
        cur = 0
    elif S[i] == 'F':
        cur += 1

if cur:
    if isX:
        xs.append(cur)
    else:
        ys.append(cur)

X=abs(X)
Y=abs(Y)
xs.sort()
ys.sort()
xss = [0]
yss = [0]
for i in xs:
    xss.append(xss[-1]+i)
for i in ys:
    yss.append(yss[-1]+i)

memo1 = {}
def dp1(x, i):
    if abs(x) > xss[i+1]: return False
    if i < 0: return x == 0
    if (x, i) in memo1: return memo1[(x,i)]
    res = dp1(x - xs[i], i-1) or dp1(x + xs[i], i-1)
    memo1[(x,i)] = res
    return res

memo2 = {}
def dp2(x, i):
    if abs(x) > yss[i+1]: return False
    if i < 0: return x == 0
    if (x, i) in memo2: return memo2[(x,i)]
    res = dp2(x - ys[i], i-1) or dp2(x + ys[i], i-1)
    memo2[(x,i)] = res
    return res



print('Yes' if dp1(X, len(xs)-1) and dp2(Y, len(ys)-1) else 'No')

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
MOD = 1000000007
def POW(x, y): return pow(x, y, MOD)
def INV(x, m=MOD): return pow(x, m - 2, m)
def DIV(x, y, m=MOD): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()

S = input()
X,Y=LI()

while S and S[0] == 'F':
    S = S[1:]
    X -= 1

isX = True
cur = 0
xs = []
ys = []
for i in range(len(S)):
    if S[i] == 'T':
        if cur:
            if isX:
                xs.append(cur)
            else:
                ys.append(cur)
        isX = not isX
        cur = 0
    elif S[i] == 'F':
        cur += 1

if cur:
    if isX:
        xs.append(cur)
    else:
        ys.append(cur)

X=abs(X)
Y=abs(Y)
xs.sort()
ys.sort()
xss = [0]
yss = [0]
for i in xs:
    xss.append(xss[-1]+i)
for i in ys:
    yss.append(yss[-1]+i)

memo1 = {}
def dp1(x, i):
    if abs(x) > xss[i+1]: return False
    if i < 0: return x == 0
    if (x, i) in memo1: return memo1[(x,i)]
    res = dp1(x - xs[i], i-1) or dp1(x + xs[i], i-1)
    memo1[(x,i)] = res
    return res

memo2 = {}
def dp2(x, i):
    if abs(x) > yss[i+1]: return False
    if i < 0: return x == 0
    if (x, i) in memo2: return memo2[(x,i)]
    res = dp2(x - ys[i], i-1) or dp2(x + ys[i], i-1)
    memo2[(x,i)] = res
    return res



print('Yes' if dp1(X, len(xs)-1) and dp2(Y, len(ys)-1) else 'No')

Solution (Python):
import sys





















input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())


YES = "Yes"  
NO = "No"  

def iterate_tokens():
    for line in sys.stdin:
        for word in line.split():
            yield word


if __name__ == '__main__':

    tokens = iterate_tokens()
    s = list(next(tokens))  
    x = int(next(tokens))  
    y = int(next(tokens))  

    
    x_axis = []
    y_axis = []
    i = 0
    cntT = 0
    cntF = 0
    for i in range(len(s)):
        if s[i] == 'T':
            if cntT % 2 == 0:
                x_axis.append(cntF)
            else:
                y_axis.append(cntF)
            cntT += 1
            cntF = 0
            continue
        else:
            cntF += 1
            continue
    if cntT % 2 == 0:
        x_axis.append(cntF)
    else:
        y_axis.append(cntF)


    x -= x_axis[0]
    del x_axis[0]
    
    x_set = {x}
    y_set = {y}
    for num in x_axis:
        x_set = {i + k for i in x_set for k in {-num, num} if - 8000 <= i <= 8000}
    for num in y_axis:
        y_set = {i+k for i in y_set for k in {-num,num} if -8000<=i<=8000}

    
    if 0 in x_set and 0 in y_set:
        print(YES)
    else:
        print(NO)

Solution (Python):


from collections import* 
from fractions import gcd
from functools import* 
from itertools import* 
from operator import mul
from bisect import* 
from heapq import* 
from math import factorial
from copy import deepcopy
import sys

sys.setrecursionlimit(10**6)

def main():
    *s,=map(len,input().split("T"))
    x,y=map(int,input().split())
    start=s[0]
    n=s[2::2]
    m=s[1::2]

    dx={start}
    dy={0}

    for i in n:
        dx={r+j for j in dx for r in{-i,i}}

    for i in m:
        dy={r+j for j in dy for r in{-i,i}}

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


if __name__ == '__main__':
    main()

Solution (Python):



















import sys
input=sys.stdin.readline
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [input() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    S = ST()
    x, y = MI()
    to_x_y = [[], []]
    deleted = False
    for i,s in enumerate(S):
        if s == "T":
            S = S[i:]
            deleted = True
            break
        else:
            x -= 1
    if not deleted:
        S=""
    toY = False
    contin = False
    
    for i, s in enumerate(S):
        if s == "F":
            if contin:
                to_x_y[toY][-1] += 1
            else:
                to_x_y[toY].append(1)
                contin = True
        else:
            toY = not toY
            contin = False

    

    
    now_x = set([0])
    for distanse in to_x_y[0]:    
        next_x = set()
        for i, X in enumerate(now_x):
            next_x.add(X + distanse)
            next_x.add(X - distanse)
        now_x = next_x
    
    
    now_y = set([0])
    for distanse in to_x_y[1]:    
        next_y = set()
        for i, Y in enumerate(now_y):
            next_y.add(Y + distanse)
            next_y.add(Y - distanse)
        now_y = next_y

    
    print("YNeos"[x not in now_x or y not in now_y::2] )


if __name__ == '__main__':
    main()

Solution (Python):



















import sys
input=sys.stdin.readline
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [input() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    S = ST()
    x, y = MI()
    to_x_y = [[], []]
    deleted = False
    for i,s in enumerate(S):
        if s == "T":
            S = S[i:]
            deleted = True
            break
        else:
            x -= 1
    if not deleted:
        S=""
    toY = False
    contin = False
    
    for i, s in enumerate(S):
        if s == "F":
            if contin:
                to_x_y[toY][-1] += 1
            else:
                to_x_y[toY].append(1)
                contin = True
        else:
            toY = not toY
            contin = False

    

    
    now_x = set([0])
    for distanse in to_x_y[0]:    
        next_x = set()
        for i, X in enumerate(now_x):
            next_x.add(X + distanse)
            next_x.add(X - distanse)
        now_x = next_x
    
    
    now_y = set([0])
    for distanse in to_x_y[1]:    
        next_y = set()
        for i, Y in enumerate(now_y):
            next_y.add(Y + distanse)
            next_y.add(Y - distanse)
        now_y = next_y

    
    print("YNeos"[x not in now_x or y not in now_y::2] )


if __name__ == '__main__':
    main()

Solution (Python):



















import sys
input=sys.stdin.readline
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [input() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    S = ST()
    x, y = MI()
    to_x_y = [[], []]
    deleted = False
    for i,s in enumerate(S):
        if s == "T":
            S = S[i:]
            deleted = True
            break
        else:
            x -= 1
    if not deleted:
        S=""
    toY = False
    contin = False
    
    for i, s in enumerate(S):
        if s == "F":
            if contin:
                to_x_y[toY][-1] += 1
            else:
                to_x_y[toY].append(1)
                contin = True
        else:
            toY = not toY
            contin = False

    

    
    now_x = set([0])
    for distanse in to_x_y[0]:    
        next_x = set()
        for i, X in enumerate(now_x):
            next_x.add(X + distanse)
            next_x.add(X - distanse)
        now_x = next_x
    
    
    now_y = set([0])
    for distanse in to_x_y[1]:    
        next_y = set()
        for i, Y in enumerate(now_y):
            next_y.add(Y + distanse)
            next_y.add(Y - distanse)
        now_y = next_y

    
    print("YNeos"[x not in now_x or y not in now_y::2] )


if __name__ == '__main__':
    main()

Solution (Python):
import sys





















input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())


YES = "Yes"  
NO = "No"  

def iterate_tokens():
    for line in sys.stdin:
        for word in line.split():
            yield word


if __name__ == '__main__':

    tokens = iterate_tokens()
    s = list(next(tokens))  
    x = int(next(tokens))  
    y = int(next(tokens))  

    
    x_axis = []
    y_axis = []
    i = 0
    cntT = 0
    cntF = 0
    for i in range(len(s)):
        if s[i] == 'T':
            if cntT % 2 == 0:
                x_axis.append(cntF)
            else:
                y_axis.append(cntF)
            cntT += 1
            cntF = 0
            continue
        else:
            cntF += 1
            continue
    if cntT % 2 == 0:
        x_axis.append(cntF)
    else:
        y_axis.append(cntF)


    x -= x_axis[0]
    del x_axis[0]
    
    x_set = {x}
    y_set = {y}
    for num in x_axis:
        x_set = {i + k for i in x_set for k in {-num, num} if - 8000 <= i <= 8000}
    for num in y_axis:
        y_set = {i+k for i in y_set for k in {-num,num} if -8000<=i<=8000}

    
    if 0 in x_set and 0 in y_set:
        print(YES)
    else:
        print(NO)

Solution (Python):
s=input().split("T")
X,Y=map(int,input().split())
x=list(map(len,s[::2]))
y=list(map(len,s[1::2]))
l=[x[0]]
s=set()
for i in x[1:]:
    s.clear()
    for j in l:
        s.add(j+i)
        s.add(j-i)
    l=list(s)
if(not X in l):
    print("No")
    quit()
l=[0]
for i in y:
    s.clear()
    for j in l:
        s.add(j+i)
        s.add(j-i)
    l=list(s)
if(not Y in l):
    print("No")
else:
    print("Yes")

Solution (Python):
s=input().split("T")
X,Y=map(int,input().split())
x=list(map(len,s[::2]))
y=list(map(len,s[1::2]))
l=[x[0]]
s=set()
for i in x[1:]:
    s.clear()
    for j in l:
        s.add(j+i)
        s.add(j-i)
    l=list(s)
if(not X in l):
    print("No")
    quit()
l=[0]
for i in y:
    s.clear()
    for j in l:
        s.add(j+i)
        s.add(j-i)
    l=list(s)
if(not Y in l):
    print("No")
else:
    print("Yes")

Solution (Python):
s=input()+"T"
X,Y=map(int,input().split())
xy=[[],[]]
t=0
m=0
for i in s:
    if(i=="T"):
        xy[m].append(t)
        m^=1
        t=0
    else:
        t+=1
x,y=xy
l=[x[0]]
s=set()
for i in x[1:]:
    s.clear()
    for j in l:
        s.add(j+i)
        s.add(j-i)
    l=list(s)
if(not X in l):
    print("No")
    quit()
l=[0]
for i in y:
    s.clear()
    for j in l:
        s.add(j+i)
        s.add(j-i)
    l=list(s)
if(not Y in l):
    print("No")
else:
    print("Yes")