【HDU1263】【STL-MAP套MAP】(省份,水果,数量)MAP嵌套大开发

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
int casenum,casei;
char FRU[100],PRO[100];
int n,num;
map<string,map<string,int> >mop;
map<string,map<string,int> >::iterator pro;
map<string,int>::iterator fru;
int main()
{
	scanf("%d",&casenum);
	for(casei=1;casei<=casenum;casei++)
	{
		mop.clear();
		scanf("%d",&n);
		while(n--)
		{
			scanf("%s%s%d",FRU,PRO,&num);
			mop[PRO][FRU]+=num;
		}
		for(pro=mop.begin();pro!=mop.end();pro++)
		{
			cout<<pro->first<<endl;
			for(fru=pro->second.begin();fru!=pro->second.end();fru++)
			{
				cout<<"   |----"<<fru->first<<'('<<fru->second<<')'<<endl;
			}
		}
		if(casei<casenum)puts("");
	}
	return 0;
}
/*
【题意】
有T(10)组数据
对于每组数据,有n次交易。
每个交易提供(水果名称,交易省份,交易数量)。
让你最后按照:
1,省份
2,水果名称
3,水果交易数量
字典序规则排序

【类型】
模拟
STL-MAP

【分析】
STL-MAP真是十分强大。
它可以通过嵌套关系延展其功能。
这里就直接对每个省份,将其名称通过map哈希做映射。
映射的对象依然是一个map,这个内嵌的map实现了对于这个省份而言的(水果名称,水果数量的映射)。
于是当出现一个交易(水果名称,交易省份,交易数量)时,
我们直接操作使得mop[pro][fru]+=num;
然后通过迭代器,遍历所有的pro,再遍历每个pro中所有的fru,最终快速解决这道题。

对于map,set的灵活使用,你学会了吗^_^?
*/

你可能感兴趣的:(map,ACM,STL,ICPC,HDU)