Command Sequence
\Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 792 Accepted Submission(s): 279
**
Problem Description
There is a robot that can move by receiving a sequence of commands.
There are four types of command in the command sequence:
- U: robot moves one unit up.
- D: robot moves one unit down.
- L: robot moves one unit left.
- R: robot moves one unit right.
Now, given a command sequence of length n. You need to find out how many substrings of the command sequence satisfy that if the robot execute the substring command, it can return to the starting position.
A substring is a contiguous sequence of characters within a string. For instance, “the best of” is a substring of “It was the best of times”. For example, “Itwastimes” is a subsequence of “It was the best of times”, but not a substring.
Input
This problem contains multiple test cases.
The first line contains an integer t (1≤t≤20) indicating the number of test cases.
For each test case, the first line contains one integer n (2≤n≤105).
The second line contains a UDLR string of length n.
Output
For each test case, output one line one integer indicating the answer.
Sample Input
1 | 1 |
Sample Output
1 | 2 |
Source
Recommend
IceyWang | We have carefully selected several similar problems for you: 7112 7111 7110 7109 7108
解题思路
如果以当前位置为起点,如果可以再次到达当前的位置那么就可以认为从当前位置可以回到当前的位置。
只需要记录从当前的位置到下次当前位置的数量即可知道子数组的数量。
1 | // |