#C7123. Organize Books by Genre
Organize Books by Genre
Organize Books by Genre
You are given a list of books, each with a title and a genre. Your task is to group the books by genre, remove duplicate titles within each genre, and sort the titles in alphabetical order. The genres themselves should also be sorted in alphabetical order.
Input Format: The first line contains an integer \(n\) denoting the number of books. Each of the following \(n\) lines contains a book record in the format: title,genre
. The title and genre are separated by a comma.
Output Format: For each genre, first print the genre followed by a colon. Then for each title under that genre, print the title on a new line and preceded by a tab character. The output should exactly match the prescribed format.
For example, if the input is:
7 Harry Potter,Fantasy Twilight,Fantasy The Hobbit,Fantasy Harry Potter,Fantasy Pride and Prejudice,Romance The Notebook,Romance Pride and Prejudice,Romance
The output should be:
Fantasy: Harry Potter The Hobbit Twilight Romance: Pride and Prejudice The Notebook
Note: In your solution, ensure you read from stdin
and write to stdout
.
inputFormat
The first line of input contains an integer n ((1 \le n \le 10^4)), which represents the number of books. Each of the next n lines contains a string and a genre separated by a comma. The title and genre do not contain extra commas.
outputFormat
For each genre (sorted in alphabetical order), print the genre followed by a colon on a single line. Then print each unique book title (sorted in alphabetical order) for that genre on its own line, preceded by a tab character.## sample
7
Harry Potter,Fantasy
Twilight,Fantasy
The Hobbit,Fantasy
Harry Potter,Fantasy
Pride and Prejudice,Romance
The Notebook,Romance
Pride and Prejudice,Romance
Fantasy:
Harry Potter
The Hobbit
Twilight
Romance:
Pride and Prejudice
The Notebook
</p>