hdu 1004 用set来排序map

首先看题目很容易就想到了用map因为map可以去掉重复的,但是引发一个问题怎么对value进行排序,于是百度了一下发现挺复杂的,然后想了很久没想出来怎么弄问了一下队友,用set来对map进行排序,很奇妙的方法!!!!,就是弄一个结构题把map的值放进去然后用set存储排序;
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <set>

using namespace std;

struct node{
	string a;
	int b;
	bool operator <(const node & c)const
	{
		return c.b<b; 
	}
};


int main()
{
	int n;
	map<string,int> mp;
	set <node> ans;
	node a;
	while(1)
	{
		mp.clear();
		ans.clear();
		scanf("%d",&n);
		if(n==0)
			break;
		for(int i=0;i<n;i++)
		{
			string str;
			cin>>str;
			mp[str]++;
		} 
		map <string,int>::iterator it;
		
		for(it = mp.begin();it!=mp.end();it++)
		{
			node shuzu;
			shuzu.a=it->first;
			shuzu.b=it->second;
			ans.insert(shuzu);
		//	cout<<shuzu.b<<endl;
		}
		set <node> ::iterator it1;
		it1=ans.begin();
		/*for(it1=ans.begin();it1!=ans.end();it1++)
		{
			cout<<(*it1).a<<" "<<(*it1).b<<endl;
		}*/
		cout<<(*it1).a<<endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值