ORIGIN

CodeForces-1296 C Yet Another Walking Robot

ACM 5 mins817 words
C. Yet Another Walking Robot
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output:standard output

There is a robot on a coordinate plane. Initially, the robot is located at the point (0,0)(0,0). Its path is described as a string ss of length nn consisting of characters ‘L’, ‘R’, ‘U’, ‘D’.

Each of these characters corresponds to some move:

  • ‘L’ (left): means that the robot moves from the point $(x,y)$ to the point $(x−1,y)$;
  • ‘R’ (right): means that the robot moves from the point $(x,y)$ to the point $(x+1,y)$;
  • ‘U’ (up): means that the robot moves from the point $(x,y)$ to the point $(x,y+1)$;
  • ‘D’ (down): means that the robot moves from the point $(x,y)$ to the point $(x,y−1)$.

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)$.

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$).

Recall that the substring of ss 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”.

You have to answer $t$ independent test cases.

Input

The first line of the input contains one integer $t$ $(1≤t≤1000)$ — the number of test cases.

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≤n≤2⋅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.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2⋅10^5$ $(∑n≤2⋅105)$.

Output

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≤l≤r≤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.

Example

input

1
2
3
4
5
6
7
8
9
4
4
LRUD
4
LURD
5
RRUDU
5
LLDDR

output

1
2
3
4
1 2
1 4
3 4
-1

Analysis

Stimulate the walking process.

Using map<pair<int,int>, int> to store the points. (The second parameter of map stands for the times of the move, that is the position in the string where it meets a certain points)

Also record the shortest l - r and pair<l, r>

Loop the string and compare the minimum at the same time. And then print the minimum.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<bits/stdc++.h>

using namespace std;
typedef pair<int,int> P;
map<P,int> a;

int main() {
int t;
cin >> t;
while(t --) {
a.clear();
int n; string s;
cin >> n >> s;
int x = 0, y = 0;
int min = 1e9;
P flag = P(0, 0);
a[flag] = 0;
for (int i = 0; i < n; i ++) {
if (s[i] == 'U') {
y ++;
} else if (s[i] == 'D') {
y --;
} else if (s[i] == 'L') {
x --;
} else x ++;
if(a[P(x, y)] != 0 or (P(x, y) == P(0, 0) and a[P(x, y)] == 0)) { // if it's visited or not
if(i - a[P(x, y)] + 1 < min) { // compare the minimum
min = i - a[P(x, y)] + 1;
flag = P(a[P(x, y)] + 1, i + 1);
}
}
a[P(x, y)] = i + 1; // record the point and the count starts from 1
}
if (flag != P(0, 0)) {
cout << flag.first << " " << flag.second << endl;
} else cout << -1 << endl;
}
return 0;
}
TOP
COMMENT
  • ABOUT
  • |
o_oyao
  The Jigsaw puzzle is incomplete with even one missing piece. And I want to be the last piece to make the puzzle complete.
Like my post?
Default QR Code
made with ❤️ by o_oyao
©o_oyao 2019-2024

|