The story dates back to Ignus'1928 when photographs were black and white and hard disk space was scarce. It was the pronite, the mood was set, people were enjoying and photographs were being clicked.
Flashes flashes, everywhere
clicking pics to the core
flashes flashes, everywhere
but NO space to store!!
During the pronite, it turned out that IITJ Shutterbugs ran out of hard disk space to store pics. An emergency core team meeting was called and it was decided that Shutterbugs would reduce the hard disk space utilization by merging two photographs into one.
Following algorithm was devised to merge them:
Since, each pixel in the photograph is either black or white; it was decided to represent photographs by "Image Trees" - Trees with each node being either a leaf or having exactly 4 children.
Note:
Each leaf node would be either black or white only.
A non-leaf node is always "mixed" in color and has exactly 4 children, where:
EITHER atleast 1 child is a non-leaf
OR if all children are leaves, then not all of them are identical in color.
See images below to understand image to tree transformation:
The Image tree is given as a sequence of 'b', 'w' and 'm' characters denoting 'black', 'white' and 'mixed' color of nodes. The sequence of nodes is the preorder traversal (see explanation) of the tree.
Explanation:
Preorder traversal of the tree is:
[Root][preorder traversal at child 1][preorder traversal at child 2][preorder traversal at child 3][preorder traversal at child 4]
.
So, preorder traversal of trees in image 1 is: b
and w
and mbwwb
respectively and
Preorder traversal of the tree in figure 2 is: mbwmbbwwmbwbb
Merging rules:
Black & White = White node
Black & Black = Black node
White & White = White node
Input:
The first line contains an integer T, the number of test cases. Then T test cases follow. Each test case contains 2 strings corresponding to the preorder traversals of the images to be merged.
There is a blank line after each test case in the input.
The two strings maybe of unequal lengths.
Output:
For each test case, merge the images and output the preorder traversal of the resulting image tree in a single line.
Constraints:
1 <= T <= 50
1 <= Size of string <= 10000
For the third test case, resulting tree would be:
mwwww
. This should be condensed into a singlew
node.
Same should be true for all sub-trees. Note that the condensing could move even further up the levels.
Read the point 2 in the note above.