#C14368. Flatten Courses
Flatten Courses
Flatten Courses
You are given a list of courses with enrolled students. Each course is provided with its name and a list of student names. Your task is to flatten the list into a set of (student, course) pairs and then output all pairs in lexicographical order (first by student name, then by course name).
More formally, if a course ( c ) has a list of students ( [s_1, s_2, \dots, s_k] ) (sorted in ascending order), then for each student ( s_i ) you generate a pair ( (s_i, c) ). Finally, sort the overall list of pairs by the student's name and, for identical names, by the course's name. Use the standard lexicographical order. This can be mathematically represented as:
[
\text{result} = \text{sort}\Big(\bigcup_{c \in C} {(s,c) : s \in \text{sorted}(students(c))}\Big)]
inputFormat
Input is given via standard input. The first line contains an integer ( n ), the number of courses. Then, ( n ) blocks follow. Each block describes a course and consists of three parts:
1. A line containing the course name (a string with no spaces).
2. A line containing an integer ( m ), the number of students enrolled in that course.
3. A line containing ( m ) space-separated student names. If ( m = 0 ), the corresponding line will be empty.
outputFormat
Output the flattened list of (student, course) pairs on standard output. For each pair, print a line with the student name and the course name separated by a single space. The pairs must be printed in lexicographical order (first by student name, then by course name).## sample
3
Math101
3
Alice Bob Charlie
History202
3
Alice David Edward
Science303
2
Charlie David
Alice History202
Alice Math101
Bob Math101
Charlie Math101
Charlie Science303
David History202
David Science303
Edward History202
</p>