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 <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long long unsigned int llui;
typedef long long int lli;
typedef long double float64;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;

#define INF 1000000000
#define MOD 1000000007
#define EPS 1e-9
#define DEBUG(x) cout<< '>' << #x << ':' << x << "\n";
#define all(x) (x).begin(), (x).end()
#define SORT(x) sort((x).begin(),(x).end())
#define UNIQUE(x) (x).erase(unique((x).begin(),(x).end()),(x).end())
#define tr(container, it) \
    for(typeof(container.begin()) it = container.begin(); it != container.end(); it++) 

// #define sz(a) int((a).size())
// #define pb push_back
// #define fill(a,v) memset(a,v,sizeof a)


#define trace(x) cout << '>' << #x << ':' << x << endl;
#define trace2(x,y) cout<< '>' << #x << ':' << x << " | " << #y << ':' << y << endl;

#define trace3(x,y,z) cout<< '>' << #x << ':' << x \
        << " | " << #y << ':' << y <<" | " << #z << ':' << z << endl;

#define trace4(x,y,z,w) cout<< '>' << #x << ':' << x \
    << " | " << #y << ':' << y <<" | " << #z << ':' << z << " | " << #w << ':' << w << endl;


inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n>>b)&1; }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }

#define LSOne(S) (S&(-S))

const double pi = acos(-1);

int cmp(double x, double y = 0, double tol = EPS) {
    return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
}

/////////////////////////////////////////////////////////////////////

/*bool sortbysec(const pair<int,int> &a,const pair<int,int> &b)
{
    return (a.second < b.second);
}*/

template<class T> inline T gcd(T a,T b) //NOTES:__gcd(0,0) gives exception
  {if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}



const int MAX_N = 200001;

//ll arr[MAX_N];
vi X,Y;
map<ii,int> M;
map<ii,int> N;

bool f(int i,int res){
	//DEBUG(X.size());
	//trace2(i,res);
	if(i==X.size() && res==0)
		return true;
	if(i==X.size())
		return false;
	if(M.find(make_pair(i,res))!=M.end())
		return M[make_pair(i,res)];
	bool ans=f(i+1,res-X[i]) || f(i+1,res+X[i]);
	M[make_pair(i,res)]=ans;
	return ans;
}


bool g(int i,int res){
	if(i==Y.size() && res==0)
		return true;
	if(i==Y.size())
		return false;
	if(N.find(make_pair(i,res))!=N.end())
		return N[make_pair(i,res)];
	bool ans=g(i+1,res-Y[i]) || g(i+1,res+Y[i]);
	N[make_pair(i,res)]=ans;
	return ans;
}

const int U=4000, V = 16000;
int dp[U][V];
void initialize() {
    for(int i = 0;i < U;i++) {
        for(int j = 0;j < V;j++) {
            dp[i][j] = -1;
        }
    }
}
 
bool possible(int i, vector<int> &A, int sum) {
    if(i == -1 and sum == 0) {
        return true;
    }
    if(i == -1) {
        return false;
    }
 
    if(dp[i][sum+8000] != -1) {
        return dp[i][sum+8000];
    }
 
    bool ans = possible(i-1, A, sum+A[i]) || 
               possible(i-1, A, sum-A[i]);
 
    return dp[i][sum+8000] = ans;
}

int main() {
	string s;
	cin>>s;
	int a,b,x=0,y=0,dx=1,dy;
	scanf("%d%d",&a,&b);
	for(int i=0;i<s.length();i++){
		if(s[i]=='T'){
			if(dx){
					X.push_back(x);
				dx=0;
				x=0;
			}
			else{
					Y.push_back(y);
				dx=1;
				y=0;
			}
		}
		else{
			if(dx)
				x++;
			else
				y++;
		}
	}
	if(dx){
			X.push_back(x);
		dx=0;
		x=0;
	}
	else{
			Y.push_back(y);
		dx=1;
		y=0;
	}

	if(X.size()==0 && Y.size()==0){
        if(a == 0 and a == 0) {
            cout << "Yes" << endl;
        } else {
            cout << "No" << endl;
        }
        return 0;
    }

	// for(int i=0;i<X.size();i++){
	// 	printf("%d ",X[i]);
	// }
	// printf("\n");
	// for(int i=0;i<Y.size();i++){
	// 	printf("%d ",Y[i]);
	// }
	// printf("\n");
	//trace2(f(0,a),g(0,b));
	
	// if((f(0,a) & g(0,b))==1)
	// 	printf("Yes\n");
	// else
	// 	printf("No\n");

	initialize();
	bool ans=possible(X.size()-1,X,a);
	initialize();
	ans= ans && possible(Y.size()-1,Y,b);

	if(ans)
		printf("Yes\n");
	else
		printf("No\n");
	return 0;
}