#C7564. Organize Books by Genre

    ID: 51449 Type: Default 1000ms 256MiB

Organize Books by Genre

Organize Books by Genre

You are given a list of books. Each book is described by its title and its genre. Your task is to organize these books into a dictionary (or map) where each key is a genre and the corresponding value is a list of book titles that belong to that genre. The list of book titles for each genre must be sorted in alphabetical order.

The input is given via standard input (stdin) and the output should be printed to standard output (stdout). If there are no books, output an empty dictionary {}.

Note: When sorting, use lexicographical (ASCII) order.

Mathematical Explanation: Let \(B\) be the list of books where each book \(b_i = (t_i, g_i)\) with title \(t_i\) and genre \(g_i\). For each unique genre \(g\) present in \(B\), produce a list \(L_g = [t_{i_1}, t_{i_2}, \dots, t_{i_k}]\) where \(g_{i_j} = g\) and \(t_{i_1} \leq t_{i_2} \leq \dots \leq t_{i_k}\) under lexicographical order. Output the dictionary that maps each genre \(g\) to the sorted list \(L_g\).

inputFormat

The first line of input contains an integer \(N\), the number of books. Each of the following \(N\) lines contains a book title and its genre separated by a comma and a space.

For example:

6
The Hobbit, Fantasy
1984, Dystopian
Brave New World, Dystopian
Dune, Science Fiction
Neuromancer, Science Fiction
The Fellowship of the Ring, Fantasy

outputFormat

Print the resulting dictionary in JSON format. Each key is a genre and its corresponding value is the list of sorted book titles.

For example:

{"Fantasy": ["The Fellowship of the Ring", "The Hobbit"], "Dystopian": ["1984", "Brave New World"], "Science Fiction": ["Dune", "Neuromancer"]}
## sample
2
1984, Dystopian
Brave New World, Dystopian
{"Dystopian": ["1984", "Brave New World"]}