Question 10420-List of conquests of A uvais online judge:
The question is very simple. A list of people attending the banquet, including their nationality and name (the name is useless at all) is provided ). Count the number of participants in each country and sort them by country name dictionary.
Input Format:
The first line is an integer N, indicating the number of participants. Next is n rows. The first word in each line is the country name, followed by the name.
Output Format:
Each row includes the country name and attendance, and the country name is output in alphabetical order.
Example input:
3
Spain Donna Elvira
England jane doe
Spain Donna Anna
Sample output:
England 1
Spain 2
Code: (use STL directly .. Too lazy)
1 # include <iostream> 2 # include <vector> 3 # include <cstdio> 4 # include <cstring> 5 # include <string> 6 # include <algorithm> 7 using namespace std; 8 9 struct country {10 string name; 11 int COUNT = 0; 12 bool operator = (const country & B) {13 return this-> name = B. name; 14} 15}; 16 17 int CMP (const country & A, const country & B) {return. name <B. name;} 18 19 vector <country> countries; 20 int main () 21 {22 Int N; 23 char STR [75]; 24 scanf ("% d", & N); 25 for (INT I = 0; I <n; I ++) 26 {27 scanf ("% s", STR); // obtain the country name 28 char C; 29 c = getchar (); 30 While (C! = '\ N' & C! = EOF) {c = getchar () ;}// ignore name 31 country nowct; 32 nowct. name = STR; 33 vector <country>: iterator iter = find (countries. begin (), countries. end (), nowct); 34 if (iter = countries. end () 35 {36 nowct. count = 1; 37 countries. push_back (nowct); 38} 39 else40 {41 ITER-> count ++; 42} 43} 44 sort (countries. begin (), countries. end (), CMP); // sort 45 for (vector <country>: iterator it = countries. begin (); it! = Countries. end (); It ++) 46 {47 cout <it-> name <"" <it-> count <Endl; 48} 49 return 0; 50}