题目描述
n个色盲聚在一 起,讨论一块布的颜色。尽管都是色盲,却盲得各不相同。每个人都有自己的主张,争论不休。最终,他们决定采取民主投票的方式决定布的颜色,不管布同不同 意。某种颜色用字符串表示(字符串为颜色单词或词组,也就是可能有被空格隔开的两个单词组成的颜色词组),只要字符串不同,程序即判断颜色不同。现在给出 这n个人所选择的颜色,输出最有可能的颜色(也就是获得投票最多的颜色),如果有多个颜色获得了最多的投票,则将它们按字典序分行全部输出。
数据规模和约定
n< =1000
颜色单词最多20个字符,只包含小写字母或者空格
输入:
第一行一个正整数n,表示色盲的人数
接下来n行,每行一句话
输出:
若干行,获得投票最多的颜色,按字典序输出
样例输入1:
5
red
blue
black
black
blue
输入样例2:
black
blue
代码如下:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
map<string,int> mp;
set<string> ss;
int main()
{
int n;
string s;
cin>>n;
for(int i=0; i<=n; i++)
{
getline(cin,s);
mp[s]++;
}
int maxx = -999999;
map<string,int>::iterator it;
for(it = mp.begin(); it!=mp.end(); it++)
{
maxx = max(maxx,it->second);
}
for(it = mp.begin(); it!=mp.end(); it++)
{
if(maxx == it->second)
{
ss.insert(it->first);
}
}
set<string>::iterator its;
for(its = ss.begin(); its!=ss.end(); its++)
{
cout<<*its<<endl;
}
return 0;
}