Codeforces Round #605 (Div. 3) B

B. Snow Walking Robot
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

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

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

Example
input
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL
output
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0

Note
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”.

题目大意:指挥机器人移动后回到原点,机器人除了回到原点外不等重复。
思路:贪心,将前后左右距离全部算出,分别取 R与L 及 U与D 最小值,这样刚好能回到原点。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
int main(){
	int t;
	cin>>t;
	while(t--)
	{
		string s;
		cin>>s;
		int n=s.size();
		int l=0,r=0,u=0,d=0;
		for(int i=0;i<n;i++)
		{
			if(s[i]=='L')
				l++;
			if(s[i]=='R')
				r++;
			if(s[i]=='U')
				u++;
			if(s[i]=='D')
				d++;
		}
		l=min(l,r);
		r=l;
		u=min(u,d);
		d=u;
		int sum=l+r+u+d;
		if(l&&!u)
		{
			sum=2;
			printf("%d\n",sum);
			printf("LR\n");
			continue;
		}
		if(!l&&u)
		{
			sum=2;
			printf("%d\n",sum);
			printf("UD\n");
			continue;
		}
		int vis=0;
		if(sum==0)
			vis=1;
		printf("%d\n",sum);
		while(l--)
			printf("L");
		while(u--)
			printf("U");
		while(r--)
			printf("R");
		while(d--)
			printf("D");
		if(vis)
			printf("\n");
		printf("\n");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值