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 in OCaml (4.02.3) (judge verdict: Wrong Answer)

open Scanf
open Printf

let flip f x y = f y x

let rec range a b = if a >= b then [] else a :: range (a+1) b

let read_int () = scanf " %d" (fun x -> x)
let rec read_ints n = if n = 0 then [] else let i = read_int () in i :: read_ints (n-1)

let rec for_iter a b f = if a >= b then [] else let x = f a in x :: for_iter (a+1) b f
let rec for_iter_ a b f = if a >= b then () else (f a; for_iter_ (a+1) b f)

let list_of_string str =
  let len = String.length str in
  let rec f i = if i >= len then [] else str.[i] :: f (i+1) in
  f 0

let lim_width = 16010
let cp = lim_width / 2

type axis = X | Y

let swt ax = match ax with X -> Y | Y -> X

let rec parse ax acc xs =
  match xs with
  | [] -> if acc = 0 then [] else [(acc, ax)]
  | (x::xs') ->
      if x = 'T' then
        if acc = 0 then parse (swt ax) 0 xs'
        else (acc, ax) :: parse (swt ax) 0 xs'
      else parse ax (acc+1) xs'

let () =
  let hs::_ as s = list_of_string @@ read_line () in
  let [xp; yp] = read_ints 2 in
  let f_first = hs = 'F' in
  let steps = parse X 0 s in
  let xs' = steps |> List.filter (fun z -> snd z = X) |> List.map fst in
  let (x0, xs) = match xs' with
  | (y::ys) when f_first -> (y, ys)
  | _                    -> (0, xs') in
  let ys = steps |> List.filter (fun t -> snd t = Y) |> List.map fst in

  let dp = Array.make lim_width false in
  let bak = Array.make lim_width false in

  let result = List.for_all (fun (ss, src, dst) ->
    Array.fill dp 0 lim_width false; dp.(src) <- true;
    List.iter (fun x ->
      Array.fill bak 0 lim_width false;
      for_iter_ 0 lim_width (fun i ->
        if dp.(i) then 
          if i >= x          then bak.(i-x) <- true;
          if i+x < lim_width then bak.(i+x) <- true);
      for_iter_ 0 lim_width (fun i ->
        dp.(i) <- bak.(i));
    ) ss;
    dp.(dst)) [(xs, cp+x0, cp+xp); (ys, cp, cp+yp)] in

  print_endline @@ if result then "Yes" else "No"