Let's imagine the surface of Mars as an infinite coordinate plane. Initially, the rover…
Private research annotation
Sign in to add a private research label or note.
- User
Let's imagine the surface of Mars as an infinite coordinate plane. Initially, the rover Perseverance-2 and the helicopter Ingenuity-2 are located at the point with coordinates (0,0) . A set of instructions s consisting of n instructions of the following types was specially developed for them:
N: move one meter north (from point (x,y) to (x,y+1) ); S: move one meter south (from point (x,y) to (x,y−1) ); E: move one meter east (from point (x,y) to (x+1,y) ); W: move one meter west (from point (x,y) to (x−1,y) ). Each instruction must be executed either by the rover or by the helicopter. Moreover, each device must execute at least one instruction. Your task is to distribute the instructions in such a way that after executing all n instructions, the helicopter and the rover end up at the same point, or determine that this is impossible.
Input The first line of input contains t (1≤t≤104 ) — the number of test cases.
The first line of each test case contains a single integer n (1≤n≤2⋅105 ) — the number of instructions.
The second line of each test case contains a string s of length n consisting of the characters 'N', 'S', 'E', 'W' — the sequence of instructions.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 .
Output For each test case, if the required distribution of instructions exists, output a string p of length n consisting of the characters 'R', 'H'. If the i -th operation should be executed by the rover, then pi=R , if the i -th operation should be executed by the helicopter, then pi=H . If there are multiple solutions, output any of them.
Otherwise, output NO.
Example inputCopy 10 6 NENSNE 3 WWW 6 NESSWS 2 SN 2 WE 4 SSNN 4 WESN 2 SS 4 EWNN 4 WEWE outputCopy RRHRRH NO HRRHRH NO NO RHRH RRHH RH RRRH RRHH Note Let's consider the first example: the string S=NENSNE . One of the possible solutions, shown in the figure below, is p=RRHRRH , using which both the rover and the helicopter will end up one meter north and one meter east.
For WWW, the solution is impossible. Generate Python code for this.
Assistant
Here is a Python solution using a stack to keep track of the instructions. The basic idea is to first execute all the instructions that move the rover or the helicopter in the same direction, and then execute the remaining instructions in pairs moving in opposite directions.
pythont = int(input()) for _ in range(t): n = int(input()) s = input() stack = [] for i in range(n): if stack and stack[-1] + s[i] in ['NS', 'SN', 'EW', 'WE']: stack.pop() else: stack.append(s[i]) if len(stack) % 2 == 1: print('NO') else: p = '' for i in range(n): if len(stack) > 0 and stack[-1] + s[i] in ['NS', 'SN', 'EW', 'WE']: stack.pop() p += 'R' else: p += 'H' if len(stack) > 0: stack.pop() print(p)