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 C# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text;

public class Program
{

    public void Proc()
    {
        string inpt = Reader.ReadLine();
        int[] goalPos = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();

        this.goalX = goalPos[0];
        this.goalY = goalPos[1];

        int xMoveRemain = 0;
        int yMoveRemain = 0;

        string[] tmp = inpt.Split('T');
        this.command = tmp;
        for (int i = 0; i < tmp.Length; i++) {
            if(i%2==0) {
                xMoveRemain += tmp[i].Length;
            } else {
                yMoveRemain += tmp[i].Length;
            }
        }
        this.dic = new Nullable<bool>[tmp.Length, xMoveRemain * 2 + 2, yMoveRemain * 2 + 2, 4];
        bool ans = CanReach(0, 0, 0, Dire.Right, xMoveRemain, yMoveRemain);
        Console.WriteLine(ans?"Yes":"No");
    }

    private enum Dire {
        Up = 0,
        Down = 1,
        Left = 2,
        Right = 3,
    }

    private int xMoveTotal;
    private int yMoveTotal;

    private Nullable<bool>[,,,] dic;

    private bool CanReach(int index, int x, int y, Dire d, int xRemain, int yRemain) {
        if(index == command.Length) {
            if(x == goalX && y == goalY) {
                return true;
            }
            return false;
        }
        if(Math.Abs(x-goalX)>xRemain) {
            return false;
        }
        if(Math.Abs(y-goalY)>yRemain) {
            return false;
        }

        if(dic[index, x+xMoveTotal, y+yMoveTotal, (int)d] != null) {
            return dic[index, x + xMoveTotal, y + yMoveTotal, (int)d].Value;
        }
        bool ans = false;
        int nextX = x;
        int nextY = y;
        if(d == Dire.Up) {
            nextY += command[index].Length;
        }
        if(d== Dire.Down) {
            nextY -= command[index].Length;
        }
        if(d == Dire.Up || d== Dire.Down) {
            ans = ans || CanReach(index + 1, nextX, nextY, Dire.Left, xRemain, yRemain - command[index].Length);
            ans = ans || CanReach(index + 1, nextX, nextY, Dire.Right, xRemain, yRemain - command[index].Length);
        }
        if(d == Dire.Left) {
            nextX -= command[index].Length;
        }
        if( d== Dire.Right) {
            nextX += command[index].Length;
        }
        if(d == Dire.Left || d == Dire.Right) {
            ans = ans || CanReach(index + 1, nextX, nextY, Dire.Up, xRemain - command[index].Length, yRemain);            
            ans = ans || CanReach(index + 1, nextX, nextY, Dire.Down, xRemain - command[index].Length, yRemain);            
        }

        dic[index,x+xMoveTotal, y+yMoveTotal, (int)d] = ans;
        return ans;
    }

    private int goalX;
    private int goalY;

    private string[] command;

    public class Reader
    {
        private static StringReader sr;
        public static bool IsDebug = false;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (sr == null)
                {
                    sr = new StringReader(InputText.Trim());
                }
                return sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
        private static string InputText = @"


TTTT
1 0


";
    }

    public static void Main(string[] args)
    {
#if DEBUG
        Reader.IsDebug = true;
#endif
        Program prg = new Program();
        prg.Proc();
    }
}