#C5322. Library Catalog Summary
Library Catalog Summary
Library Catalog Summary
You are given several test cases representing a library's catalog system. For each test case, you will be provided with a number of books. Each book description consists of a genre followed by a list of keywords describing the book.
Your task is to compile a summary for each test case. For every test case, you should group the books by their genre. For each genre, collect all distinct keywords from all books of that genre. Then, sort the genres in lexicographical order. For each genre, print the genre name on one line, followed by a second line that begins with a space and then the sorted list of unique keywords for that genre, separated by a single space.
Note: Sorting should be done in standard lexicographical order. For example, given keywords alien
, spaceship
, and future
, the sorted order is: alien future spaceship
.
The mathematical representation for sorting can be denoted as follows: if \( S \) is a set of keywords, then the output list is \( sorted(S) \) where the sorting order is in increasing lexicographical order.
inputFormat
The input is given via standard input (stdin) and follows the format below:
T N1 Book_1 Book_2 ... Book_N1 N2 Book_1 Book_2 ... Book_N2 ...
Here, T is the number of test cases. For each test case, a line containing an integer N indicates the number of books followed by N lines. Each book is represented by a string where the first token is its genre and the following tokens are keywords.
outputFormat
For each test case, output to standard output (stdout) the summary as follows:
- For each genre (in lexicographical order), print the genre on one line.
- On the next line, print a space followed by the sorted list of unique keywords for that genre (also in lexicographical order), separated by a single space.
There should be no extra spaces or blank lines in the output.
## sample1
3
ScienceFiction alien spaceship future
Fantasy magic dragon adventure
ScienceFiction future space travel
Fantasy
adventure dragon magic
ScienceFiction
alien future space spaceship travel
</p>