18冬第二题 PAT甲级 1153 Decode Registration Card of PAT (25分) 鸡贼的题

本文详细介绍了PAT甲级竞赛中的1153题,该题涉及数据结构和排序算法。题目要求根据给定的注册卡号和分数,对不同级别、站点和日期的考试者进行统计和查询。文章讨论了题目的难点,如准考证号码包含字母、排序需求以及不同类型查询的处理,并提供了代码注释和满分解决方案。重点在于使用结构体存储数据并自定义排序函数,以及在处理查询3时,利用unordered_map提高效率。
部署运行你感兴趣的模型镜像

题目

A registration card number of PAT consists of 4 parts:

  • the 1st letter represents the test level, namely, T for the top level, A for advance and B for basic;
  • the 2nd - 4th digits are the test site number, ranged from 101 to 999;
  • the 5th - 10th digits give the test date, in the form of yymmdd;
  • finally the 11th - 13th digits are the testee's number, ranged from 000 to 999.

Now given a set of registration card numbers and the scores of the card owners, you are supposed to output the various statistics according to the given queries.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (≤10​4​​) and M (≤100), the numbers of cards and the queries, respectively.

Then N lines follow, each gives a card number and the owner's score (integer in [0,100]), separated by a space.

After the info of testees, there are M lines, each gives a query in the format Type Term, where

  • Type being 1 means to output all the testees on a given level, in non-increasing order of their scores. The corresponding Term will be the letter which specifies the level;
  • Type being 2 means to output the total number of testees together with their total scores in a given site. The corresponding Term will then be the site number;
  • Type being 3 means to output the total number of testees of every site for a given test date. The corresponding Term will then be the date, given in the same format as in the registration card.

Output Specification:

For each query, first print in a line Case #: input, where # is the index of the query case, starting from 1; and input is a copy of the corresponding input query. Then output as requested:

  • for a type 1 query, the output format is the same as in input, that is, CardNumber Score. If there is a tie of the scores, output in increasing alphabetical order of their card numbers (uniqueness of the card numbers is guaranteed);
  • for a type 2 query, output in the format Nt Ns where Nt is the total number of testees and Ns is their total score;
  • for a type 3 query, output in the format Site Nt where Site is the site number and Nt is the total number of testees at Site. The output must be in non-increasing order of Nt's, or in increasing order of site numbers if there is a tie of Nt.

If the result of a query is empty, simply print NA.

Sample Input:

8 4
B123180908127 99
B102180908003 86
A112180318002 98
T107150310127 62
A107180908108 100
T123180908010 78
B112160918035 88
A107180908021 98
1 A
2 107
3 180908
2 999

Sample Output:

Case 1: 1 A
A107180908108 100
A107180908021 98
A112180318002 98
Case 2: 2 107
3 260
Case 3: 3 180908
107 2
123 2
102 1
Case 4: 2 999
NA

鸡贼之处

1.准考证带字母
只能用string存准考证,所以还得弄个结构体,存准考证和分数

2.要求排序
就因为这个排序,一定要用结构体和vector,在自定义一个排序函数

3.收集、计算
如果是收集(查询1),那只用结构体和vector没有问题。
如果是计算(查询3),因为结构体和vector的成员值是只读的(我试过了,成员值只能局部改,出了框改不了),只能用map。而map会超时(很惨了),所以只能用unodered_map了(头文件同名,别忘了加)。

代码注释

1.substr
字符串截断,PAT常见操作

2.for(auto i:v)
如果用不到STL容器的编号就可以用这个c++14的枚举特性

满分代码

#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;
int n,m,k,num=0,total=0;
string s;
struct node{
    string id;
    int value;
};
bool cmp(node &a,node &b){
    return a.value==b.value?a.id<b.id:a.value>b.value;
}
int main(){
    scanf("%d %d",&n,&m);
    vector<node> v(n);
    for(int i=0;i<n;i++){
        cin>>v[i].id>>v[i].value;
    }
    for(int i=1;i<=m;i++){
        cin>>k>>s;
        printf("Case %d: %d %s\n",i,k,s.c_str());
        vector<node> ans;
        if(k==1){
            for(auto it:v){
                if(it.id.substr(0,1)==s){
                    ans.push_back(it);
                }
            }
            if(ans.size()==0){
                printf("NA\n");
                continue;
            }
            sort(ans.begin(),ans.end(),cmp);
            for(auto j:ans){
                printf("%s %d\n",j.id.c_str(),j.value);
            }
        }else if(k==2){
            num=0;
            total=0;
            for(auto it:v){
                if(it.id.substr(1,3)==s){
                    num++;
                    total+=it.value;
                }
            }
            if(num==0){
                printf("NA\n");
                continue;
            }
            printf("%d %d\n",num,total);
        }else if(k==3){
            unordered_map<string, int> ans3;
            for(auto it:v){
                if(it.id.substr(4,6)==s){
                    ans3[it.id.substr(1,3)]++;
                }
            }
            if(ans3.size()==0){
                printf("NA\n");
                continue;
            }
            for(auto it:ans3){
                ans.push_back({it.first,it.second});
            }
            sort(ans.begin(),ans.end(),cmp);
            for(auto j:ans){
                printf("%s %d\n",j.id.c_str(),j.value);
            }
        }
    }
    return 0;
}

如果喜欢,请给我点赞或者评论,感谢

 

 

 

 

您可能感兴趣的与本文相关的镜像

Dify

Dify

AI应用
Agent编排

Dify 是一款开源的大语言模型(LLM)应用开发平台,它结合了 后端即服务(Backend as a Service) 和LLMOps 的理念,让开发者能快速、高效地构建和部署生产级的生成式AI应用。 它提供了包含模型兼容支持、Prompt 编排界面、RAG 引擎、Agent 框架、工作流编排等核心技术栈,并且提供了易用的界面和API,让技术和非技术人员都能参与到AI应用的开发过程中

评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值