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 <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define CK(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)
#define F first
#define S second
#define ll long long

const int INF = 1e9;
const long long LLINF = 1e18;

using namespace std;

int dy[] = {0,1,0,-1};
int dx[] = {1,0,-1,0};
int dy8[] = {0,1,1,1,0,-1,-1,-1};
int dx8[] = {1,1,0,-1,-1,-1,0,1};

/*==================================================*/
/*variable*/
string s;
int x, y;

int maxh, maxw;

int dp[8010][8010][4];  //上に進んだ回数, 右に進んだ回数, 方向
/*==================================================*/
/*function*/

int dfs(int sumUp, int sumRight, int dir, int ind){

    if(dp[sumUp][sumRight][dir] != 0) return dp[sumUp][sumRight][dir];

    if(ind == s.size()){
        if(2*sumUp - maxh == y && 2*sumRight - maxw == x) return dp[sumUp][sumRight][dir] = 1;
        else {
            return dp[sumUp][sumRight][dir] =  -1;
        }
    }

    int goal = -1;

    if(s[ind] == 'T'){
        REP(i,-1,2){
            if(i == 0) continue;
            int newDir = (dir + i + 400000000) % 4;
            dp[sumUp][sumRight][newDir] = dfs(sumUp, sumRight, newDir, ind+1);

            if(dp[sumUp][sumRight][newDir] == 1) goal = 1;
        }
    }else{
        if(dir == 0 || dir == 2){
            int diff = 0;
            if(dir == 0 ) diff = 1;

            int t = dfs(sumUp, sumRight+diff, dir, ind+1);

            dp[sumUp][sumRight+diff][dir] = dfs(sumUp, sumRight+diff, dir, ind+1);

            if(dp[sumUp][sumRight+diff][dir] == 1) goal = 1;
        } else {
            int diff = 0;
            if(dir == 3 ) diff = 1;

            dp[sumUp+diff][sumRight][dir] = dfs(sumUp+diff, sumRight, dir, ind+1);

            if(dp[sumUp+diff][sumRight][dir] == 1) goal = 1;
        }
    }

    return dp[sumUp][sumRight][dir] = goal;
}

/*==================================================*/

int main() {
    cin>>s>>x>>y;
    int dir = 1;    //横
    REP(i,0,s.size()){
        if(s[i] == 'T') dir *= (-1);
        else{
            if(dir == 1) maxw++;
            else maxh++;
        }
    }

    if(dfs(0,0,0,0) == 1) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;

    return 0;
}