CF1296C.Yet Another Walking Robot

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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)(x, y) to the point (x−1,y)(x - 1, y);
  • 'R' (right): means that the robot moves from the point (x,y)(x, y) to the point (x+1,y)(x + 1, y);
  • 'U' (up): means that the robot moves from the point (x,y)(x, y) to the point (x,y+1)(x, y + 1);
  • 'D' (down): means that the robot moves from the point (x,y)(x, y) to the point (x,y−1)(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 (xe,ye)(x_e, y_e), then after optimization (i.e. removing some single substring from ss) the robot also ends its path at the point (xe,ye)(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 ss).

Recall that the substring of ss is such string that can be obtained from ss 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 tt independent test cases.

坐标平面上有一个机器人。初始时,机器人位于点 (0,0)(0, 0)。其运动路径由一个长度为 nn 的字符串 ss 描述,该字符串仅由字符 'L'、'R'、'U'、'D' 组成。

每个字符对应一次移动:

  • 'L'(向左):表示机器人从点 (x,y)(x, y) 移动到点 (x−1,y)(x - 1, y);
  • 'R'(向右):表示机器人从点 (x,y)(x, y) 移动到点 (x+1,y)(x + 1, y);
  • 'U'(向上):表示机器人从点 (x,y)(x, y) 移动到点 (x,y+1)(x, y + 1);
  • 'D'(向下):表示机器人从点 (x,y)(x, y) 移动到点 (x,y−1)(x, y - 1)。

开发该机器人的公司要求你以某种方式优化机器人的路径。为此,你可以删除路径中任意一个非空子串。但该公司不希望客户察觉机器人行为发生改变。这意味着:若优化前机器人最终停在点 (xe,ye)(x_e, y_e),则优化后(即从 ss 中删除某个单一子串)机器人也必须最终停在点 (xe,ye)(x_e, y_e)。

由于该项目预算有限,你需要删除最短的可能的非空子串,使得机器人的路径终点保持不变。有可能无法对路径进行优化。此外,优化后的路径也可能变成空字符串(即被删除的子串恰好是整个字符串 ss)。

注意:字符串 ss 的子串是指通过从 ss 的前缀删除若干字符(可能为零个)并从其后缀删除若干字符(可能为零个)所得到的字符串。例如,"LURLLR" 的子串包括 "LU"、"LR"、"LURLLR"、"URL",但不包括 "RR" 和 "UL"。

你需要回答 tt 个相互独立的测试用例。

输入格式

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

The next 2t2t lines describe test cases. Each test case is given on two lines. The first line of the test case contains one integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the length of the robot's path. The second line of the test case contains one string ss consisting of nn characters 'L', 'R', 'U', 'D' — the robot's path.

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

输入的第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000),表示测试用例的数量。

接下来的 2t2t 行描述了各测试用例。每个测试用例占两行:第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5),表示机器人路径的长度;第二行包含一个由 nn 个字符组成的字符串 ss,其中每个字符为 'L'、'R'、'U' 或 'D',表示机器人的移动路径。

保证所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5(即 ∑n≤2⋅105\sum n \le 2 \cdot 10^5)。

输出格式

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 ll and rr such that 1≤l≤r≤n1 \le l \le r \le n — endpoints of the substring you remove. The value r−l+1r-l+1 should be minimum possible. If there are several answers, print any of them.

对于每个测试用例,请输出对应的答案。如果无法移除一个非空子串,使得机器人路径的终点保持不变,则输出 -1。否则,请输出两个整数 ll 和 rr,满足 1≤l≤r≤n1 \le l \le r \le n,表示你要移除的子串的左右端点。要求 r−l+1r-l+1 尽可能小。若存在多个满足条件的答案,输出任意一个即可。

输入输出样例

  • 输入#1

    4
    4
    LRUD
    4
    LURD
    5
    RRUDU
    5
    LLDDR

    输出#1

    1 2
    1 4
    3 4
    -1

输入解题思路,AI测评打分。不知道怎么写?

首页