And the Winner Is
InterviewTime limit1sMemory limit128 MB
Each ballot has one character per candidate; discard any ballot that marks more than one candidate in the same race, then report the top vote-getter (ties included) in every race, in input order.
- Level
Medium5 of 10
- Topics
- Implementation, Array, Hash map, Simulation
- Solved
- No attempts yet
Problem
After an election we must count the votes each candidate received and determine the winners. We have to be careful, though: voters sometimes fill out their ballots incorrectly, and any such ballot must be discarded. Write a program that correctly determines the winners of the elections.
Why several elections at once? A single ballot usually contains many separate votes — for senator, for congressperson, for district positions, and so on. Here we assume that if the vote for any one of these categories is filled out incorrectly, then the entire ballot is discarded.
Input
The first line contains the number of data sets () in the file. It is followed by data sets of the form below.
The first line of a data set contains three integers , , and : the number of candidates (), the number of races (), and the number of voters (). The next lines describe the candidates. Each line begins with an integer from to giving the race that candidate is running in, followed by a single space and then the candidate's name (a name may contain spaces). Every race has at least one candidate.
The next lines describe each voter's ballot. Every line is a string of exactly characters, each either x or #. An x in position means the voter voted for candidate , and a # in position means they did not. If a voter voted for more than one candidate in the same race, the entire ballot is discarded. On the other hand, casting no vote at all in a particular race is fine.
Output
For each data set, first print Data Set x: on a line by itself, where x is its number. Then print the names of all candidates who finished first (or tied for first) in their race, in the order in which they appeared in the input.