Punched Cards
Time limit1sMemory limit1024 MB
Order n punched cards so that, reading each column downward, the first letter encountered spells the target string s, or report that no order works.
- Level
Medium7 of 10
- Topics
- Graph, Topological sort, Greedy, Sorting
- Solved
- No attempts yet
Problem
punched cards were found in the warehouse of the company hosting the programming olympiad. A punched card is a strip of cells, each of which either contains a lowercase English letter or is a hole.
The olympiad jury wants to order all the punched cards so that, when they are placed one under another from top to bottom in this order, the olympiad slogan appears: the given string of length .
In other words, fix the order in which the cards are laid and consider an arbitrary position (). Then the -th character of the string must match the character at position of the topmost punched card that has a letter at position . If for some there is no punched card with a letter at position , then the required string is considered impossible to obtain.
Help the jury figure out the order in which the punched cards must be placed.

Fig. 1: The order of the cards from the second sample. The letters visible from above are highlighted
Input
The first line contains two integers and (), the number of punched cards and the number of cells, respectively.
The second line contains the string consisting of lowercase English letters.
The -th of the following lines contains the description of the -th punched card.
The description begins with an integer (), the number of positions with letters on this punched card. The sum of all is guaranteed not to exceed .
Then follows the description of the letters on this punched card: pairs , (, is a lowercase English letter) for all integers ; each pair indicates that the character is present at position . The remaining positions contain holes. The numbers of the positions with letters on a single punched card are guaranteed to be given in increasing order, that is, for any we have .
Output
If there is a way to order the punched cards as required, output integers (), where is the number of the topmost punched card, is the number of the second card from the top, and so on up to the card , which lies at the bottom. If there are several possible answers, you may output any of them.
If there is no way to order the punched cards as required, output the single number .