B. Snow Walking Robot

Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell (0,0)(0,0) on an infinite grid.

You also have the sequence of instructions of this robot. It is written as the string ss consisting of characters ‘L’, ‘R’, ‘U’ and ‘D’. If the robot is in the cell (x,y)(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)(x−1,y);
  • if the current instruction is ‘R’, then the robot can move to the right to (x+1,y)(x+1,y);
  • if the current instruction is ‘U’, then the robot can move to the top to (x,y+1)(x,y+1);
  • if the current instruction is ‘D’, then the robot can move to the bottom to (x,y−1)(x,y−1).

You’ve noticed the warning on the last page of the manual: if the robot visits some cell (except (0,0)(0,0)) twice then it breaks.

So the sequence of instructions is valid if the robot starts in the cell (0,0)(0,0), performs the given instructions, visits no cell other than (0,0)(0,0) two or more times and ends the path in the cell (0,0)(0,0). Also cell (0,0)(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)(0,0)) and “UUDD” (the cell (0,1)(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 qq independent test cases.Input

The first line of the input contains one integer qq (1≤q≤2⋅1041≤q≤2⋅104) — the number of test cases.

The next qq lines contain test cases. The ii-th test case is given as the string ss consisting of at least 11 and no more than 105105 characters ‘L’, ‘R’, ‘U’ and ‘D’ — the initial sequence of instructions.

It is guaranteed that the sum of |s||s| (where |s||s| is the length of ss) does not exceed 105105 over all test cases (∑|s|≤105∑|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 tt 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 00, you are allowed to print an empty line (but you can don’t print it).ExampleinputCopy

6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL

outputCopy

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


思路:其实这个题有点欧拉回路的感觉。不过更准确的是对称性,如果想回来那必定要保证有一个左就要有一个右,有一个上就要有一个下。同时要注意一些小特判,比如只有左右/上下的根据题意是不能重复走到这条边的。

那么我们找最少的上下和左右数就可以了

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e5+1000;
typedef long long LL;
LL L,R,U,D;
int main(void)
{
  LL t;cin>>t;
  while(t--)
  {
  	string str;cin>>str;
  	L=R=U=D=0;
	for(LL i=0;i<str.size();i++)
	{
		if(str[i]=='L') L++;
		if(str[i]=='R') R++;
		if(str[i]=='U') U++;
		if(str[i]=='D') D++;	
	}   

	LL t=min(L,R);
	LL k=min(U,D);
	if(t!=0&&k!=0)
	{
	cout<<2*t+2*k<<endl;
	for(LL i=1;i<=t;i++) cout<<"L";
	for(LL i=1;i<=k;i++) cout<<"U";
	for(LL i=1;i<=t;i++) cout<<"R";
	for(LL i=1;i<=k;i++) cout<<"D";
	cout<<endl;
 	}
 	else if(t==0&&k!=0)
 	{
 		cout<<2<<endl;
 		cout<<"U"<<"D"<<endl;	
	}
	else if(t!=0&&k==0)
	{
		cout<<2<<endl;
		cout<<"L"<<"R"<<endl;
	}
	else cout<<0<<endl;
  }
return 0;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值