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

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <array>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <stdio.h>
#include <cstdio>
#include <cstring>
#include <math.h>

using namespace std;

#define Rep(i,a,b) for(int i = a; i < b; i++)
#define rep(i,b) Rep(i,0,b)
#define rrep(i,a) for(int i = a; i >= 0; i--)
#define allof(a) (a).begin(), (a).end()

typedef long long ll;
const int inf = 1e9 + 7;
const ll infll = 1ll << 60ll;
const ll mod = 1e9 + 7;
// 0~3までは右左下上 4~7までは斜め
constexpr int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
constexpr int dy[] = { 0, -1, 0, 1, 1, -1, -1, 1 };

/*
// 最大公約数
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}

// 最小公倍数
ll lcm(ll a, ll b) {
	return a * b / gcd(a, b);
}*/
const ll MAXN = 8020;

int main()
{
	string s;
	cin >> s;
	int x, y;
	cin >> x >> y;

	vector<int> t;
	int cnt = 0;
	//文字列を変形する
	for (int i = 0; i < s.size(); i++) {
		if (s[i] == 'T') {
			if (cnt != 0) t.push_back(cnt);
			t.push_back(0);
			cnt = 0;
		}
		else {
			cnt++;
			if (i == s.size() - 1) t.push_back(cnt);
		}
	}

	bool dp_x[MAXN][MAXN * 2] = { false }, dp_y[MAXN][MAXN * 2] = { false };
	dp_x[0][8000] = true;
	dp_y[0][8000] = true;

	bool flag = false;
	bool xy = true; // trueならx軸について見ている
	int cnt_x = 0, cnt_y = 0;

	for (int i = 0; i < t.size(); i++) {
		if (t[i] == 0) {
			xy = !xy;
		}
		else if (xy) {
			cnt_x++;
			for (int j = 0; j < MAXN * 2; j++) {
				if (dp_x[cnt_x - 1][j]) {
					if (j + t[i] < MAXN * 2) dp_x[cnt_x][j + t[i]] = true;
					if (j - t[i] >= 0 && flag) dp_x[cnt_x][j - t[i]] = true;
				}
			}
		}
		else if (!xy) {
			cnt_y++;
			for (int j = 0; j < MAXN * 2; j++) {
				if (dp_y[cnt_y - 1][j]) {
					if (j + t[i] < MAXN * 2) dp_y[cnt_y][j + t[i]] = true;
					if (j - t[i] >= 0) dp_y[cnt_y][j - t[i]] = true;
				}
			}
		}

		if (!flag) flag = true;
	}

	cout << ((dp_x[cnt_x][x + 8000] && dp_y[cnt_y][y + 8000]) ? "Yes" : "No") << endl;

	return 0;
}