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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <stdlib.h>
using namespace std;


int main() {
	string s;
	cin >> s;
	int x;
	int y;
	cin >> x >> y;
	int* xMove = new int[8001];
	int* yMove = new int[8001];
	int xIdx = -1;
	int yIdx = -1;
	bool xDir = true;
	
	for (int i = 0; i < s.length(); ++i) {
		if (s[i] == 'T') {
			xDir = !xDir;
		}
		else {
			if (xDir) {
				if (xIdx == -1 || (i - 1 > 0 && s[i - 1] == 'T')) {
					++xIdx;
				}
				++xMove[xIdx];
			} else {
				if (yIdx == -1 || (i - 1 > 0 && s[i - 1] == 'T')) {
					++yIdx;
				}
				++yMove[yIdx];
			}
		} 
	}
	bool xNotMove = false;
	bool yNotMove = false;
	if (xIdx < 0) {
		xIdx = 0;
		xNotMove = true;
	}
	if (yIdx < 0) {
		yIdx = 0;
		yNotMove = true;
	}
	bool** xSum = new bool*[xIdx + 1];
	bool** ySum = new bool*[yIdx + 1];
	
	for (int i = 0; i < xIdx + 1; ++i) {
		xSum[i] = new bool[2 * abs(x) + 1];
		for (int j = 0; j < 2 * abs(x) + 1; ++j) {
			xSum[i][j] = false;
		}
	}
	for (int i = 0; i < yIdx + 1; ++i) {
		ySum[i] = new bool[2 * abs(y) + 1];
		for (int j = 0; j < 2 * abs(y) + 1; ++j) {
			ySum[i][j] = false;
		}
	}

	if (s.length() > 0 && s[0] == 'F') {
		xSum[0][abs(x) + xMove[0]] = true;
	} else if (!xNotMove) {
		xSum[0][abs(x) + xMove[0]] = true;
		xSum[0][abs(x) - xMove[0]] = true;
	}
	
	for (int i = 1; i <= xIdx; ++i) {
		for (int sum = -abs(x); sum <= abs(x); ++sum) {
			xSum[i][abs(x) + sum] = xSum[i - 1][abs(x) + sum - xMove[i]] ||
			xSum[i - 1][abs(x) + sum + xMove[i]];
		}
	}
	if (!xSum[xIdx][abs(x) + x]) {
		cout << "No" << endl;
		return 0;
	}
	if (!yNotMove) {
		ySum[0][abs(y) - yMove[0]] = true;
		ySum[0][abs(y) + yMove[0]] = true;
	}
	for (int i = 1; i <= yIdx; ++i) {
		for (int sum = -abs(y); sum <= abs(y); ++sum) {
			ySum[i][abs(y) + sum] = ySum[i - 1][abs(y) + sum - yMove[i]] ||
			ySum[i - 1][abs(y) + sum + yMove[i]];
		}
	}
	if (!ySum[yIdx][abs(y) + y]) {
		cout << "No" << endl;
		return 0;
	}
	cout << "Yes" << endl;

	return 0;
}