B. Snow Walking Robot

B. Snow Walking Robot

B. Snow Walking Robot

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Example

input

Copy

6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL

output

Copy

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

=========================================================================

最终要求是删的数尽可能少,长度尽可能大,可以重新安排剩余的字母,这就好办了,我们可以采取“兜大弯”策略,也就是先一直上去,再右拐,再一直下去,再左拐,一定不会经过原点之外点两次,只需要记录左右最小值,上下最小值即可,当然,特判二者都是0,一者是0的情况

# include<iostream>
# include<vector>
# include<cstring>
# include<queue>
# include<algorithm>
using namespace std;


int main ()
{

   int t;

   cin>>t;

   while(t--)
   {
       string s;

       cin>>s;

       int cnt1=0,cnt2=0,cnt3=0,cnt4=0;

       for(int i=0;i<s.length();i++)
       {
           if(s[i]=='U')
            cnt1++;
           else if(s[i]=='D')
            cnt2++;
           else if(s[i]=='R')
            cnt3++;
           else
            cnt4++;
       }


       cnt1=min(cnt1,cnt2);

       cnt3=min(cnt3,cnt4);

       if(cnt1+cnt3==0)
       {
           cout<<0<<endl;
       }
       else if(cnt1==0)
       {
           cout<<2<<endl;

           cout<<"LR"<<endl;
       }
       else if(cnt3==0)
       {
           cout<<2<<endl;

           cout<<"UD"<<endl;
       }
       else
       {

           cout<<(cnt1+cnt3)*2<<endl;

           for(int i=1;i<=cnt1;i++)
           {
               cout<<'U';
           }

           for(int i=1;i<=cnt3;i++)
           {
               cout<<'R';
           }

           for(int i=1;i<=cnt1;i++)
           {
               cout<<'D';
           }

           for(int i=1;i<=cnt3;i++)
           {
               cout<<'L';
           }

           cout<<endl;
       }
   }

    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦三码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值