1.lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
Sample Input
you are my friend
Sample Output
4
#include<stdio.h>
#include<iostream>
#include<sstream>
#include<set>
using namespace std;
int main()
{
string s1;
while(getline(cin,s1)) //函数里面接受一个字符串(含空格)
{
if(s1[0]=='#')
break;
stringstream stream(s1);
set<string>ss; //定义 保证里面的元素不重复
string s2;
while(stream>>s2)
{
ss.insert(s2); //插入元素
}
cout<<ss.size()<<endl; //不同元素个数
}
return 0;
}
2。Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges’ favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.
This year, they decide to leave this lovely job to you.
Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) – the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.
A test case with N = 0 terminates the input and this test case is not to be processed.
Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
Sample Input
5
green
red
blue
red
red
3
pink
orange
pink
0
Sample Output
red
pink
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<map>
#define inf 0x3f3f3f3f
using namespace std;
int main()
{
int n;
while(cin>>n)
{
if(n==0)
break;
map<string,int>mp;
string s1;
for(int i=0;i<n;i++)
{
s1.erase(0,s1.length()); //清空
cin>>s1;
mp[s1]++; //字符串出现的次数
}
map<string,int>::iterator it; //迭代器
int maxx=-inf;
string s2;
for(it=mp.begin();it!=mp.end();it++)
{
if((*it).second>=maxx)
{
s2=(*it).first; //first指string second指int
maxx=(*it).second;
}
}
cout<<s2<<endl;
}
return 0;
}
map<string,int>mp;可以理解为容器中字符串的个数