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: Accepted)

#![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 start = 0;
    for &c in &cs {
        if c == 'F' {
            start += 1;
        } else {
            break;
        }
    }
    if start == cs.len() {
        if (gx, gy) == (cs.len() as isize, 0) {
            println!("Yes");
        } else {
            println!("No");
        }
        return;
    }

    let gx = gx - start as isize;

    let mut xs = vec![];
    let mut ys = vec![];

    let mut f_len = 0;
    let mut dir = 0;
    for i in start..cs.len() {
        let c = cs[i];
        if c == 'F' {
            f_len += 1;
        } else {
            if f_len > 0 {
                if dir == 0 {
                    xs.push(f_len);
                } else {
                    ys.push(f_len);
                }
            }
            dir = 1 - dir;
            f_len = 0;
        }
    }

    if f_len > 0 {
        if dir == 0 {
            xs.push(f_len);
        } else {
            ys.push(f_len);
        }
    }

    let mut x_set: HashSet<isize> = HashSet::new();
    x_set.insert(0);
    for &x in &xs {
        let mut s: HashSet<isize> = HashSet::new();
        for &ex in &x_set {
            s.insert(ex + x);
            s.insert(ex - x);
        }
        x_set = s;
    }

    let mut y_set: HashSet<isize> = HashSet::new();
    y_set.insert(0);
    for &y in &ys {
        let mut s: HashSet<isize> = HashSet::new();
        for &ey in &y_set {
            s.insert(ey + y);
            s.insert(ey - y);
        }
        y_set = s;
    }

    if x_set.contains(&gx) && y_set.contains(&gy) {
        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]))
    }
}