#K77617. Book Organization
Book Organization
Book Organization
You are given a list of books, where each book is represented as a string containing a genre and a title separated by a comma. Your task is to organize the books by their genre and then sort the book titles in each genre alphabetically.
Instructions:
- The first line of input contains an integer \(n\), representing the number of books.
- The following \(n\) lines each contain a book description in the format
Genre,Title
. There can be extra spaces around the comma. - You need to group the books by genre. For each genre, sort the book titles in lexicographical order.
- Finally, sort the genres lexicographically. For each genre in order, first print the genre name, and then print its sorted book titles on new lines.
Example:
Input: 5 Fantasy,The Hobbit Science Fiction,Dune Fantasy,Harry Potter Science Fiction,Foundation Mystery,The Hound of the Baskervilles</p>Output: Fantasy Harry Potter The Hobbit Mystery The Hound of the Baskervilles Science Fiction Dune Foundation
inputFormat
The input is read from standard input (stdin) and has the following format:
- An integer \(n\) on the first line, representing the number of books.
- \(n\) subsequent lines, each containing a book entry in the format
Genre,Title
.
outputFormat
The output should be printed to standard output (stdout). For each distinct genre (sorted in lexicographical order), first print the genre name, then print each of its book titles (sorted lexicographically), each on its own line.
## sample5
Fantasy,The Hobbit
Science Fiction,Dune
Fantasy,Harry Potter
Science Fiction,Foundation
Mystery,The Hound of the Baskervilles
Fantasy
Harry Potter
The Hobbit
Mystery
The Hound of the Baskervilles
Science Fiction
Dune
Foundation
</p>