JAG-channel II
Time limit3sMemory limit256 MB
Find the lexicographically smallest member posting order consistent with the recorded top-to-bottom thread picks under move-to-front reordering.
- Level
Medium7 of 10
- Topics
- Backtracking, Simulation, Bit manipulation
- Solved
- No attempts yet
Problem
JAG is a group of members who work on spreading competitive programming. The members talk every day on a board called JAG-channel. The board holds several threads, and the list is always sorted by the time of the latest post, newest first. As soon as someone posts in a thread, that thread moves to the top of the list.
One night each of the members created one thread. The members are labelled by the first uppercase letters, and each thread is written as the letter of the member who created it. The next morning each member posted once in each of different threads among the ones created that night. The members care about speed, so each of them scanned the list from top to bottom and posted in a thread the moment it caught their interest. The members posted in separate periods of time, so while one member was making their posts, no other member posted anything.
You know, for every member, the order of the threads they posted in, but you do not know the order the threads were in at the start. The list is reordered on every post, so some orders of the members are impossible: the posts of an earlier member can leave a later member unable to meet their threads in the recorded top-to-bottom order. Find the lexicographically smallest possible order of the members.
Input
The first line contains two integers and separated by one space (, ).
Each of the next lines contains a string of exactly distinct uppercase letters. The -th character of the -th line is the thread in which the -th member made their -th post. A thread is written as the letter of the member who created it, so 'B' is the thread created by the second member, B.
At least one possible order of the members is guaranteed to exist.
Output
Print the lexicographically smallest possible order of the members as one line of uppercase letters. The -th character is the member who posted during the -th period.