Yet Another Walking Robot (Codeforces 1296C)
Tags: Implement, data structures

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'.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 $$$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".You have to answer $$$t$$$ independent test cases.

Input: The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 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 \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.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$$$).

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 \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.


Sample Inputs:
4
4
LRUD
4
LURD
5
RRUDU
5
LLDDR

Sample Outputs:
1 2
1 4
3 4
-1

Solution (Python):
from collections import defaultdict
t = int(input())
for _ in range(t):
    cnt = {}
    n = int(input())
    s = input().strip()
    sum_ = 0
    minn = 1000000000
    one = two = 0
    cnt[0] = 0
    for i in range(1, n + 1):
        if s[i - 1] == 'L':
            sum_ -= 1
        elif s[i - 1] == 'R':
            sum_ += 1
        elif s[i - 1] == 'U':
            sum_ += 1000000000
        elif s[i - 1] == 'D':
            sum_ -= 1000000000
        if sum_ in cnt and minn > i - cnt[sum_] + 1:
            (one, two) = (cnt[sum_] + 1, i)
            minn = i - cnt[sum_] + 1
        cnt[sum_] = i
    if minn == 1000000000:
        print('-1')
    else:
        print(one, two)

Solution (Python):
t = int(input())
for _ in range(t):
    n = int(input())
    s = input()
    d = dict([(0, 0)])
    ans = [0, 0]
    pref = 0
    for (i, c) in enumerate(s):
        if c == 'L':
            pref -= 1
        elif c == 'R':
            pref += 1
        elif c == 'D':
            pref -= 1 << 30
        else:
            pref += 1 << 30
        if d.__contains__(pref) and (ans[0] == 0 or i - d[pref] < ans[1] - ans[0]):
            ans = [d[pref] + 1, i + 1]
        d[pref] = i + 1
    if ans[0] == 0:
        ans = [-1]
    print(*ans)

Solution (Python):
I = input
for _ in [0] * int(I()):
    I()
    a = {1: 1}
    i = s = 1
    r = (-1,)
    m = 300000.0
    for c in I():
        s += (c == 'R') - (c == 'L') + 300000.0 * ((c > 'R') - (c < 'L'))
        j = a.get(s, -m)
        if i - j < m:
            m = i - j
            r = (j, i)
        i += 1
        a[s] = i
    print(*r)