题目:
- ### 这道题主要考察map,vector,string的综合运用
- 单号和城市都可以用string类型存储
#include <bits/stdc++.h>
using namespace std;
map<string,vector<string>> mp;
vector<string> citys;
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;++i){
string a,b;
cin>>a>>b;
//已经有了该城市
if(!mp.count(b)){
citys.push_back(b);
}
mp[b].push_back(a);
}
//遍历城市
for(const auto&city:citys){
cout<<city<<' '<<mp[city].size()<<"\n";
//遍历单号
for(const auto&i:mp[city]){
cout<<i<<"\n";
}
}
return 0;
}
整体思路:
- 判断有无重复的城市,有则不再输出
- 每次都将单号放进来
- 依次输出