参照柳神的代码完成,想要更完美的体验请前往柳神的博客
//个人习惯,题中并未用到如此多的头文件
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<unordered_map>
#include<cctype>
using namespace std;
struct info{
string str;
int score;
};
bool cmp(info a,info b){
return a.score != b.score ? a.score > b.score : a.str < b.str;
}
int main(){
int n,m,k;
string s;
cin >> n >> m;
vector<info> v(n);
for(int i = 0;i < n;i++)
cin >> v[i].str >> v[i].score;
for(int i = 1;i <= m;i++){
int cnt = 0,sum = 0;
vector<info> ans;
cin >> k >> s;
printf("Case %d: %d %s\n",i,k,s.c_str());
if(k == 1)
for(int j = 0;j < n;j++)
if(v[j].str.substr(0,1) == s)
ans.push_back(v[j]);
if(k == 2){
for(int j = 0;j < n;j++){
if(v[j].str.substr(1,3) == s){
cnt++;
sum += v[j].score;
}
}
if(cnt != 0)
printf("%d %d\n",cnt,sum);
}
if(k == 3){
unordered_map<string,int> mp; //避免超时
for(int j = 0;j < n;j++){
if(v[j].str.substr(4,6) == s)
mp[v[j].str.substr(1,3)]++;
}
for(auto it:mp)
ans.push_back({it.first,it.second});
}
sort(ans.begin(),ans.end(),cmp);
for(int j = 0;j < ans.size();j++)
printf("%s %d\n",ans[j].str.c_str(),ans[j].score);
if(((k == 1 || k == 3) && ans.size() == 0) || (k == 2 && cnt == 0))
printf("NA\n");
}
return 0;
}