Map个人总结(例题HDU-1004和HDU-1263)

      map映照容器的元素是由一个键值和一个映照数据组成的,键值和映照数据之间有一一映照的关系。map映照容器的数据结构也是采用红黑树来实现的,插入元素的键值不允许重复,比较函数只对元素的键值进行比较,元素的各项数据可以通过键值检索出来。

	定义使用

  	#include<map>//头文件

  	map<string,int>ma;//定义

  	map<string,int>::iteratorit//正向遍历迭代器

  	map<string,int>::reverse_iteratorit//反向遍历迭代器

  	循环结构

  	For(it=ma.begin();it!=ma.end();it++)

  	输出

  	Cout<<it->ma.first<<it->ma.second<<endl;

例题:HDU-1004

http://acm.hdu.edu.cn/showproblem.php?pid=1004

Let the Balloon Rise

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 139711    Accepted Submission(s): 55242


Problem Description
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
 

Author
WU, Jiazhi
 

Source
 

Recommend
JGShining

AC:

	#include <iostream> 
	#include <cstdio>
	#include <string>
	#include <algorithm>
         #include <map>
	using namespace std; 
	int main() 
	{
	 int m;
	 map<string,int>ma; 
	while(cin >> m && m != 0) 
	{
	 string a; 
	for(int i = 0;i < m;i++) 
	{ 
	cin >> a; 
	ma[a]++; 
	} 
	map<string,int>::iterator it; 
	int max = -230;string mx; 
	for(it = ma.begin();
	it != ma.end();it++) 
	{ 
	if((*it).second > max) 
	{ max = (*it).second; 
	mx = (*it).first; 
	} 
	} 
	cout << mx <<endl; 
	ma.clear(); 
	} 
	return 0; 
	} 

注意:


	map函数内可以使用结构体,也可以进行map的叠加

  	1.Structacc{

  	map<string,int>ma1;

  	};

  	Ma<string,acc>ma2;

  	2.Map<string,map<string,int>>ma;

例题:HDU-1263

http://acm.hdu.edu.cn/showproblem.php?pid=1263

水果

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9395    Accepted Submission(s): 3756


Problem Description
夏天来了~~好开心啊,呵呵,好多好多水果~~
Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表,这样Joe就可以很容易掌握所有水果的销售情况了.
 

Input
第一行正整数N(0<N<=10)表示有N组测试数据.
每组测试数据的第一行是一个整数M(0<M<=100),表示工有M次成功的交易.其后有M行数据,每行表示一次交易,由水果名称(小写字母组成,长度不超过80),水果产地(小写字母组成,长度不超过80)和交易的水果数目(正整数,不超过100)组成.
 

Output
对于每一组测试数据,请你输出一份排版格式正确(请分析样本输出)的水果销售情况明细表.这份明细表包括所有水果的产地,名称和销售数目的信息.水果先按产地分类,产地按字母顺序排列;同一产地的水果按照名称排序,名称按字母顺序排序.
两组测试数据之间有一个空行.最后一组测试数据之后没有空行.
 

Sample Input
 
 
1 5 apple shandong 3 pineapple guangdong 1 sugarcane guangdong 1 pineapple guangdong 3 pineapple guangdong 1
 

Sample Output
 
 
guangdong |----pineapple(5) |----sugarcane(1) shandong |----apple(3)
 

Source
 

Recommend
JGShining


AC1:

#include <iostream>
#include <cstdio>
#include <string>
#include <map>

using namespace std;

struct acc{
	map<string,int>My;
};
int main()
{
	map<string,acc>Di;
	map<string,acc>::iterator it;//地名的迭代器
	map<string,int>::iterator jj;//水果的种类及数量的迭代器
	string fruit,place;
	int count;
	int n,t;
	cin >> t;
	while(t--)
	{
		Di.clear();
		cin >> n;
		while(n--)
		{
			cin >> fruit >> place >> count;
			Di[place].My[fruit] += count;
		}
		for(it = Di.begin();it != Di.end();it++)
		{
			cout << it->first <<endl;
			for(jj = it->second.My.begin();jj!=it->second.My.end();jj++)//it->second是水果的种类
			{
				cout << "   |----" << jj->first<<"("<<jj->second<<")"<<endl;
			}
		}
		if(t != 0)
		cout << endl;
	}
	return 0;
}

另外还要注意,map可以嵌套使用

map<string,map<string,int> > ma;

AC2:

#include <iostream>
#include <cstdio>
#include <string>
#include <map>

using namespace std;

int main()
{
	string fruit,place;
	int t,n,count;
	cin >> t;
	while(t--)
	{
	  map<string,map<string,int> > ma;
	  map<string,map<string,int> >::iterator it;
	  map<string,int>::iterator iti;
	  cin >> n;
	  while(n--)
	  {
	  	cin >> fruit >> place >> count;
	  	ma[place][fruit] += count;//类似二元数组
      }		
      for(it = ma.begin();it!=ma.end();it++)
      {
      	cout << it->first << endl;
      	for(iti = it->second.begin();iti!=it->second.end();iti++)//道理等同使用结构体的情况
      	{
      		cout << "   |----" << iti->first << "(" << iti->second << ")" <<endl;
	  }
	}
		  if(t!=0)
	  cout << endl;
}
     return 0;
 } 



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值