#K4041. Merge Student Records
Merge Student Records
Merge Student Records
You are given two lists of student records. Each record contains a name, a unique student_id and a list of courses the student takes. The records in each list might contain duplicate students (i.e. two records with the same student_id), and in such a case, the courses lists should be merged (i.e. take the union of the courses). When merging duplicate records, the name from the first occurrence (from the first list if available) should be preserved. Finally, print the merged records sorted in increasing order of student_id, and within each record output the courses sorted in lexicographical order.
The problem can be described by the following formula:
[ MergedRecord = \bigcup_{record \in List1 \cup List2} { (student_id, name, \mathrm{sort}(courses)) } ]
where ( \mathrm{sort}(courses) ) represents the lexicographical sorting of the courses.
inputFormat
The input is given via standard input (STDIN) in the following format:
n m [First List Records] [Second List Records]
Here, n
and m
are two integers separated by space, representing the number of records in the first and second list respectively. Then for each record in a list, the format is as follows:
name student_id k course_1 course_2 ... course_k
where k
is the number of courses for that record.
outputFormat
Output the merged student records to standard output (STDOUT). For each record, output one line in the format:
student_id name course_1 course_2 ... course_p
The records must be printed in increasing order of student_id
, and the list of courses for each record must be sorted in lexicographical order. There should be a single space between elements.
2 2
Alice
S001
2
Math
Science
Bob
S002
1
History
Alice B.
S001
1
English
Charlie
S003
1
Math
S001 Alice English Math Science
S002 Bob History
S003 Charlie Math
</p>