Flipped Course

View as PDF

Submit solution

Points: 100
Time limit: 2.0s
PyPy 3 3.0s
Python 3 3.0s
Memory limit: 1G

Author:
Problem type

Phoebe volunteered to navigate the kayaking event because, in her words, "a map is just a very small grid." After arriving nowhere near the finish line, she admits that she held the map upside down for exactly one nonempty consecutive section of the course. She cannot remember when she turned it over, but she is quite sure this explains everything.

The kayak starts at coordinates (0, 0). The intended course consists of N moves, each one metre north, south, east, or west. During the section when the map was upside down, each move was taken in the opposite direction: north became south, south became north, east became west, and west became east. All moves outside that section were taken correctly, and the moves were always performed in their original order.

Given the intended course and the kayak's actual finishing coordinates (X, Y), count how many sections could explain the result. A section is identified by its first and last move, so two sections are different if either endpoint differs. If no section could produce the given finish, output 0.

Input

The first line contains an integer N (1 \le N \le 1 \times 10^6), the number of moves.

The second line contains a string S of length N, describing the intended moves in order:

  • N increases the y-coordinate by 1.
  • S decreases the y-coordinate by 1.
  • E increases the x-coordinate by 1.
  • W decreases the x-coordinate by 1.

The third line contains two integers X and Y (-N \le X, Y \le N), the actual finishing coordinates.

Output

Print a single integer: the number of nonempty consecutive sections for which taking every move in the opposite direction would make the kayak finish at (X, Y).

Example 1

Input
4
NSNS
0 0
Output
4
Explanation

The possible sections are moves 1 to 2, 2 to 3, 3 to 4, and 1 to 4. Each has zero net displacement, so reversing its directions leaves the finish unchanged. The empty section is not allowed and is not counted.

Example 2

Input
1
E
-1 0
Output
1
Explanation

The only nonempty section is the entire course. Reversing its one move takes the kayak west instead of east.

Example 3

Input
2
NE
0 0
Output
0
Explanation

Reversing only the first move finishes at (1, -1), reversing only the second finishes at (-1, 1), and reversing both finishes at (-1, -1). None reaches (0, 0).


Comments

There are no comments at the moment.