#C11901. Organize Books by Genre
Organize Books by Genre
Organize Books by Genre
You are given a collection of books each identified by a genre and a numerical identifier. Your task is to group the books by their genre (in the order they first appear) and, for each genre, output the book identifiers in sorted order. Each test case starts with an integer T representing the number of test cases. For each test case, the first number is N (the number of books), followed by N lines each containing a genre (a string) and an identifier (an integer) separated by a space.
For example, if a test case contains:
3 fiction 2 nonfiction 1 fiction 1
Then the output for that test case should be:
fiction: 1 2 nonfiction: 1
Note: The sorting order for the identifiers is in ascending order. Use stdin
for input and stdout
for output.
inputFormat
The input begins with an integer T (the number of test cases). Each test case is structured as follows:
- The first line contains an integer N, the number of books in that test case.
- The next N lines each contain a string and an integer separated by a space, representing the genre and the book's identifier respectively.
All input should be read from stdin
.
outputFormat
For each test case, output the list of genres in the order they first appear. For each genre, print the genre followed by a colon on one line, and on the next line print the sorted list of identifiers corresponding to that genre separated by a single space. Separate outputs for different test cases with an empty line.
All output should be written to stdout
.
2
3
fiction 2
nonfiction 1
fiction 1
4
romance 1
science 2
romance 3
science 1
fiction:
1 2
nonfiction:
1
romance:
1 3
science:
1 2
</p>