Description
![Download as PDF Download as PDF](https://i-blog.csdnimg.cn/blog_migrate/2ea975a8d421ce3226c46b0946fd5932.png)
Problem B
List of Conquests
Input: standard input
Output: standard output
Time Limit: 2 seconds
In Act I, Leporello is telling Donna Elvira about his master's long list of conquests:
``This is the list of the beauties my master has loved, a list I've made out myself: take a look, read it with me. In Italy six hundred and forty, in Germany two hundred and thirty-one, a hundred in France, ninety-one in Turkey; but in Spain already a thousand and three! Among them are country girls, waiting-maids, city beauties; there are countesses, baronesses, marchionesses, princesses: women of every rank, of every size, of every age.'' (Madamina, il catalogo è questo)
As Leporello records all the ``beauties'' Don Giovanni ``loved'' in chronological order, it is very troublesome for him to present his master's conquest to others because he needs to count the number of ``beauties'' by their nationality each time. You are to help Leporello to count.
Input
The input consists of at most 2000 lines, but the first. The first line contains a number n, indicating that there will be n more lines. Each following line, with at most 75 characters, contains a country (the first word) and the name of a woman (the rest of the words in the line) Giovanni loved. You may assume that the name of all countries consist of only one word.
Output
The output consists of lines in alphabetical order. Each line starts with the name of a country, followed by the total number of women Giovanni loved in that country, separated by a space.
Sample Input
3
Spain Donna Elvira
England Jane Doe
Spain Donna Anna
Sample Output
England 1
Spain 2
Problem-setter: Thomas Tang, Queens University, Canada
“Failure to produce a reasonably good and error free problem set illustrates two things a) Lack of creativity b) Lack of commitment”
题目大意:
很水的一道题,但是让我收获很多,就是说,输入一个国家和人名,然后最终统计一个国家出现了多少次.
解题思路:
这道题让我觉得收获大的地方就是:1,gets(a),2,cin>>b[i]
不得不再次强调,gets(a)是读到回车结束,而cin>>b[i]是读入到空格结束,关于更多有关输入和输出的陷阱和方法,将会在后续的blog中逐步更新。
还有个收获就是sort函数也可以进行字典序排列。。
代码:
# include<cstdio>
# include<iostream>
# include<algorithm>
using namespace std;
char a[79];
string b[2000+4];
int main(void)
{
int n;
while ( cin>>n )
{
for ( int i = 0;i < n;i++ )
{
gets(a);
cin>>b[i];
}
sort(b,b+n);
for ( int i = 0;i < n; )
{
cout<<b[i]<<" ";
int num = 1;
int j;
for ( j = i+1;j < n;j++ )
{
if ( b[i]!=b[j] )
{
break;
}
num++;
}
cout<<num<<endl;
i = j;
}
}
return 0;
}