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 Haskell (GHC 7.10.3) (judge verdict: Accepted)

{-# LANGUAGE BangPatterns #-}
import Control.Arrow
import Control.Monad.Fix
import Data.Foldable
import Data.List
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VMM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VM
import qualified Data.ByteString.Char8 as B
import Data.ByteString.Internal (accursedUnutterablePerformIO)
import qualified Data.Set as S
import System.IO.Unsafe
import Debug.Trace

unew :: VU.Unbox a => Int -> VM.IOVector a
unew !n = accursedUnutterablePerformIO $ VM.unsafeNew n

uread :: VU.Unbox a => Int -> VM.IOVector a -> a
uread !i !v = accursedUnutterablePerformIO $ VM.unsafeRead v i

uwrite :: VU.Unbox a => Int -> a -> VM.IOVector a -> VM.IOVector a
uwrite !i !a !v =
  accursedUnutterablePerformIO $ VM.unsafeWrite v i a >> return v

umodify :: VU.Unbox a => Int -> (a -> a) -> VM.IOVector a -> VM.IOVector a
umodify !i !f !v =
  accursedUnutterablePerformIO $ VM.unsafeModify v f i >> return v

uthaw :: VU.Unbox a => VU.Vector a -> VM.IOVector a
uthaw !v = accursedUnutterablePerformIO $ VU.unsafeThaw v

ufreeze :: VU.Unbox a => VM.IOVector a -> VU.Vector a
ufreeze !v = accursedUnutterablePerformIO $ VU.unsafeFreeze v

solve :: Int -> Int -> B.ByteString -> Bool
solve !x !y !bs =
  foldl' go dpX rx VU.! toIndex x && foldl' go dpY ry VU.! toIndex y
 where
  go !prev !i = accursedUnutterablePerformIO $ VU.generateM
    (maxN * 2 + 1)
    (\(!k') ->
      let k = k' - maxN
      in  return
          $  (if (k + i) < maxN then prev VU.! toIndex (k + i) else False)
          || (if (k - i) >= -maxN then prev VU.! toIndex (k - i) else False)
    )

  maxN = B.length bs
  dpX  = VU.fromList [ i == rx0 | i <- [-maxN .. maxN] ]
  dpY  = VU.fromList [ i == 0 | i <- [-maxN .. maxN] ]

  toIndex !k = k + maxN

  (!rx0, !rx, !ry) =
    let
      (bs0, bs1)     = B.span (== 'F') bs
      (rx, ry, _, _) = if B.length bs1 == 0
        then ([], [], B.empty, 0)
        else fix
          (\f (!rx, !ry, !bs, !parity) ->
            let (bs0, bs1) = B.span (== 'F') bs
                r          = B.length bs0
                next       = if B.length bs1 == 0 then id else f
                bs1'       = if B.length bs1 == 0 then B.empty else B.tail bs1
                cons       = if r > 0 then (r :) else id
            in  if parity == 0
                  then next $ (cons rx, ry, bs1', 1)
                  else next $ (rx, cons ry, bs1', 0)
          )
          ([], [], B.tail bs1, 1)
    in
      (B.length bs0, rx, ry)

main = do
  let readInt = fmap (second B.tail) . B.readInt

  bs     <- B.getLine
  (x, y) <-
    (\vec -> (vec VU.! 0, vec VU.! 1)) . VU.unfoldrN 2 readInt <$> B.getLine
  putStrLn $ if solve x y bs then "Yes" else "No"