1296C.md (3032B)
1 Source: https://codeforces.com/problemset/problem/1296/C 2 3 C. Yet Another Walking Robot 4 5 time limit per test 6 7 1 second 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 There is a robot on a coordinate plane. Initially, the robot is located at the point $(0, 0)$. Its path is described as a string $s$ of length $n$ consisting of characters 'L', 'R', 'U', 'D'. 22 23 Each of these characters corresponds to some move: 24 25 * 'L' (left): means that the robot moves from the point $(x, y)$ to the point $(x - 1, y)$; 26 * 'R' (right): means that the robot moves from the point $(x, y)$ to the point $(x + 1, y)$; 27 * 'U' (up): means that the robot moves from the point $(x, y)$ to the point $(x, y + 1)$; 28 * 'D' (down): means that the robot moves from the point $(x, y)$ to the point $(x, y - 1)$. 29 30 The company that created this robot asked you to optimize the path of the robot somehow. To do this, you can remove any non-empty substring of the path. But this company doesn't want their customers to notice the change in the robot behavior. It means that if before the optimization the robot ended its path at the point $(x_e, y_e)$, then after optimization (i.e. removing some single substring from $s$) the robot also ends its path at the point $(x_e, y_e)$. 31 32 This optimization is a low-budget project so you need to remove the shortest possible non-empty substring to optimize the robot's path such that the endpoint of his path doesn't change. It is possible that you can't optimize the path. Also, it is possible that after the optimization the target path is an empty string (i.e. deleted substring is the whole string $s$). 33 34 Recall that the substring of $s$ is such string that can be obtained from $s$ by removing some amount of characters (possibly, zero) from the prefix and some amount of characters (possibly, zero) from the suffix. For example, the substrings of "LURLLR" are "LU", "LR", "LURLLR", "URL", but not "RR" and "UL". 35 36 You have to answer $t$ independent test cases. 37 38 Input 39 40 The first line of the input contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. 41 42 The next $2t$ lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the robot's path. The second line of the test case contains one string $s$ consisting of $n$ characters 'L', 'R', 'U', 'D' — the robot's path. 43 44 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$). 45 46 Output 47 48 For each test case, print the answer on it. If you cannot remove such non-empty substring that the endpoint of the robot's path doesn't change, print -1. Otherwise, print two integers $l$ and $r$ such that $1 \le l \le r \le n$ — endpoints of the substring you remove. The value $r-l+1$ should be minimum possible. If there are several answers, print any of them. 49 50 Example 51 52 Input 53 54 ``` 55 4 56 4 57 LRUD 58 4 59 LURD 60 5 61 RRUDU 62 5 63 LLDDR 64 ``` 65 66 Output 67 68 ``` 69 1 2 70 1 4 71 3 4 72 -1 73 ```