1153 Decode Registration Card of PAT (25分)附测试点1、4分析

本题为PAT的甲组1153题,也是PAT的乙组1095题,此前写过一篇博文讲过乙组1095题(参见1095 解码PAT准考证 (25分)击破测试点3、4,50ms内通关),当时使用的是字符串作为键值进行处理,可以正确通过测试点1、4。本次采用的是atoi函数将字符串转为int数字作为键值进行处理,碰到了测试点1、4的错误。测试点1、4的问题在于有查询过程中有可能出现的前导零,输出时候需要原样进行输出。

即查询时遇到
2 0010107
需要输出
Case 3: 2 0010107
NA
而非
Case 3: 2 10107
NA

下面是通过代码,由于查找索引变为int整数,速度方面比上一篇博文的代码要更胜一筹。
在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;

struct Node {
    char Id[20];
    char level;
    int site, date, id, score;
    Node() {
        level = site = date = id = score = 0;
    }
    Node(char str[], int sc) {
        strcpy(Id, str);
        level = Id[0];
        char temp[10];
        for (int i = 1; i <= 3; i++) {
            temp[i - 1] = Id[i];
        }
        temp[3] = '\0';
        site = atoi(temp);
        for (int i = 4; i <= 9; i++) {
            temp[i - 4] = Id[i];
        }
        temp[6] = '\0';
        date = atoi(temp);
        for (int i = 10; i <= 12; i++) {
            temp[i - 10] = Id[i];
        }
        temp[3] = '\0';
        id = atoi(temp);
        score = sc;
    }
};

vector<Node> vt;
map<char, vector<Node> > mplevel;
map<int, vector<int> > mpsite;
map<int, map<int, vector<int> > > mpdate;

int cmp(Node a, Node b) {
    if (a.score != b.score) {
        return a.score > b.score;
    }
    else {
        return strcmp(a.Id, b.Id) < 0;
    }
}

int cmpDate(pair<int, int>a, pair<int, int> b) {
    if(a.first!=b.first){
        return a.first > b.first;
    }
    else {
        return a.second < b.second;
    }
}

int main() {
    int n, m, sc;
    char str[20];
    scanf("%d%d", &n, &m);
    getchar();
    for (int i = 0; i < n; i++) {
        scanf("%s %d", str, &sc);
        Node nd = Node(str, sc);
        // 保存等级索引时的结果
        if (mplevel.count(nd.level) == 0) {
            mplevel[nd.level] = vector<Node>();
            mplevel[nd.level].push_back(nd);
        }
        else {
            mplevel[nd.level].push_back(nd);
        }
        //保存场次索引时的结果
        if (mpsite.count(nd.site) == 0) {
            mpsite[nd.site] = vector<int>(0);
            mpsite[nd.site].push_back(i);
        }
        else {
            mpsite[nd.site].push_back(i);
        }
        //保存日期索引时的结果
        if (mpdate.count(nd.date) == 0) {
            mpdate[nd.date] = map<int, vector<int> >();
            mpdate[nd.date][nd.site] = vector<int>(0);
            mpdate[nd.date][nd.site].push_back(i);
        }
        else {
            if (mpdate[nd.date].count(nd.site) == 0) {
                mpdate[nd.date][nd.site] = vector<int>(0);
                mpdate[nd.date][nd.site].push_back(i);
            }
            else {
                mpdate[nd.date][nd.site].push_back(i);
            }
        }
        vt.push_back(nd);
    }
    for (auto x : mplevel) {
        sort(mplevel[x.first].begin(), mplevel[x.first].end(), cmp);
    }
    int opt, term;
    char level, space;
    for (int i = 1; i <= m; i++) {
        scanf("%d", &opt);
        if (opt == 1) {
            scanf("%c%c", &space, &level);
            getchar();
            printf("Case %d: %d %c\n", i, opt, level);
            if (mplevel.count(level)) {
                for (auto x : mplevel[level]) {
                    printf("%s %d\n", x.Id, x.score);
                }
            }
            else {
                printf("NA\n");
            }
        }
        else {
            char temp[60];
            scanf("%s", &temp);
            term = atoi(temp);
            printf("Case %d: %d %s\n", i, opt, temp);
            if (opt == 2) {
                if (mpsite.count(term)) {
                    int sum = 0;
                    for (auto x : mpsite[term]) {
                        sum += vt[x].score;
                    }
                    printf("%d %d\n", mpsite[term].size(), sum);
                }
                else {
                    printf("NA\n");
                }
            }
            else if (opt == 3) {
                if (mpdate.count(term)) {
                    vector<pair<int, int> > ans;
                    for (auto x : mpdate[term]) {
                        ans.push_back(pair<int, int>(x.second.size(), x.first));
                    }
                    sort(ans.begin(), ans.end(), cmpDate);
                    for (auto x : ans) {
                        printf("%03d %d\n", x.second, x.first);
                    }
                }
                else {
                    printf("NA\n");
                }
            }
            else {
                printf("NA\n");
            }
        }
    }
    return 0;
}
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ProfSnail

谢谢老哥嗷

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值