luogu#P5102. [JOI 2016 Final] 领地 / Territory
[JOI 2016 Final] 领地 / Territory
Problem Description
This problem is translated from JOI 2016 Final T4 “縄張り”.
There is a Cartesian coordinate plane. JOI is at .
Every day, JOI follows a fixed procedure and makes one round of moves. The procedure has steps and is described by a string of length . This string consists only of the uppercase letters .
The -th character from the left, , indicates where JOI will move. If before executing step , JOI is at :
- If , JOI moves to .
- If , JOI moves to .
- If , JOI moves to .
- If , JOI moves to .
On the next day, JOI starts executing the procedure from where he stopped the previous day.
JOI marks and every point he reaches after finishing each step. At the beginning, JOI has not marked any points. After days, for any integers , if the four points , , , have each been marked at least once, then the square with these four points as vertices belongs to JOI’s territory.
Find how many territories JOI has after days.
Input Format
The first line contains two integers , separated by spaces.
The second line contains a string , representing JOI’s movement procedure.
Output Format
Output one integer, the number of territories JOI has.
12 1
EENWSEEESWWS
3
12 2
EENWSEEESWWS
7
7 1
ENNWNNE
0
16 5
WSESSSWWWEEENNNW
21
Hint
Sample Explanation 1

The text in the figure is from the previous full statement, and I was too lazy to modify it. “市政厅” means , and “散步路径” means the movement path.
I did not carefully edit the image, so it may be misaligned or not symmetric. Please bear with it.
Sample Explanation 2
The difference between Sample 2 and Sample 1 is that in Sample 2, JOI walked for two days.

Constraints and Hints
For all testdata, , .
| Subtask # | Special Constraints | Points |
|---|---|---|
| 1 | 5 | |
| 2 | 10 | |
| 3 | 23 | |
| 4 | None | 62 |
Translated by ChatGPT 5