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 <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define vout(x) rep(i,x.size()) cout << x[i] << " "
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

int main() {
    string s; cin >> s;
    int x, y; cin >> x >> y;
    vint m_x, m_y;
    int flag = 1;
    int ct = 0;
    rep(i,s.size()) {
        if (s[i] == 'F') ct++;
        if (s[i] == 'T' || i == s.size()-1) {
            if (flag == 1 && ct != 0) m_x.push_back(ct);
            else if (flag != 1 && ct != 0) m_y.push_back(ct);
            flag *= -1;
            ct = 0;
        }
    }
    bool ok_x = false;
    if (m_x.size() == 0) {
        if (x == 0) ok_x = true;
    }
    else {
        vector<vector<bool>> dp_x(m_x.size(),vector<bool>(17000));
        if (s[0] != 'T') dp_x[0][8000+m_x[0]] = 1;
        else dp_x[0][8000+m_x[0]] = dp_x[0][8000-m_x[0]] = 1;
        REP(i,m_x.size()) {
            rep(j,16000) {
                if (j-m_x[i] >= 0) {
                    dp_x[i][j] = dp_x[i-1][j-m_x[i]] || dp_x[i-1][j+m_x[i]];
                }
                else dp_x[i][j] = dp_x[i-1][j+m_x[i]];
            }
        }
        ok_x = dp_x[m_x.size()-1][8000+x];
    }

    bool ok_y = false;
    if (m_y.size() == 0) {
        if (y == 0) ok_y = true;
    }
    else {
        vector<vector<bool>> dp_y(m_y.size(),vector<bool>(17000));
        dp_y[0][8000+m_y[0]] = dp_y[0][8000-m_y[0]] = 1;
        REP(i,m_y.size()) {
            rep(j,16000) {
                if (j-m_y[i] >= 0) {
                    dp_y[i][j] = dp_y[i-1][j-m_y[i]] || dp_y[i-1][j+m_y[i]];
                }
                else dp_y[i][j] = dp_y[i-1][j+m_y[i]];
            }
        }
        ok_y = dp_y[m_y.size()-1][y+8000];
    }
    if (ok_x && ok_y) cout << "Yes" << endl;
    else cout << "No" << endl;
}