题目
![]()
解法1
点击查看代码
#include<iostream>
#include<vector>
#include<algorithm>
#include<unordered_map>
using namespace std;
int main(){
int N,M; scanf("%d %d",&N,&M);
vector<pair<string,int> > vp(N);
for(int i =0;i<N;i++) cin>>vp[i].first>>vp[i].second;
for(int i =1;i<=M;i++){
int type; string term;
cin >> type >> term;
printf("Case %d: %d %s\n", i, type, term.c_str());
if(type == 1){
vector<pair<string, int>> res;
for(auto& p : vp)
if(p.first[0] == term[0])
res.push_back(p);
if(res.empty()) printf("NA\n");
else {
sort(res.begin(), res.end(), [](auto& a, auto& b){
if(a.second != b.second) return a.second > b.second;
return a.first < b.first;
});
for(auto& p : res) printf("%s %d\n", p.first.c_str(), p.second);
}
}else if(type == 2){
int cnt=0,sum=0;
for(auto it : vp){
if(it.first.substr(1,3) == term) {
cnt++; sum +=it.second;
}
}
if(cnt == 0) cout<<"NA"<<endl;
else cout<<cnt<<" "<<sum<<endl;
}else{
unordered_map<string,int> siteMap;
for(auto it : vp)
if(it.first.substr(4,6) == term)
siteMap[it.first.substr(1,3)]++;
if(siteMap.empty()) cout<<"NA"<<endl;
else{
vector<pair<string,int> > siteVec(siteMap.begin(),siteMap.end());
sort(siteVec.begin(),siteVec.end(),[](pair<string,int>& p1,pair<string,int>& p2){
if(p1.second != p2.second) return p1.second > p2.second;
return p1.first < p2.first;
});
for(auto it: siteVec){
cout<<it.first<<" "<<it.second<<endl;
}
}
}
}
return 0;
}