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 <tourist>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <iomanip>
#include <limits>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <queue>
#include <tuple>
#include <deque>
#include <algorithm>
#include <numeric>
#include <iomanip>
#include <utility>
#include <complex>
#include <functional>
using namespace std;
const int MOD = 1000000007;
typedef long long ll;
typedef pair<ll, ll> p;
const int INF = (1 << 28);
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
//setprecision(15)有効数字15桁
//-std=c++14
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}
struct edge {
    int to, cost;
};
int x, y, n;
string s;
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> s;
    n=s.size();
    bool dp[n+1][16001];
    vector<int> count;
    int temp=0;
    for(int i=0;i<n;i++){
        if(s[i]=='F'){
            temp++;
            if(i==n-1){
                count.push_back(temp);
            }
        }
        else{
            count.push_back(temp);
            temp=0;
        }
    }
    /*
    for(int i=0;i<count.size();i++){
        cout<<count[i]<<" ";
    }
    cout<<"\n";
    */
    for (int i = 0; i <= n; i++)
    {
        for(int j=0;j<=16000;j++){
            dp[i][j]=false;
        }
    }
    cin>>x>>y;
    dp[0][8000]=true;
    bool check=true;
    int k=0;
    for(int i=0;i<n;i++){
        if(i==n-1&&s[i]=='F'&&check){
           for(int j=0;j<=16000;j++){
                if(dp[i][j]){
                    dp[i+1][j+count[k]]=true;
                    if(k!=0)
                    dp[i+1][j-count[k]]=true;
                    //cout<<i<<" "<<j<<"\n";
                    }
                }
                k++;
                continue; 
        }
        if(s[i]=='T'){
            check=!check;
            if(!check){
                for(int j=0;j<=16000;j++){
                if(dp[i][j]){
                    dp[i+1][j+count[k]]=true;
                    if(k!=0)
                    dp[i+1][j-count[k]]=true;
                    //cout<<i<<" "<<j<<"\n";
                    }
                }
                k++;
                continue;
            }
            k++;
        }
        for(int j=0;j<=16000;j++){
            if(dp[i][j])
            dp[i+1][j]=true;
        }
    }
    if(!dp[n][8000+x])return no,0;
    for (int i = 0; i <= n; i++)
    {
        for(int j=0;j<=16000;j++){
            dp[i][j]=false;
        }
    }
    dp[0][8000]=true;
    check=false;
    k=0;
    for(int i=0;i<n;i++){
        //cout<<k<<"\n";
        if(i==n-1&&s[i]=='F'&&check){
           for(int j=0;j<=16000;j++){
                if(dp[i][j]){
                    dp[i+1][j+count[k]]=true;
                    dp[i+1][j-count[k]]=true;
                    //cout<<i<<" "<<j<<"\n";
                    }
                }
                k++;
                continue; 
        }
        if(s[i]=='T'){
            check=!check;
            if(!check){
                for(int j=0;j<=16000;j++){
                if(dp[i][j]){
                    dp[i+1][j+count[k]]=true;
                    dp[i+1][j-count[k]]=true;
                    //cout<<i<<" "<<j<<"\n";
                    }
                }
                k++;
                continue;
            }
            k++;
        }
        for(int j=0;j<=16000;j++){
            if(dp[i][j])
            dp[i+1][j]=true;
        }
    }
    if(!dp[n][8000+y])return no,0;
    yes;
}