• 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的情况

    1. # include
    2. # include
    3. # include
    4. # include
    5. # include
    6. using namespace std;
    7. int main ()
    8. {
    9. int t;
    10. cin>>t;
    11. while(t--)
    12. {
    13. string s;
    14. cin>>s;
    15. int cnt1=0,cnt2=0,cnt3=0,cnt4=0;
    16. for(int i=0;ilength();i++)
    17. {
    18. if(s[i]=='U')
    19. cnt1++;
    20. else if(s[i]=='D')
    21. cnt2++;
    22. else if(s[i]=='R')
    23. cnt3++;
    24. else
    25. cnt4++;
    26. }
    27. cnt1=min(cnt1,cnt2);
    28. cnt3=min(cnt3,cnt4);
    29. if(cnt1+cnt3==0)
    30. {
    31. cout<<0<
    32. }
    33. else if(cnt1==0)
    34. {
    35. cout<<2<
    36. cout<<"LR"<
    37. }
    38. else if(cnt3==0)
    39. {
    40. cout<<2<
    41. cout<<"UD"<
    42. }
    43. else
    44. {
    45. cout<<(cnt1+cnt3)*2<
    46. for(int i=1;i<=cnt1;i++)
    47. {
    48. cout<<'U';
    49. }
    50. for(int i=1;i<=cnt3;i++)
    51. {
    52. cout<<'R';
    53. }
    54. for(int i=1;i<=cnt1;i++)
    55. {
    56. cout<<'D';
    57. }
    58. for(int i=1;i<=cnt3;i++)
    59. {
    60. cout<<'L';
    61. }
    62. cout<
    63. }
    64. }
    65. return 0;
    66. }

     

  • 相关阅读:
    Springboot实战:员工管理系统
    基于SpringBoot的协同过滤算法商品推荐系统
    【MAPBOX基础功能】13、mapbox点击线图层高亮指定的线段
    初刷leetcode题目(5)——数据结构与算法
    队列的实现方式—Python数据结构(三)
    Spring Cloud Gateway 集成Sa-Token
    QT快捷键
    全志烧录工具LiveSuite使用注意事项
    2023火锅展/江西火锅食材展/中国火锅展/火锅用品展
    第六章【ADFS集成Exchang实现OWA\ECP单点登录SSO】验证AD联合身份验证(ADFS)是否配置成功
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126304554