CF1272B.Snow Walking Robot
普及-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0,0) on an infinite grid.
You also have the sequence of instructions of this robot. It is written as the string s consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell (x,y) right now, he can move to one of the adjacent cells (depending on the current instruction).
- If the current instruction is 'L', then the robot can move to the left to (x−1,y);
- if the current instruction is 'R', then the robot can move to the right to (x+1,y);
- if the current instruction is 'U', then the robot can move to the top to (x,y+1);
- if the current instruction is 'D', then the robot can move to the bottom to (x,y−1).
You've noticed the warning on the last page of the manual: if the robot visits some cell (except (0,0)) twice then it breaks.
So the sequence of instructions is valid if the robot starts in the cell (0,0), performs the given instructions, visits no cell other than (0,0) two or more times and ends the path in the cell (0,0). Also cell (0,0) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: "UD", "RL", "UUURULLDDDDLDDRRUU", and the following are considered invalid: "U" (the endpoint is not (0,0)) and "UUDD" (the cell (0,1) is visited twice).
The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move.
Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.
Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).
You have to answer q independent test cases.
最近,你购买了一台雪地行走机器人并把它带回了家。假设你的家位于无限网格上的一个格子 (0,0)。
你还拥有该机器人的指令序列,它是一个由字符 'L'、'R'、'U' 和 'D' 组成的字符串 s。若机器人当前位于格子 (x,y),则它可根据当前指令移动至相邻的一个格子:
- 若当前指令为
'L',则机器人可向左移动至 (x−1,y); - 若当前指令为
'R',则机器人可向右移动至 (x+1,y); - 若当前指令为
'U',则机器人可向上移动至 (x,y+1); - 若当前指令为
'D',则机器人可向下移动至 (x,y−1)。
你在说明书最后一页注意到一则警告:若机器人访问了某个格子(除 (0,0) 外)两次或以上,则机器人将损坏。
因此,一个指令序列被称为有效,当且仅当:机器人从格子 (0,0) 出发,依次执行给定指令,除起点 (0,0) 外不重复访问任何格子,且最终停在格子 (0,0);此外,格子 (0,0) 最多只能被访问两次——一次在路径开始时,一次在路径结束时(若路径为空,则仅访问一次)。例如,以下指令序列是有效的:"UD"、"RL"、"UUURULLDDDDLDDRRUU";而以下指令序列是无效的:"U"(终点不是 (0,0)),以及 "UUDD"(格子 (0,1) 被访问了两次)。
然而,初始的指令序列可能并不有效。为了避免机器人损坏,你决定以如下方式重新编程:从初始指令序列中删除若干(可能全部、也可能一个不删)指令,然后将剩余指令按任意顺序重新排列,再启动机器人执行。
你的任务是:尽可能少地删除初始序列中的指令,使得剩余指令经重排后构成一个有效序列。请输出你能得到的最长有效序列。
注意:你可以自由选择剩余指令的任意排列顺序(无需最小化交换次数或其他类似指标)。
你需要回答 q 个相互独立的测试用例。
输入格式
The first line of the input contains one integer q (1≤q≤2⋅104) — the number of test cases.
The next q lines contain test cases. The i-th test case is given as the string s consisting of at least 1 and no more than 105 characters 'L', 'R', 'U' and 'D' — the initial sequence of instructions.
It is guaranteed that the sum of ∣s∣ (where ∣s∣ is the length of s) does not exceed 105 over all test cases (∑∣s∣≤105).
输入的第一行包含一个整数 q(1≤q≤2⋅104),表示测试用例的数量。
接下来的 q 行包含各测试用例。第 i 个测试用例以字符串 s 给出,该字符串由至少 1 个、至多 105 个字符组成,每个字符为 'L'、'R'、'U' 或 'D',表示初始的指令序列。
保证所有测试用例中 ∣s∣(即字符串 s 的长度)之和不超过 105(即 ∑∣s∣≤105)。
输出格式
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions t the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is 0, you are allowed to print an empty line (but you can don't print it).
对每个测试用例,输出其答案。第一行输出剩余指令的最大数量;第二行输出机器人需要执行的有效剩余指令序列 t。指令按所打印序列从左到右依次执行。若存在多个答案,输出任意一个即可。若答案为 0,允许输出空行(但也可不输出)。
输入输出样例
输入#1
6 LRU DURLDRUDRULRDURDDL LRUDDLRUDRUL LLLLRRRR URDUR LLL
输出#1
2 LR 14 RUURDDDDLLLUUR 12 ULDDDRRRUULL 2 LR 2 UD 0
说明/提示
There are only two possible answers in the first test case: "LR" and "RL".
The picture corresponding to the second test case:
Note that the direction of traverse does not matter
Another correct answer to the third test case: "URDDLLLUURDR".
第一个测试用例中仅有两种可能的答案:“LR”和“RL”。
第二个测试用例对应的图片:
注意:遍历方向无关紧要。
第三个测试用例的另一个正确答案:“URDDLLLUURDR”。
输入解题思路,AI测评打分。不知道怎么写?