CodeForces-1272B ---- Snow Walking Robot

Snow Walking Robot

题目来源: CodeForces - 1272B 点击进入原题

Description:

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

Note

题目大意:

有一个机器人,你需要通过一系列的指令(UDRL)来控制它的运动,现在给你一段指令,你可以通过这段指令中的全部或者一部分,来进行判断机器人是否能够走出去后再次回到原点,并且这个过程中,除了起点以外的点都不能重复经过。

解题思路:

我们只需要让机器人走出一个矩形,他就可以做到走出去后再次回到原点,并且满足除原点以外,其他点只经过一次。但是如果命令中只出现了上下,或者左右这两种指令,那么我们只能进行一次上下或者左右。其他情况,则必定是输出0。

AC代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
char s[maxn];
void solve()
{
	int n;
	scanf("%d",&n);
	while(n--){
		scanf("%s",&s);
		int len=strlen(s);
		int up=0,down=0,l=0,r=0;
		for(int i=0;i<len;i++){
			switch(s[i])
			{
				case 'U': up++;break;
				case 'D': down++;break;
				case 'R': l++;break;
				case 'L': r++;break;
			}
		}
		int ud=min(up,down);
		int lr=min(l,r);
		int cnt=0;
		if(ud!=0 && lr!=0 ){
			cnt=(ud+lr)*2;
			printf("%d\n",cnt);
			for(int i=0;i<lr;i++) printf("L");
			for(int i=0;i<ud;i++) printf("U");
			for(int i=0;i<lr;i++) printf("R");
			for(int i=0;i<ud;i++) printf("D");
		}
		else if(ud==0 && lr!=0){
			printf("2\n");
			printf("LR");
		}
		else if(lr==0 && ud!=0){
			printf("2\n");
			printf("UD");
		}
		else{
			printf("0");
		}
		printf("\n");
	}
	return ;
}
int main()
{
	solve();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值