Minimum Longest Substring

 You are given a binary string 

of length . Rearrange the binary string to form binary strings and such that the length of the longest common substring between and

is minimum.

Print both strings

and

.

Note that both

and must be anagrams of

— see the examples below for more clarity.

Input Format

  • The first line of input contains a single integer
, denoting the number of test cases. The description of
  • test cases follows.
  • Each test case consists of two lines.
  • The first line of each test case contains a single integer
  • — the length of the binary string
  • .
  • The second line of each test case contains a binary string
  • of length
    • .

    Output Format

    • For each test case, output two lines. The first line contains string
    while the second line contains string . Both strings and must be anagrams of string and thus, have length
    • .

    Constraints

  • Sum of
  • over all cases does not exceed
    • .

    Subtasks

    Subtask #1 (100 points): Original constraints

    Sample Input 1

    3
    3
    101
    4
    1111
    4
    0111
    

    Sample Output 1

    110
    011
    1111
    1111
    1101
    0111
    

    Explanation

    Test case

    : The given string is . Two rearrangements of such that the length of longest common substring is minimum are and . The longest common substring between strings and is , having length . It can be proved that for no other pair of rearrangements of string , the LCS has a length less than

    .

    Test case

    : The given string is . Here, all rearrangements of are the same. Two rearrangements of such that the length of longest common substring is minimum are and . The longest common substring between strings and is , having length

    .

    Test case

    : The given string is . Two rearrangements of such that the length of longest common substring is minimum are and . The longest common substring between strings and is having length . It can be proved that for no other pair of rearrangements of string , the LCS has a length less than .
    Previous
    Next Post »