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 Go (1.6) (judge verdict: Wrong Answer)

package main

import (
	"fmt"
	"strings"
)

const Right = 1
const Up = 2
const Left = 3
const Down = 4

var F = "F"[0]
var T = "T"[0]
var prefixes []int
var tx, ty int
var slength int

// 2018-08-02T12:33:59+0800
// 2018-08-02T12:43:06+0800
// 2018-08-02T13:01:55+0800
// dfs1() -> @see https://arc087.contest.atcoder.jp/submissions/2933611
// 2018-08-02T13:32:53+0800
// dfs() -> @see https://arc087.contest.atcoder.jp/submissions/2933712
func main() {
	var s, news string
	fmt.Scanf("%s", &s)
	fmt.Scanf("%d%d", &tx, &ty)
	// Replace 4T to 2T.
	for ; true; {
		news = strings.Replace(s, "TTT", "T", -1)
		if len(news) == len(s) {
			break
		}
		s = news
	}
	slength = len(s)
	// Calculate the prefixes of 'F'.
	prefixes = make([]int, len(s))
	if s[len(s)-1] == F {
		prefixes[len(s)-1] = 1
	}
	for i := len(s) - 2; i >= 0; i-- {
		if s[i] == F {
			prefixes[i] = prefixes[i+1] + 1
		} else {
			prefixes[i] = prefixes[i+1]
		}
	}
	if dfs(s, Right, 0, 0) {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}

func dfs(s string, direction, x, y int) bool {
	for ; true; {
		if len(s) == 0 {
			if x == tx && y == ty {
				return true
			} else {
				return false
			}
		}
		if s[0] == F {
			switch direction {
			case Right:
				x += 1
			case Up:
				y += 1
			case Left:
				x -= 1
			case Down:
				y -= 1
			}
			s = s[1:]
		} else {
			if slength > len(s) && prefixes[slength-len(s)-1] < (abs(x-tx)+abs(y-ty)) {
				return false
			}
			if len(s) > 1 && s[1] == T {
				a := direction + 3
				if a > 4 {
					a = a - 4
				}
				return dfs(s[2:], direction, x, y) || dfs(s[2:], a, x, y)
			}
			a, b := direction+1, direction+3
			if a > 4 {
				a = a - 4
			}
			if b > 4 {
				b = b - 4
			}
			return dfs(s[1:], a, x, y) || dfs(s[1:], b, x, y)
		}
	}
	return false
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func dfs1(s string, direction, x, y, tx, ty int) bool {
	for ; true; {
		if len(s) == 0 {
			if x == tx && y == ty {
				return true
			} else {
				return false
			}
		}
		if s[0] == F {
			switch direction {
			case Right:
				x += 1
			case Up:
				y += 1
			case Left:
				x -= 1
			case Down:
				y -= 1
			}
			s = s[1:]
		} else {
			a, b := direction+1, direction+3
			if a > 4 {
				a = a - 4
			}
			if b > 4 {
				b = b - 4
			}
			return dfs1(s[1:], a, x, y, tx, ty) || dfs1(s[1:], b, x, y, tx, ty)
		}
	}
	return false
}