PAT class B true question 1095 ๐ž“œ decoding PAT quasi textual research (detailed explanation, C/C + + example, test point analysis)

Welcome to pay attention to WeChat public number: Computer Science of black science, reply to the dialog box: PAT class B real question] get all the real questions and code examples, invite you to join the PAT algorithm brush question exchange qq group: 821388108
Personal blog:

Research on decoding PAT

Title Description:

PAT examination permit number consists of four parts:

  • The first is level, that is, T stands for top level; A stands for level A; B stands for level B;
  • The second to fourth digit is the examination room number, ranging from 101 to 999;
  • The 5th to 10th place is the test date, with the format of year, month and day occupying 2 places in sequence;
  • The last 11-13 are candidate numbers, ranging from 000 to 999.

Given a series of candidates' registration numbers and their scores, please output all kinds of statistical information as required.

Input format:

Enter two positive integers, N (< 10410 ^ 4104) and M (< 100), which are the number of candidates and the number of statistical requirements.

Next N lines, each line gives a candidate's pass number and his / her score (integer in the range [0100]), separated by spaces.

After examinee information, give line M, each line gives a statistical requirement, the format is: type instruction, where

  • A type of 1 indicates that the scores of candidates of a specified level are required to be output in non ascending order, and the corresponding instructions give letters representing the specified level;
  • A type of 2 requires that the number of candidates and the total score of a designated examination hall be counted and output, and the corresponding instruction gives the number of the designated examination hall;
  • A type of 3 requires that the number of candidates on a specified date be counted and output by the examination room, and the corresponding instruction gives a specified date in the same format as the date on the admission ticket.

Output format:

For each statistical requirement, first output the Case เท: requirement in one line, where เท is the number of the requirement, starting from 1; the requirement is to copy the requirement given by the input. Then the corresponding statistical results are output:

  • The output format of the instruction of type 1 is the same as that of the input examinee information, i.e. the examination permit number score. For candidates with parallel scores, increase the output according to the dictionary order of their examination permit number (the topic shall be guaranteed to have no duplicate examination permit number);
  • Instructions of type 2 are output in the format of total number of people;
  • The output of the instruction of type 3 is in the order of non increasing number of people, and the format is the total number of people in the examination room number. If the number of people is in parallel, it will be output according to the increasing order of examination room number.

If the query result is empty, NA is output.

Enter example 1:

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

Output example 1:

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

Enter example 2:

1 6
T101000101000 0
3 000101
2 101
1 T
1 A
1 B
3 180908

Output example 2:

Case 1: 3 000101
101 1
Case 2: 2 101
1 0
Case 3: 1 T
T101000101000 0
Case 4: 1 A
Case 5: 1 B
Case 6: 3 180908

Source: PAT class B 1095
By CHEN, Yue
Setting: Zhejiang University

Problem solving:

Problem-solving thought

The content of the topic is relatively long, so it is more complicated to deal with. According to the requirements of the instruction, it is easy to see that not only the overall processing of the examination number, but also part of the content should be processed. Therefore, the information of each part (such as level, examination room number, etc.) should be stored separately in the structure storing each candidate's information.

Each type of statistical requirement is then dealt with separately. When a certain type of statistics requires the query result to be empty, NA is output.

Note that when copying the date in the output statistics requirements, the width is 6 bits. For example, 001010 should be output instead of 1010. If the date is processed as an integer, the issue of output width needs to be considered. If the date is not processed as an integer (such as string), the issue of output format does not need to be considered.

Pit reminder

Timeout problem

  1. The problem of timeout is usually due to the use of cin input and cout output. So we need to change them all to scanf input and printf output.

  2. If there is time-out, map is generally used when processing type 3 statistical requirements, because map will be automatically sorted by pressing the key, which will cause time-out. Changing the place where map is used to unordered map can generally solve the problem.

  3. Other timeouts may be due to problems with your algorithm.

Code example (C/C + +)

Tip: please save the following code in. cpp format (C + + program) and slide the code left and right to view the complete code (copying this article and linking to the computer side will be better)

#include <iostream>
#include <cstdlib>
#include <vector>
#include <unordered_map>    //Test point 3 with map will time out
#include <algorithm>

using namespace std;

struct student {
    string id, r, a, d, num;   //They are the examination permit number, grade, examination room number, examination date and examinee number
    int g;  // Fraction

struct room_num {
    string a;
    int c;

vector <student> stu;

bool cmp1(student x, student y) {
    if (x.g != y.g) {
        return x.g > y.g;
    else {
        return <;
bool cmp2(room_num x, room_num y) {
    if (x.c != y.c) {
        return x.c > y.c;
    else {
        return x.a < y.a;

void command1(string tmp, int &flag) {
    sort(stu.begin(), stu.end(), cmp1);
    for (int i = 0; i < stu.size(); i ++) {
        if (stu[i].r == tmp) {
            flag = 1;
            printf("%s %d\n", stu[i].id.c_str(), stu[i].g);
void command2(string tmp, int &flag) {
    int c = 0, sum = 0;
    for (int i = 0; i < stu.size(); i ++) {
        if (stu[i].a == tmp) {
            flag = 1;
            sum += stu[i].g;
    if (flag) {
        printf("%d %d\n", c, sum);
void command3(string tmp, int &flag) {
    unordered_map <string, int> mp;
    for (int i = 0; i < stu.size(); i ++) {
        if (stu[i].d == tmp) {
            flag = 1;
    vector <room_num> rn;
    for (unordered_map <string, int> :: iterator it = mp.begin(); it != mp.end(); it++) {
        room_num tmp;
        tmp.a = it -> first;
        tmp.c = it -> second;
    sort(rn.begin(), rn.end(), cmp2);
    for (int i = 0; i < rn.size(); i++) {
        printf("%s %d\n", rn[i].a.c_str(), rn[i].c);
int main() {
    int N, M;
    cin >> N >> M;
    for (int i = 0; i < N; i++) {
        char s[14];
        student tmp;
        scanf("%s%d", s, &tmp.g); = s;
        tmp.r =, 1);   //note
        tmp.a =, 3);
        tmp.d =, 6);
        tmp.num =, 3);
    for (int i = 0; i < M; i++) {
        int c, flag = 0;
        char s[7];
        string str;
        scanf("%d%s", &c, s);
        printf("Case %d: %d %s\n", i + 1, c, s);
        str = s;
        switch(c) {
            case 1:
                command1(str, flag); break;
            case 2:
                command2(str, flag); break;
                command3(str, flag); 
        if (flag == 0) {
    return 0;

Published 95 original articles, won praise 3, visited 3577
Private letter follow

Posted on Thu, 16 Jan 2020 07:49:36 -0500 by paul2463