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.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCorder
{
	public class Program
	{
		static void Main(string[] args)
		{
			var cin = new Scanner();
			string s = cin.Next();
			int n = s.Length;
			int x = cin.Int();
			int y = cin.Int();

			List<int> xs = new List<int>();
			List<int> ys = new List<int>();
			int cnt = 0;
			bool isX = true;
			for (int i = 0; i < n; i++) {
				if (s[i] == 'F') {
					++cnt;
				} else {
					if (cnt > 0) {
						if (isX) {
							xs.Add(cnt);
						} else {
							ys.Add(cnt);
						}

						cnt = 0;
					}

					isX = !isX;
				}
			}

			if (cnt > 0) {
				if (isX) {
					xs.Add(cnt);
				} else {
					ys.Add(cnt);
				}
			}

			int sumX = xs.Count == 0 ? 0 : xs.Sum();
			int sumY = ys.Count == 0 ? 0 : ys.Sum();
			bool okX = false;
			if (xs.Count > 0) {
				bool[,] dp = new bool[xs.Count, sumX * 2 + 2];
				dp[0, xs[0] + sumX] = true;
				for (int i = 1; i < xs.Count; i++) {
					for (int j = 0; j < sumX * 2; j++) {
						if (dp[i - 1, j]) {
							dp[i, j + xs[i]] = true;
							dp[i, j - xs[i]] = true;
						}
					}
				}

				if (dp[xs.Count - 1, x + sumX]) {
					okX = true;
				}
			} else if (x == 0) {
				okX = true;
			}

			bool okY = false;
			if (ys.Count > 0) {
				bool[,] dp = new bool[ys.Count, sumY * 2 + 2];
				dp[0, ys[0] + sumY] = true;
				dp[0, -ys[0] + sumY] = true;
				for (int i = 1; i < ys.Count; i++) {
					for (int j = 0; j < sumY * 2; j++) {
						if (dp[i - 1, j]) {
							dp[i, j + ys[i]] = true;
							dp[i, j - ys[i]] = true;
						}
					}
				}

				if (dp[ys.Count - 1, y + sumY]) {
					okY = true;
				}
			} else if (y == 0) {
				okY = true;
			}

			Console.WriteLine(okX && okY ? "Yes" : "No");
		}
	}

	public class Scanner
	{
		private readonly char[] delimiter_ = new char[] { ' ' };
		private string[] buf_;
		private int index_;

		public Scanner()
		{
			buf_ = new string[0];
			index_ = 0;
		}

		public string Next()
		{
			if (index_ < buf_.Length) {
				return buf_[index_++];
			}

			string st = Console.ReadLine();
			while (st == "") {
				st = Console.ReadLine();
			}

			buf_ = st.Split(delimiter_, StringSplitOptions.RemoveEmptyEntries);
			if (buf_.Length == 0) {
				return Next();
			}

			index_ = 0;
			return buf_[index_++];
		}

		public int Int() => int.Parse(Next());
		public long Long() => long.Parse(Next());
		public double Double() => double.Parse(Next());

		public int[] ArrayInt(int N, int add = 0)
		{
			int[] Array = new int[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Int() + add;
			}
			return Array;
		}

		public long[] ArrayLong(int N, long add = 0)
		{
			long[] Array = new long[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Long() + add;
			}
			return Array;
		}

		public double[] ArrayDouble(int N, double add = 0)
		{
			double[] Array = new double[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Double() + add;
			}
			return Array;
		}
	}
}