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: Accepted)

//#define TEST

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string s;
    static List<int> dx; // x軸方向の移動距離
    static List<int> dy; // y軸方向の移動距離
    static int x;
    static int y;

    static void Main()
    {
        s = Console.ReadLine();
        int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
        x = input[0];
        y = input[1];
        dx = new List<int>();
        dy = new List<int>();

        int c = 0;
        bool f = false; // trueならx軸方向、falseならy軸方向
        int st = 0;
        while (st < s.Length && s[st] != 'T')
            st++;
        dx.Add(st);
        for (int i = st + 1; i < s.Length; i++)
        {
            if (s[i] == 'F')
            {
                c++;
                continue;
            }
            if (f && c > 0)
                dx.Add(c);
            else if (!f && c > 0)
                dy.Add(c);
            c = 0;
            f = !f;
        }
        if (f)
            dx.Add(c);
        else
            dy.Add(c);

#if TEST
        foreach (var i in dx)
            Console.Write("{0} ", i);
        Console.WriteLine();
        foreach (var i in dy)
            Console.Write("{0} ", i);
        Console.WriteLine();
#endif

        /* dpx[x,i] : dxをi+1番目まで見て、x-8000に到達可能か */
        bool[,] dpx = new bool[16001, dx.Count];
        dpx[dx[0] + 8000, 0] = true;
        for (int i = 1; i < dx.Count; i++)
        {
            int xl = dx[i];
            for (int j = 0; j <= 16000; j++)
            {
                if (j - xl >= 0)
                {
                    dpx[j, i] = dpx[j - xl, i - 1];
                }
                if (j + xl <= 16000)
                {
                    dpx[j, i] = dpx[j, i] || dpx[j + xl, i - 1];
                }
            }
        }
        bool xf = dpx[x + 8000, dx.Count - 1];

#if TEST
        Console.WriteLine(xf);
#endif

        /* dpy[y,i] : dyをi番目まで見て、y-8000に到達可能か */
        bool[,] dpy = new bool[16001, dy.Count + 1];
        dpy[8000, 0] = true;
        for (int i = 0; i < dy.Count; i++)
        {
            int yl = dy[i];
            for (int j = 0; j <= 16000; j++)
            {
                if (j - yl >= 0)
                {
                    dpy[j, i + 1] = dpy[j - yl, i];
                }
                if (j + yl <= 16000)
                {
                    dpy[j, i + 1] = dpy[j, i + 1] || dpy[j + yl, i];
                }
            }
        }
        bool yf = dpy[y + 8000, dy.Count];
#if TEST
        Console.WriteLine(yf);
#endif

        Console.WriteLine((xf && yf) ? "Yes" : "No");
    }
}