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: Runtime Error)

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

int main(){
  bool dplr[4010][16010] = {}, dpud[4010][16010] = {};
  dplr[0][8000] = 1; dpud[0][8000] = 1;
  string S; cin >> S; int x, y; cin >> x >> y;
  int start;
  rep(i, S.size()){
    if(S[i]=='F') x--;
    if(S[i]=='T'){
      start = i; break;
    }
    if(i==S.size() - 1){
      if(x==S.size()) cout << "Yes" << endl;
      else cout << "No" << endl;
      return 0;
    }
  }
  vector<int> UD, LR;
  int cntt = 0, cntf = 0;
  REP(i, start, S.size()){
    if(S[i]=='T'){
      if(cntt%2 && cntf!=0) UD.push_back(cntf);
      if(!(cntt%2) && cntf!=0) LR.push_back(cntf);
      cntf=0;
      cntt++;
    }
    if(S[i]=='F') cntf++;
  }
  if(cntt%2 && cntf!=0) UD.push_back(cntf);
  if(!(cntt%2) && cntf!=0) LR.push_back(cntf);
  rep(i, LR.size()){
    rep(j, 16010){
      if(j>=LR[i]) dplr[i+1][j] |= dplr[i][j-LR[i]];
      if(j+LR[i]<=16009) dplr[i+1][j] |= dplr[i][j+LR[i]];
    }
  }
  rep(i, UD.size()){
    rep(j, 16010){
      if(j>=UD[i]) dpud[i+1][j] |= dpud[i][j-UD[i]];
      if(j+LR[i]<=16009) dpud[i+1][j] |= dpud[i][j+UD[i]];
    }
  }
  if(dplr[LR.size()][x+8000] && dpud[UD.size()][y+8000]) cout << "Yes" << endl;
  else cout << "No" << endl;
}