算法提高 色盲的民主
时间限制:1.0s 内存限制:256.0MB
色盲的民主
色盲的民主
问题描述
n个色盲聚在一起,讨论一块布的颜色。尽管都是色盲,却盲得各不相同。每个人都有自己的主张,争论不休。最终,他们决定采取民主投票的方式决定布的颜色,不管布同不同意。某种颜色用字符串表示(字符串为颜色单词或词组,也就是可能有被空格隔开的两个单词组成的颜色词组),只要字符串不同,程序即判断颜色不同。现在给出这n个人所选择的颜色,输出最有可能的颜色(也就是获得投票最多的颜色),如果有多个颜色获得了最多的投票,则将它们按字典序分行全部输出。
输入格式
第一行一个正整数n,表示色盲的人数
接下来n行,每行一句话
接下来n行,每行一句话
输出格式
若干行,获得投票最多的颜色,按字典序输出
样例输入
5
red
blue
black
black
blue
red
blue
black
black
blue
样例输出
black
blue
blue
数据规模和约定
n<=1000
颜色单词最多20个字符,只包含小写字母或者空格
颜色单词最多20个字符,只包含小写字母或者空格
注
对于char s[20],由于cin >> s是读取到空格处便会结束,也就是对于light red,用cin只能输入light。如果要整个输入一行,则使用cin.getline(s, 20),其中20为这一行的最大长度,也就是你的s的容量,如果容量为30,则cin.getline(s, 30)。
另外,你在cin>>n以后cin.getline(s,30)应该会得到一个空字符串,这是因为整数n后面的换行符还未被输入。
另外,你在cin>>n以后cin.getline(s,30)应该会得到一个空字符串,这是因为整数n后面的换行符还未被输入。
#include <iostream>
#include <queue>
#include <algorithm>
#include <map>
#include <vector>
#include <stdio.h>
#include <string>
using namespace std;
struct cmp
{
bool operator()(string const& a, string const& b)const
{
return a < b;
}
};
map<string, int, cmp>mymap;
int main()
{
int n;
scanf("%d", &n);
getchar();
int i;
string s;
int max = 1;
for(i = 0; i < n; i++)
{
getline(cin, s);
map<string,int>::iterator it;
it = mymap.find(s);
if (it == mymap.end())
{
mymap.insert(map<string, int>::value_type(s, 1));
}
else
{
it->second++;
if ((int)it->second > max)
max = (int) it->second;
}
}
map<string,int>::iterator it;
for (it = mymap.begin(); it != mymap.end(); it++)
{
if(it->second == max)
cout<<it->first<<endl;
}
return 0;
}