An expedition outfitter keeps a certification roster: every crew member who has earned a wilderness certification (first aid, rope rescue, cold-water rescue, and so on) has one roster entry per certification they hold. A newly announced expedition specifies a fixed list of required certifications -- anyone assigned to it must hold every one of them, though holding additional, non-required certifications is perfectly fine. Given the roster and the expedition's required list, find every crew member who is fully qualified for the expedition.
Line 1: an integer k -- the number of required certifications. Line 2: k space-separated strings -- the required certification names (pairwise distinct). Line 3: an integer n -- the number of roster entries. Each of the next n lines contains an integer crew_id and a string certification, meaning that crew member holds that certification. A crew member may hold several certifications (one roster entry each), and, occasionally, the exact same (crew_id, certification) pair may appear more than once without changing the outcome.
Print, in strictly increasing order and space-separated on one line, the ids of every crew member who holds all k required certifications. If nobody qualifies, print an empty line.
Example 1
Input
2 firstaid rope 6 101 firstaid 101 rope 101 climbing 102 firstaid 102 rope 103 rope
Expected
101 102
Explanation
Crew 101 holds firstaid and rope (plus an extra, non-required 'climbing' certification, which does not disqualify) -- qualified. Crew 102 holds both firstaid and rope -- qualified. Crew 103 holds only rope, missing firstaid -- not qualified. Sorted ascending, the qualifying ids are 101 and 102.
Example 2
Input
3 firstaid rope wilderness 5 5 firstaid 5 rope 5 wilderness 5 firstaid 9 firstaid
Expected
5
Explanation
Crew 5 holds firstaid, rope, and wilderness (the duplicated 'firstaid' row changes nothing) -- qualified for all three required certifications. Crew 9 holds only firstaid -- not qualified. The output is just 5.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →