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 Rust (1.15.1) (judge verdict: Time Limit Exceeded)

#![allow(unused_imports, unused_macros, dead_code)]
use std::f64::*;
use std::cmp::*;
use std::collections::*;

macro_rules! dump{
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

const INF: i64 = 0x3f3f3f3f3f3f3f3f;
const MOD: i64 = 1000000007;
const NOTHING: usize = std::usize::MAX;
const DX: [usize; 4] = [1, 0, std::usize::MAX, 0];
const DY: [usize; 4] = [0, 1, 0, std::usize::MAX];

fn main() {
    let cs: Vec<char> = readln();
    let (gx, gy): (isize, isize) = readln();
    let mut f_len = 0;
    let mut t_len = 0;
    let mut s: HashSet<(isize, isize, usize)> = HashSet::new();
    s.insert((0, 0, 0));
    for &c in &cs {
        if c == 'F' {
            if t_len == 0 {
                f_len += 1;
                continue;
            } else {
                f_len += 1;
                let mut ns: HashSet<(isize, isize, usize)> = HashSet::new();
                if t_len == 1 {
                    for &(x, y, d) in &s {
                        let nd = (d + 1) % 4;
                        ns.insert((x, y, nd));
                        let nd = (d + 3) % 4;
                        ns.insert((x, y, nd));
                    }
                } else {
                    for &(x, y, _) in &s {
                        for d in 0..4 {
                            ns.insert((x, y, d));
                        }
                    }
                }
                t_len = 0;
                s = ns;
            }
        } else {
            if f_len == 0 {
                t_len += 1;
                continue;
            } else {
                let mut ns: HashSet<(isize, isize, usize)> = HashSet::new();
                t_len += 1;
                for &(x, y, d) in &s {
                    ns.insert((x + f_len * DX[d] as isize, y + f_len * DY[d] as isize, d));
                }
                f_len = 0;
                s = ns;
            }
        }
    }
    if f_len > 0 {
        let mut ns: HashSet<(isize, isize, usize)> = HashSet::new();
        for &(x, y, d) in &s {
            ns.insert((x + f_len * DX[d] as isize, y + f_len * DY[d] as isize, d));
        }
        s = ns;
    }

    for d in 0..4 {
        if s.contains(&(gx, gy, d)) {
            println!("Yes");
            return;
        }
    }
    println!("No");
}

trait Read {
    fn read(s: &str) -> Self;
}

fn readln<T: Read>() -> T {
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).unwrap();
    T::read(buf.trim_right())
}

macro_rules! read_impl{
    ($($t:ty)*) => ($(
        impl Read for $t {
            fn read(s: &str) -> $t{
                s.parse().unwrap()
            }
        }
    )*)
}
read_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }

impl Read for String {
    fn read(s: &str) -> Self {
        s.to_string()
    }
}

impl Read for Vec<char> {
    fn read(s: &str) -> Self {
        s.chars().collect()
    }
}

impl<T: Read> Read for Vec<T> {
    fn read(s: &str) -> Self {
        s.split_whitespace().map(T::read).collect()
    }
}

impl<A: Read, B: Read> Read for (A, B) {
    fn read(s: &str) -> Self {
        let tokens: Vec<_> = s.split_whitespace().collect();
        (A::read(tokens[0]), B::read(tokens[1]))
    }
}

impl<A: Read, B: Read, C: Read> Read for (A, B, C) {
    fn read(s: &str) -> Self {
        let tokens: Vec<_> = s.split_whitespace().collect();
        (A::read(tokens[0]), B::read(tokens[1]), C::read(tokens[2]))
    }
}

impl<A: Read, B: Read, C: Read, D: Read> Read for (A, B, C, D) {
    fn read(s: &str) -> Self {
        let tokens: Vec<_> = s.split_whitespace().collect();
        (A::read(tokens[0]), B::read(tokens[1]), C::read(tokens[2]), D::read(tokens[3]))
    }
}