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>();
			bool isFirst = true;
			bool isX = true;
			int cnt = 0;
			for (int i = 0; i < n; i++) {
				if (isFirst) {
					if (s[i] == 'F') {
						--x;
					} else {
						isFirst = false;
						isX = false;
					}
				} else {
					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);
				}

				cnt = 0;
			}

			int sumX = xs.Count == 0 ? 0 : xs.Sum();
			int sumY = ys.Count == 0 ? 0 : ys.Sum();
			int diffX = Math.Abs(Math.Abs(sumX) - Math.Abs(x));
			int diffY = Math.Abs(Math.Abs(sumY) - Math.Abs(y));
			if((diffX != 0 && xs.Count == 0) || (diffY != 0 && ys.Count == 0) || diffX % 2 != 0 || diffY % 2 != 0) {
				Console.WriteLine("No");
				return;
			}

			bool okX = false;
			if (diffX != 0) {
				int target = diffX / 2;
				int[,] dp = new int[xs.Count, target];
				int[] xx = xs.OrderBy(i => i).ToArray();
				if (xx[0] <= target) {
					dp[0, 0] = 1;
					dp[0, xx[0]] = 1;
					for (int i = 1; i < xs.Count; i++) {
						for (int j = 0; j < diffX; j++) {
							if (dp[i - 1, j] == 1) {
								dp[i, j] = 1;
								int next = j + xx[i];
								if (next == target) {
									okX = true;
									break;
								}

								if (next < target) {
									dp[i, next] = 1;
								}
							}
						}

						if (okX) {
							break;
						}
					}
				}
			} else {
				okX = true;
			}

			bool okY = false;
			if (diffY != 0) {
				int target = diffY / 2;
				int[,] dp = new int[ys.Count, target];
				int[] yy = ys.OrderBy(i => i).ToArray();
				if (yy[0] <= target) {
					dp[0, 0] = 1;
					dp[0, yy[0]] = 1;
					for (int i = 1; i < ys.Count; i++) {
						for (int j = 0; j < diffY; j++) {
							if (dp[i - 1, j] == 1) {
								dp[i, j] = 1;
								int next = j + yy[i];
								if (next == target) {
									okY = true;
									break;
								}

								if (next < target) {
									dp[i, next] = 1;
								}
							}
						}

						if (okY) {
							break;
						}
					}
				}
			} else {
				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;
		}
	}
}