题目详情:
使用二重Map能够快速解决。
代码:
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
using namespace std;
int t,n,m;
string s1,s2;
int main()
{
cin>>t;
while(t--){
cin>>n;
map<string,map<string,int> >fr; //<产地,<名称,数量>>
while(n--){
cin>>s1>>s2>>m;
fr[s2][s1]+=m;
}
map<string,map<string,int> >::iterator it; //迭代器,指向总体
map<string,int> :: iterator it2; //迭代器,指向第二重(<名称,数量>)
for(it=fr.begin();it!=fr.end();it++)
{
cout<<(*it).first<<endl;
for(it2=(*it).second.begin();it2!=(*it).second.end();it2++)
cout<<" |----"<<(*it2).first<<"("<<(*it2).second<<")"<<endl;
}
if(t>=1) cout<<endl;
}
return 0;
}