Snow Walking Robot (Codeforces 1272B)
Tags: Greedy, constructive algorithms

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 \le q \le 2 \cdot 10^4$$$) — 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 at least $$$1$$$ and no more than $$$10^5$$$ 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 $$$10^5$$$ over all test cases ($$$\sum |s| \le 10^5$$$).

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

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

Sample Inputs:
6
LRU
DURLDRUDRULRDURDDL
LRUDDLRUDRUL
LLLLRRRR
URDUR
LLL

Sample Outputs:
2
LR
14
RUURDDDDLLLUUR
12
ULDDDRRRUULL
2
LR
2
UD
0

Solution (Python):
q = int(input())
instructions = []
for _ in range(q):
    instructions.append(input())

def func(instruction):
    L = 0
    R = 0
    U = 0
    D = 0
    for i in range(len(instruction)):
        if instruction[i] == 'L':
            L += 1
        if instruction[i] == 'R':
            R += 1
        if instruction[i] == 'U':
            U += 1
        if instruction[i] == 'D':
            D += 1
    if L < R:
        R = L
    else:
        L = R
    if U < D:
        D = U
    else:
        U = D
    if L == 0 and U != 0:
        print(2)
        print('UD')
    elif L != 0 and U == 0:
        print(2)
        print('LR')
    elif L == 0 and U == 0:
        print('0')
    else:
        print(2 * L + 2 * U)
        ins = 'L' * L + 'U' * U + 'R' * R + 'D' * D
        print(ins)
for instruction in instructions:
    func(instruction)

Solution (Python):
q = int(input())
lst = []
for i in range(q):
    lst.append(input())
for x in lst:
    u = x.count('U')
    v = x.count('D')
    l = x.count('L')
    r = x.count('R')
    a = min(u, v)
    b = min(l, r)
    if a < 1:
        b = b and 1
    if b < 1:
        a = a and 1
    print(2 * (a + b))
    print('U' * a + 'L' * b + 'D' * a + 'R' * b)

Solution (Python):
q = int(input())
r = []
for i in range(q):
    t = input()
    u = list(set(t))
    u.sort()
    if len(u) == 1:
        r.append(0)
        r.append('')
    elif len(u) == 2:
        if u == ['L', 'R']:
            r.append(2)
            r.append('LR')
        elif u == ['D', 'U']:
            r.append(2)
            r.append('DU')
        else:
            r.append(0)
            r.append('')
    elif len(u) == 3:
        if t.find('L') != -1:
            if t.find('R') != -1:
                r.append(2)
                r.append('LR')
            else:
                r.append(2)
                r.append('DU')
        else:
            r.append(2)
            r.append('DU')
    else:
        f1 = t.count('L')
        f2 = t.count('R')
        f3 = t.count('U')
        f4 = t.count('D')
        f5 = min(f1, f2)
        f6 = min(f3, f4)
        p1 = ['L'] * f5
        p2 = ['U'] * f6
        p3 = ['R'] * f5
        p4 = ['D'] * f6
        p = p1 + p2 + p3 + p4
        listToStr = ''.join([str(elem) for elem in p])
        r.append(len(listToStr))
        r.append(listToStr)
for i in range(len(r)):
    print(r[i])