-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path1296C.cpp
More file actions
executable file
·51 lines (44 loc) · 784 Bytes
/
1296C.cpp
File metadata and controls
executable file
·51 lines (44 loc) · 784 Bytes
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
40
41
42
43
44
45
46
47
48
49
50
51
// Problem Code: 1296C
#include <iostream>
#include <string>
#include <map>
#include <utility>
using namespace std;
void optimize_robot(int n, string& s) {
int l, r;
pair<int, int> p = {0, 0};
map<pair<int, int>, int> m;
r = n;
m[p] = l = -1;
for (int i = 0; i < n; m[p] = i++) {
// move robot to new position
if (s[i] == 'L')
p.first--;
else if (s[i] == 'R')
p.first++;
else if (s[i] == 'D')
p.second--;
else
p.second++;
// if cycle found, update distance
if (m.count(p) && i - m[p] < r - l + 1) {
l = m[p] + 1;
r = i;
}
}
if (l == -1)
cout << -1 << endl;
else
cout << l + 1 << " " << r + 1 << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
string s;
cin >> n >> s;
optimize_robot(n, s);
}
return 0;
}