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 <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;

bool dpX[4005][16005];  // dpX[i+1][j] : i番目の演算まででx座標がjの位置にいられるならtrue
bool dpY[4005][16005];  // dpY[i+1][j] : i番目の演算まででy座標がjの位置にいられるならtrue
 
int main() {
    string s;
    ll gx, gy;
    cin >> s >> gx >> gy;
    ll n = s.size();
    // x,y座標ともに+nだけ平行移動して考える
    gx += n;  gy += n;

    vl vx, vy;
    ll cntF = 0, cntT = 0; 
    rep(i, n) {
        if(s[i] == 'F') {
            cntF++;
        }
        else if(s[i] == 'T') {
            if(cntT%2 == 0) {
                vx.pb(cntF);
            }
            else {
                vy.pb(cntF);
            }
            cntF = 0;
            cntT++;
        }
    }
    if(s[n-1] == 'F' && cntT%2 == 0) {
        vx.pb(cntF);
    }
    if(s[n-1] == 'F' && cntT%2 == 1) {
        vy.pb(cntF);
    }

    ll mx = vx.size();
    dpX[0][n] = true;
    rep(i, mx) {
        exrep(j, 0, 2*n) {
            if(j - vx[i] >= 0 && dpX[i][j - vx[i]]) {
                dpX[i+1][j] = true;
            }
            if(i > 0 && j + vx[i] <= 2*n && dpX[i][j + vx[i]]) {
                dpX[i+1][j] = true;
            }
        }
    }

    ll my = vy.size();
    dpY[0][n] = true;
    rep(i, my) {
        exrep(j, 0, 2*n) {
            if(j - vy[i] >= 0 && dpY[i][j - vy[i]]) {
                dpY[i+1][j] = true;
            }
            if(j + vy[i] <= 2*n && dpY[i][j + vy[i]]) {
                dpY[i+1][j] = true;
            }
        }
    }
    
    if(dpX[mx][gx] && dpY[my][gy]) {
        out("Yes");
    }
    else {
        out("No");
    }

    re0;
}