Hide

Problem J
A Night at Buckingham Palace

In a unfortunate turn of events, Sherlock Holmes’ assistant Dr. John Watson has died in an explosion while he was attending matters at the Buckingham Palace. Fortunately, the Royal family was not in town, so whoever planned the attack was clearly not very intelligent. Regardless, Mycroft, Sherlock’s brother and a “minor” member of the British Government, cares quite deeply about this attack and immediately asks Sherlock to start investigating. However, left assistant-less, he quickly recruits you to replace Dr. John Watson!

After looking through Buckingham Palace, Sherlock quickly determines that the attacker is Moriarty, and he has dropped direction cards (notecards with the exact directions to get to his location) all over the palace. These cards, if assembled correctly, will lead you straight to the Moriarty. Luckily, to assemble these cards, there are a few hints provided. On the back of the cards, Moriarty has written pairs in the format (n, m) where n is the value of the card, and m is a string of L’s and R’s. Based on this data, Sherlock has figured out that each card belongs to a binary tree, and the sequence of L’s and R’s determines where in the tree it is. If there is an empty string instead of L’s and R’s, that value is the root of the tree. Sherlock has also determined that to put together the direction cards, you must print each level of the binary tree (level order traversal), and the string of each level printed is the order of the direction cards that will lead you right to Moriarty.

As an example, in the above, the node containing $9$ would be represented by (9,), and the node containing $8$ would be represented by (8, RL). The level order traversal would be $9, 3, 1, 12, 7, 8, 14, 5, 10, 2, 6, 4, 11$.

However, Moriarity may have taken a few cards with him, rendering the tree incomplete. For example, if the card (3, L) wasn’t present in the tree, then the tree would be incomplete and you would output “incomplete”.

\includegraphics[width=0.5\textwidth ]{anightatbuckinghampalace.png}

Input

The input consists of at most $100$ testcases, all on the same line.

Each testcase consists of one or more direction cards. Each direction card consist of a pair (n,m), as described above. Each sequence is terminated by (). The value $n$ ($1 \leq n \leq 100$) in each card will always be present, while $m$ ($0 \leq |m| \leq 8$) may be absent. Every testcase will contain at least one card and no more than $256$ cards. Additionally, for each testcase, all values of $m$ are pairwise distinct.

Output

For each testcase, print the level order traversal of the sequence. If the sequence is incomplete, output the string “incomplete” instead.

Sample Input 1 Sample Output 1
(11,LL) (7,LLL) (8,R) (5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) () (3,L) (4,R) ()
5 4 8 11 13 4 7 2 1
incomplete

Please log in to submit a solution to this problem

Log in