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 <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <unordered_map>

using namespace std;

struct St {
    string s;
    int degree;
    int x;
    int y;
    St(string s, int degree, int x, int y){
        this->s = s;
        this->degree = degree;
        this->x = x;
        this->y = y;
    }
    long long hash() const{
        return s.size()*100000000000 + degree*100000000 + x*10000 + y;
    }
    bool operator==(const St &left) const {
        return this->s == left.s && this->degree == left.degree &&
            this->x == left.x && this->y == left.y;
    }
};

namespace std{
    template<>
    class hash<St>{
        public:
        size_t operator () ( const St &p ) const{ return p.hash();}
    };
}


unordered_map<St, bool> map;

int countF(string s){
    int count = 0;
    for(int i=0;i<s.size();i++){
        if(s[i] == 'F')
            count++;
    }
    return count;
}

int countT(string s){
    return s.size() - countF(s);
}

int countVertical(string s, int degree) {
    bool flag;
    if(degree%180 == 0)
        flag = false;
    else
        flag = true;
    int count = 0;
    for(int i=0;i<s.size();i++){
        if(s[i] == 'T')
            flag = !flag;
        else {
            if(flag)
                count++;
        }
    }
    return count;
}

bool execute(string s, int degree, long long dx, long long dy, long long countf, long long countv) {
    St st = St(s,degree,dx,dy);
    if (map.find(st) != map.end()){
        return map[st];
    }
    if(s == ""){
        if(dx ==0 && dy==0){
            return true;
        }
        else{
            return false;
        }
    }

    // pruning!!
    int countt = s.size() - countf;
    if (countv < abs(dy))
        return false;
    int h = countf- countv;
    if (h < abs(dx)){
        return false;
    }
    if(degree == 0 && countt < 2 && dx < 0)
        return false;
    if(degree == 180 && countt < 2 && dx > 0)
        return false;
    if(degree == 90 && countt < 2 && dy < 0)
        return false;
    if(degree == 270 && countt < 2 && dy > 0)
        return false;

    string next = s.substr(1);
    if(s[0] == 'F'){
        if(degree==0){
            map[st] = execute(next, degree, dx-1,dy,countf-1,countv);
            return map[st];
        }
        else if (degree == 90){
            map[st] =  execute(next, degree,dx, dy-1,countf-1,countv-1);
            return map[st];
        }
        else if (degree == 180){
            map[st]  = execute(next, degree,dx+1, dy,countf-1,countv);
            return map[st];
        }
        else{
            map[st] = execute(next, degree,dx,dy+1,countf-1,countv-1);
            return map[st];
        }
    } else {
        int minusdegree = degree - 90;
        int plusdegree = degree + 90;
        if(minusdegree < 0)
            minusdegree += 360;
        if(plusdegree >= 360)
            plusdegree -= 360;
        if(degree == 0){
            if (dy >=0)
                return execute(next, plusdegree, dx,dy,countf,countv);
            else
                return execute(next, minusdegree, dx,dy,countf,countv);
        } else if(degree ==90){
            if (dx >=0)
                return execute(next, minusdegree, dx,dy,countf,countv);
            else
                return execute(next, plusdegree, dx,dy,countf,countv);

        } else if(degree == 180){
            if (dy >=0)
                return execute(next, minusdegree, dx,dy,countf,countv);
            else
                return execute(next, plusdegree, dx,dy,countf,countv);
        } else {
            if (dx >=0)
                return execute(next, plusdegree, dx,dy,countf,countv);
            else
                return execute(next, minusdegree, dx,dy,countf,countv);
        }
    }
}


int main(){
    string s;
    long long x,y;
    cin >> s >> x >> y;
    int v = countVertical(s, 0);
    int f = countF(s);
    int h = f - v;
    if (v%2 != abs(y)%2){
        cout << "No" << endl;
        return 0;
    }
    if (h%2 != abs(x)%2){
        cout << "No" << endl;
        return 0;
    }
    if(execute(s, 0, x, y, f, v))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}