PAT 1153 Decode Registration Card of PAT (25 分)- 甲级

参照柳神的代码完成,想要更完美的体验请前往柳神的博客

//个人习惯,题中并未用到如此多的头文件
#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值