PAT乙级1095 解码PAT准考证 (25 分)

https://pintia.cn/problem-sets/994805260223102976/problems/1071786104348536832

借鉴的柳神思路
https://blog.csdn.net/liuchuo/article/details/84972869

#include 
#include 
#include 
#include 
#include 
using namespace std;
struct node{
	string s;
	int v;
};
vector stu(10000);
bool cmp(node a, node b){
	if(a.v == b.v)	return a.s < b.s;
	else	return a.v > b.v;
}
int main(){
	int N, M;
	scanf("%d%d", &N, &M);
	for(int i = 0; i < N; i++)
		cin >> stu[i].s >> stu[i].v;
	for(int i = 1; i <= M; i++){
		vector result;
		int style, count = 0, score = 0;;
		string order;
		cin >> style >> order;
		printf("Case %d: %d %s\n", i, style, order.c_str());
		if(style == 1){
			for(int j = 0; j < N; j++){
				if(stu[j].s[0] == order[0])
					result.push_back(stu[j]);
			}
		}else if(style == 2){
			for(int j = 0; j < N; j++){
				if(stu[j].s.substr(1,3) == order){
					count++;
					score += stu[j].v;	
				}
			}
			if(count)	printf("%d %d\n", count, score);
		}else if(style == 3){
			unordered_map m;
			for(int j = 0; j < N; j++)
				if(stu[j].s.substr(4, 6) == order) m[stu[j].s.substr(1, 3)]++;
			for(auto i : m)	result.push_back({i.first, i.second});	
		}
		sort(result.begin(), result.end(), cmp);
		for (int j = 0; j < result.size(); j++) printf("%s %d\n", result[j].s.c_str(), result[j].v);
		if(((style != 2) && !result.size()) || (style == 2 && !count))	printf("NA\n");
	}
	return 0;
}

你可能感兴趣的:(PAT刷题之旅,PAT乙级)