Lab Question 2.1 Election question <br/> Description: <br/> A mayor election is underway in a city. The name of the person to be elected by the voter is on a large ballot paper, select the final statistical vote <br/> and determine the final candidate for mayor (initial election ). <Br/> lab task: <br/> for a group of votes, obtain the candidate for the mayor and the number of votes obtained. <Br/> data input: <br/> the input data is provided by the input.txt file. Each line contains a set of strings (excluding spaces), indicating the name of the selected candidate <br/>. The length of a string cannot exceed 30 characters. (A maximum of 10000 strings are supported ). <Br/> result output: <br/> the mayor's choice is output, and the income value is output.txt. (If multiple people select the same number of votes, <br/> the result is output in lexicographically ascending order .)
Submitted three times to pass all test points...
Depressed. Such a simple question...
But it was originally required to be written in a linear table...
But it seems that it is very troublesome to use that one to write...
So map + vector is used...
Note that,
If multiple people select the same number of votes,
The output is in lexicographically ascending order.
So I put the person with the highest number of votes and the same number of votes in a vector.
Sort the names in Lexicographic Order and output the results.
The original string has been overloaded. You can directly compare strings of the string type.
The comparison result is sorted by lexicographically.
However, the string type of input and output must use CIN cout.
The Code is as follows.
# Include <iostream> <br/> # include <map> <br/> # include <vector> <br/> # include <string> <br/> # include <Algorithm> <br/> using namespace STD; </P> <p> bool my_sort (pair <string, int> P1, pair <string, int> P2) <br/>{< br/> return p1.second> p2.second; <br/>}</P> <p> bool my_sort2 (pair <string, int> P1, pair <string, int> P2) <br/>{< br/> return p1.first <p2.first; <br/>}</P> <p> int main () <br/>{< br/> Map <string, int> T _ Map; <br/> char name [101]; <br/> while (scanf ("% s", name )! = EOF) <br/> t_map [name] ++; <br/> vector <pair <string, int> V1; <br/> vector <pair <string, int> V2; <br/> Map <string, int>: iterator it; <br/> for (IT = t_map.begin (); it! = T_map.end (); It ++) <br/> v1.push _ back (* It); <br/> vector <pair <string, int >:: iterator it_v; <br/> sort (v1.begin (), v1.end (), my_sort); <br/> int max = V1 [0]. second; <br/> for (it_v = v1.begin (); it_v! = V1.end (); it_v ++) <br/>{< br/> If (max = it_v-> second) <br/> v2.push _ back (* it_v ); <br/> else <br/> break; <br/>}< br/> sort (v2.begin (), v2.end (), my_sort2 ); <br/> for (it_v = v2.begin (); it_v! = V2.end (); it_v ++) <br/>{< br/> cout <it_v-> first <Endl; <br/>}< br/> cout <V2 [0]. second <Endl; <br/> return 0; <br/>}