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: Wrong Answer)

import Data.List
import Data.Array.IO
import Control.Monad

size = 8000

main = do
  s <- getLine
  (x:y:_) <- map (read :: String -> Int) . words <$> getLine
  memoX <- newArray ((-1, (-size)), (size, size)) False :: IO (IOUArray (Int, Int) Bool)
  memoY <- newArray ((-1, (-size)), (size, size)) False :: IO (IOUArray (Int, Int) Bool)
  let inst = reverse $ decode 'F' s []
      (xI:yI:_) = map reverse $ split 0 inst [[], []]
  dpX memoX 0 xI
  dpY memoY 0 yI
  writeArray memoX (-1, 0) True
  writeArray memoY (-1, 0) True
  resultX <- readArray memoX ((length xI) - 1, x)
  resultY <- readArray memoY ((length yI) - 1, y)
  if resultX && resultY then putStrLn "Yes"
                        else putStrLn "No"

decode :: Char -> String -> [Int] -> [Int]
decode _ [] n = n
decode _ (x:xs) []
  | x == 'F'  = decode x xs [1]
  | otherwise = decode x xs [0]
decode c (x:xs) (n:ns)
  | x == 'F' && c == 'F' = decode x xs (n+1:ns)
  | x == 'F' && c == 'T' = decode x xs (1:n:ns)
  | x == 'T' && c == 'F' = decode x xs (0:n:ns)
  | otherwise            = decode x xs (0:n:ns)

split :: Int -> [Int] -> [[Int]] -> [[Int]]
split _ [] s  = s
split 0 (i:[]) (x:xs)
  | i == 0    = x:xs
  | otherwise = (i:x):xs
split 1 (i:[]) (x:y:ys)
  | i == 0    = x:y:ys
  | otherwise = x:(i:y):ys
split 0 (i:is) (x:xs)
  | i == 0    = split 1 is (x:xs)
  | otherwise = split 0 is ((i:x):xs)
split 1 (i:is) (x:y:ys)
  | i == 0    = split 0 is (x:y:ys)
  | otherwise = split 1 is (x:(i:y):ys)

dpX :: IOUArray (Int, Int) Bool -> Int -> [Int] -> IO (IOUArray (Int, Int) Bool)
dpX memo _ []     = return memo
dpX memo 0 (d:ds) = do
  writeArray memo (0, d) True
  dpX memo 1 ds >>= return
dpX memo n (d:ds) = do
  memo' <- foldM (memoize n d) memo [(-size)..size]
  dpX memo' (n+1) ds >>= return

dpY :: IOUArray (Int, Int) Bool -> Int -> [Int] -> IO (IOUArray (Int, Int) Bool)
dpY memo _ []     = return memo
dpY memo 0 (d:ds) = do
  writeArray memo (0, d) True
  writeArray memo (0, (-d)) True
  dpY memo 1 ds >>= return
dpY memo n (d:ds) = do
  memo' <- foldM (memoize n d) memo [(-size)..size]
  dpY memo' (n+1) ds >>= return

memoize :: Int -> Int -> IOUArray (Int, Int) Bool -> Int -> IO (IOUArray (Int, Int) Bool)
memoize n d memo x
  | x - d >= (-size) && x + d <= size = do
    a <- readArray memo (n-1, x-d)
    b <- readArray memo (n-1, x+d)
    writeArray memo (n, x) $ a || b
    return memo
  | x - d >= (-size) = do
    a <- readArray memo (n-1, x-d)
    writeArray memo (n, x) a
    return memo
  |  x + d <= size = do
    b <- readArray memo (n-1, x+d)
    writeArray memo (n, x) b
    return memo
  | otherwise = do
    writeArray memo (n, x) False
    return memo