Given each kid's ranked list of apps from oldest to youngest, assign each kid the highest-ranked app not already taken by an older kid.
Easy3Hash mapGreedySimulationImplementationInterviewNo attempts yetTime limit2sMemory limit512 MBJoyce Stick is the mother of several children, and she keeps strict rules about how much technology they use. One rule is that each kid keeps at most one app on their phone. The kids are not thrilled about it, but they know that complaining costs them their half hour of television a day. To get as many different apps as possible among them, they agreed that every kid picks a different app. Each kid likes different apps, and each one wants the app they like best, or close to best, on their phone.
Here is the procedure they settled on. Each kid writes down the apps they like in one list, from the app they like most to the app they like least, leaving off apps they have no interest in at all. The oldest kid takes the first app on their own list. The second oldest takes the first app on their list, unless the oldest already took it, in which case they take the second app on their list. Every remaining kid does the same in order of age: each takes the highest app on their list that no older kid has taken.
Given every kid's preference list, determine the app that ends up on each phone.
The first line contains a positive integer n (n≤100), the number of kids.
Each of the next n lines holds one kid's list. The line starts with a positive integer m (m≤50), the number of apps on that list, followed by m strings, the app names from most preferred to least preferred. The first of these lines is the oldest kid's list, the second line is the second oldest kid's list, and so on. An app name is at most 10 characters long. App names are case sensitive, so Maps and maps are different apps.
Print the app chosen for each kid, from the oldest kid to the youngest, on one line, separating the names with a single blank.
No input has a kid whose apps have all been taken by older kids.