You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given three numbers A, B and C all having N digits, little Johnny needs to write a program to permute the digits of the number C so that the new number is greater than both A and B.
2
+
3
+
This problem would be so easy if Johnny's teacher didn't require that Johnny's program must be correct in both the real world and the reversed world. What happens once the world is reversed? One thing Johnny knows for sure is that all the numbers are also reversed. The second thing, the "greater than" relation becomes the "smaller than" relation.
4
+
5
+
For the sake of clarity, we will use aR to denote the number a in the reversed world (for instance, 113R = 311).
6
+
7
+
Thus Johnny task is now permute the digits of C so that the new number is greater than both A and B. In additional, the reverse of the new number should be smaller than both AR and BR. If there are multiple possible solutions, Johnny needs to find the smallest one.
8
+
9
+
As usual, please help Little Johnny with his task!
10
+
11
+
12
+
#Input
13
+
14
+
The first line contains a number T (about 10) which is the number of the test cases. Each test case has the following form.
15
+
16
+
The first line contains N (1 <= N <= 1000000).
17
+
18
+
The second line contains the number A.
19
+
20
+
The third line contains the number B.
21
+
22
+
The fourth line contains the number C.
23
+
24
+
Each test case's input is separated by a blank line.
25
+
Output
26
+
27
+
For each test case, print the solution in a single line. If there is no solution, print the number -1.
0 commit comments