626A.md (1398B)
1 Source: https://codeforces.com/problemset/problem/626/A 2 3 A. Robot Sequence 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Calvin the robot lies in an infinite rectangular grid. Calvin's source code contains a list of *n* commands, each either 'U', 'R', 'D', or 'L' — instructions to move a single square up, right, down, or left, respectively. How many ways can Calvin execute a non-empty contiguous substrings of commands and return to the same square he starts in? Two substrings are considered different if they have different starting or ending indices. 22 23 Input 24 25 The first line of the input contains a single positive integer, *n* (1 ≤ *n* ≤ 200) — the number of commands. 26 27 The next line contains *n* characters, each either 'U', 'R', 'D', or 'L' — Calvin's source code. 28 29 Output 30 31 Print a single integer — the number of contiguous substrings that Calvin can execute and return to his starting square. 32 33 Examples 34 35 Input 36 37 ``` 38 6 39 URLLDR 40 ``` 41 42 Output 43 44 ``` 45 2 46 ``` 47 48 Input 49 50 ``` 51 4 52 DLUU 53 ``` 54 55 Output 56 57 ``` 58 0 59 ``` 60 61 Input 62 63 ``` 64 7 65 RLRLRLR 66 ``` 67 68 Output 69 70 ``` 71 12 72 ``` 73 74 Note 75 76 In the first case, the entire source code works, as well as the "RL" substring in the second and third characters. 77 78 Note that, in the third case, the substring "LR" appears three times, and is therefore counted three times to the total result.