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: Time Limit Exceeded)

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 tx, ty 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
// dfs() -> @see https://arc087.contest.atcoder.jp/submissions/2933766
// 2018-08-02T13:57:26+0800 # Giving up and looking for help.
// 2018-08-02T14:04:20+0800
// 2018-08-02T15:14:25+0800
func main() {
	var s string
	fmt.Scanf("%s", &s)
	fmt.Scanf("%d%d", &tx, &ty)
	s = "F" + s + "TF"
	// how many continuous f, how many continuous t, ...
	// Length of vh will be ii+1.
	vh := make([]int, len(s)+1)
	// Length of dx will be idx.
	dx := make([]int, len(s)+1)
	// Length of dy will be idy.
	dy := make([]int, len(s)+1)
	isx := true
	idx, idy := 0, 0
	ii := 0
	vh[0] = 0
	for i := 1; i < len(s); i++ {
		if s[i] != s[i-1] {
			if s[i] == F {
				if isx {
					dx[idx] = vh[ii-1]
					idx ++
				} else {
					dy[idy] = vh[ii-1]
					idy ++
				}
				if vh[ii]%2 != 0 {
					isx = !isx
				}
			}
			ii++
		}
		vh[ii]++
	}
	x, y := vh[0], 0
	// x -> tx
	// y -> ty
	if dp(dx[1:idx], x) && dpy(dy[0:idy], y) {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}

func dp(dx []int, x int) bool {
	if len(dx) == 0 {
		return x == tx
	}
	return dp(dx[1:], x+dx[0]) || dp(dx[1:], x-dx[0])
}

func dpy(dy []int, y int) bool {
	if len(dy) == 0 {
		return y == ty
	}
	return dpy(dy[1:], y+dy[0]) || dpy(dy[1:], y-dy[0])
}

var prefixes []int
var slength int

func main1() {
	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 + 2
				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
}