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<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
typedef long long ll;
using namespace std;


int a[10000],b=0,c=0;
int ax[5000],ay[5000];
int  DPX[4002][16002],DPY[4002][16002];

int sumx=0,sumy=0;

int dpx(int k,int sum){
    /*0~k番まででsumになるなら1 ならないなら-1*/
    if(k<=-1)return false;
    else if(DPX[k][sum]!=0){
    }else if(k==0){
        if(sum==8000+sumx||sum==8000+sumx+ax[k]){
            DPX[k][sum]=1;
        }else{
            DPX[k][sum]=-1;
        }
    }else if(dpx(k-1,sum)==1||dpx(k-1,sum-ax[k])==1){
        DPX[k][sum]=1;
    }else{
        DPX[k][sum]=-1;
    }
    return DPX[k][sum];
}

int dpy(int k,int sum){
    /*0~k番まででsumになるなら1 ならないなら-1*/
    if(k<=-1)return false;
    else if(DPY[k][sum]!=0){
    }else if(k==0){
        if(sum==8000+sumy||sum==8000+sumy+ay[k]){
            DPY[k][sum]=1;
        }else{
            DPY[k][sum]=-1;
        }
    }else if(dpy(k-1,sum)==1||dpy(k-1,sum-ay[k])==1){
        DPY[k][sum]=1;
    }else{
        DPY[k][sum]=-1;
    }
    return DPY[k][sum];
}


int main(){
    char s[10000];cin>>s;
    int x,y;cin>>x>>y;
    int len=strlen(s);
    while(c<=len-1){
        if(s[c]=='F'){
            a[b]++;
        }else{
            b++;
        }
        c++;
    }
    /*for(int i=0;i<=b;i++)printf("%d ",a[i]);*/
    x=x-a[0];
    int i;
    for(i=2;i<=b;i=i+2)ax[i/2-1]=a[i];
    int lastx=b/2-1;
    for(i=1;i<=b;i=i+2)ay[i/2]=a[i];
    int lasty=(b-1)/2;
    
    for(int i=0;i<=lastx;i++)sumx-=ax[i],ax[i]=2*ax[i];
    for(int i=0;i<=lasty;i++)sumy-=ay[i],ay[i]=2*ay[i];
    
    if(dpx(lastx,10000+x)==1&&dpy(lasty,10000+y)==1)printf("Yes");
    else printf("No");
    
    /*for(int i=0;i<=19999;i++)DPX[i]=false,DPY[i]=false;
    DPX[10000]=true;DPY[10000]=true;
    
    
    if(dpx(lastx,10000+x)==true&&dpy(lasty,10000+y)==true)printf("Yes");
    else printf("No");*/
    
    
    return 0;
}