Admissible Map
Time limit3sMemory limit512 MB
Count the substrings of a U/L/D/R string that, read row by row as a grid, form a map whose arrows make only cycles and stay inside the grid.
- Level
Medium7 of 10
- Topics
- String, Hash map, String matching
- Solved
- No attempts yet
Statement
A map is a matrix consisting of symbols from the set of U, L, D, and R.
A map graph of a map matrix is a directed graph with vertices numbered (), where is the number of rows and is the number of columns. The graph has directed edges , where , , , and . A map graph is valid when all edges point to valid vertices in the graph.
An admissible map is a map whose map graph is valid and consists of a set of cycles.
A description of a map is the concatenation of all rows of the map: the string .
You are given a string . Count the substrings of that can constitute a description of some admissible map. A substring is defined by a pair of indices and (, where is the length of ) and equals . Two substrings are different when their index pairs differ, even if they spell the same string.
Input
The only line contains a string of at least 1 and at most 20,000 symbols, each U, L, D, or R.
Output
Print one integer: the number of substrings of that constitute a description of some admissible map.
Hints
In the first example, two substrings can constitute a description of an admissible map: RDUL as a matrix (pic. 1) and DU as a matrix (pic. 2).

In the second example, no substring works. For instance, reading RDRU as a matrix gives a graph that is not a set of cycles (pic. 3).

In the third example, three substrings RL, two substrings RLRL, and one substring RLRLRL work, and some of them work in more than one way. For instance, RLRLRL works as a matrix (pic. 4) and as a matrix (pic. 5).
