[Algorithm exercise] What is the list of conquests?

Source: Internet
Author: User
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}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.