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 Common Lisp (SBCL 1.1.14) (judge verdict: Accepted)

;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;; Body

(defun main ()
  (let* ((s (read-line))
         (len (length s))
         (x (read))
         (y (read))
         (hs (make-array len :element-type 'uint15 :fill-pointer 0))
         (vs (make-array len :element-type 'uint15 :fill-pointer 0)))
    (declare ((integer 1 8000) len)
             ((integer -8000 8000) x y))
    (loop with prev-t = -1
          with dest = hs
          for i below (length s)
          when (char= #\T (char s i))
          do (vector-push (- i prev-t 1) dest)
             (setq prev-t i
                   dest (if (eql dest hs) vs hs))
          finally (vector-push (- i prev-t 1) dest))
    (if
     (and
      (let ((dp1 (make-array (+ 1 (* 2 len)) :initial-element 0 :element-type 'bit))
            (dp2 (make-array (+ 1 (* 2 len)) :initial-element 0 :element-type 'bit)))
        (setf (aref dp1 (+ len (aref hs 0))) 1)
        (if (= 1 (length hs))
            (= (aref hs 0) x)
            (loop for h across (subseq hs 1)
                  for dp = dp1 then (if (eql dp dp1) dp2 dp1)
                  for dest-dp = dp2 then (if (eql dest-dp dp1) dp2 dp1)
                  do (fill dest-dp 0)
                     (loop for i below (length dp)
                           when (= 1 (aref dp i))
                           do (setf (aref dest-dp (+ i h)) 1
                                    (aref dest-dp (- i h)) 1))
                  finally (return (= 1 (aref dest-dp (+ len x)))))))
      (let ((dp1 (make-array (+ 1 (* 2 len)) :initial-element 0 :element-type 'bit))
            (dp2 (make-array (+ 1 (* 2 len)) :initial-element 0 :element-type 'bit)))
        (setf (aref dp1 len) 1)
        (if (zerop (length vs))
            (zerop y)
            (loop for v across vs
                  for dp = dp1 then (if (eql dp dp1) dp2 dp1)
                  for dest-dp = dp2 then (if (eql dest-dp dp1) dp2 dp1)
                  do (fill dest-dp 0)
                     (loop for i below (length dp)
                           when (= 1 (aref dp i))
                           do (setf (aref dest-dp (+ i v)) 1
                                    (aref dest-dp (- i v)) 1))
                  finally (return (= 1 (aref dest-dp (+ len y))))))))
     (write-line "Yes")
     (write-line "No"))))

#-swank(main)