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 <utility> // pair make_pair
using std::swap; // c++11
using std::pair;
using std::make_pair;
#include <tuple> // tuple(c++11) make_tuple(c++11)
using std::tuple; // get<address>(variable)
using std::make_tuple;
#include <string> // string stoi(c++11) stoll(c++11) to_string(c++11)
using std::string;
using std::stoi;
using std::stoll;
using std::to_string;
#include <queue> // queue priority_queue
using std::queue;
using std::priority_queue;
#include <stack> // stack
using std::stack;
#include <vector> // vector
using std::vector;
#include <map> // map
using std::map;
#include <set> // set
using std::set; // insert erase find count
#include <algorithm> // min max
using std::sort;
using std::lower_bound; // iterator,  >=
using std::upper_bound; // iterator,  >
using std::min;
using std::max;
using std::min_element; // iterator
using std::max_element; // iterator
#include <complex> // complex
using std::complex; // real imag
#include <iostream> // cin cout
using std::cin;
using std::cout;
using std::endl;
#include <iomanip> // setprecision fixed(manipulator)
using std::setprecision;
using std::fixed;
#include <cmath>
#include <cstdio>

#define lli long long int
#define INF (int)1e09
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define EFOR(i, m, n) for (int i = m; i <= n; i++)
#define RFOR(i, m, n) for (int i = m; i > n; i--)
#define REFOR(i, m, n) for (int i = m; i >= n; i--)
#define VSORT(v) sort((v).begin(), (v).end()) // sort vector
typedef pair<int, int> PI;
typedef tuple<int, int, int> TI;
template <typename T> using Vec = vector<T>; // c++11


// O(|s|^2)
bool partial_sum(Vec<int>& move, int ask) {
    if ((int)move.size() == 0) {
        if (ask == 0) {
            return true;
        } else {
            return false;
        }
    }

    bool flag;
    int count = 0;
    FOR (i, 0, (int)move.size()) {
        count += move[i];
    }
    if (count == ask) {
        flag = true;
    } else if (count < ask || (count - ask) % 2 == 1) {
        flag = false; 
    } else {
        // 部分和問題を解く
        int target = (count - ask) / 2;
        Vec<Vec<bool> > dp((int)move.size(), Vec<bool>(2 * count + 1, false)); // |s| <= 8000
        dp[0][0] = true;
        dp[0][move[0]] = true;
        FOR (i, 1, (int)move.size()) {
            FOR (j, 0, target + 1) {
                if (dp[i - 1][j] == true) {
                    dp[i][j] = true;
                    if (j - move[i] > 0) {
                        dp[i][j - move[i]] = true;
                    }
                    if (j + move[i] < 2 * count + 1) {
                        dp[i][j + move[i]] = true;
                    }
                }
            }
        }
        flag = dp[(int)move.size() - 1][target];
    }
    return flag;
}

int main(void) {
    string s;
    cin >> s;
    s += 'T';
    int x;
    int y;
    cin >> x >> y;

    Vec<int> move;
    Vec<int> move_x;
    Vec<int> move_y;
    // O(|s|)
    int count = 0;
    FOR (i, 0, (int)s.length()) {
        if (s[i] == 'T') {
            move.push_back(count);
        } else {
            count++;
        }
    }
    FOR (i, 1, (int)move.size()) {
        if (i % 2 == 0) {
            move_x.push_back(move[i]);
        } else {
            move_y.push_back(move[i]);
        }
    }

    bool fx = partial_sum(move_x, x);
    bool fy = partial_sum(move_y, y);
    if (fx && fy) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }

    return 0;
}