关于算法-数据结构:PAT甲级2019年春季考试-72-Anniversary

103次阅读

共计 2609 个字符,预计需要花费 7 分钟才能阅读完成。

7-2 Anniversary (25 分)

Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the celebration, the alumni association(校友会)has gathered the ID’s of all her alumni. Now your job is to write a program to count the number of alumni among all the people who come to the celebration.

Input Specification:

Each input file contains one test case. For each case, the first part is about the information of all the alumni. Given in the first line is a positive integer $N (≤10​^5​​)$. Then N lines follow, each contains an ID number of an alumnus. An ID number is a string of 18 digits or the letter X. It is guaranteed that all the ID’s are distinct.

The next part gives the information of all the people who come to the celebration. Again given in the first line is a positive integer $M(≤10^5)$. Then M lines follow, each contains an ID number of a guest. It is guaranteed that all the ID’s are distinct.

Output Specification:

First print in a line the number of alumni among all the people who come to the celebration. Then in the second line, print the ID of the oldest alumnus — notice that the 7th – 14th digits of the ID gives one’s birth date. If no alumnus comes, output the ID of the oldest guest instead. It is guaranteed that such an alumnus or guest is unique.

Sample Input:

5
372928196906118710
610481197806202213
440684198612150417
13072819571002001X
150702193604190912
6
530125197901260019
150702193604190912
220221196701020034
610481197806202213
440684198612150417
370205198709275042

Sample Output:

3
150702193604190912

题目限度:

题目粗心:

给出所有校友会成员的 id 和所有加入庆贺客人的 id,计算加入庆贺典礼的人中校友会的成员人数,并输入年龄最大的那个校友会成员的 id,如果没有校友会成员加入,则输入客人中年龄最大的人。

算法思路:

首先应用 unordered_map<string,bool> isAlumni 存储所有的校友,其次应用 oldestBirth,oldestPeople 别离示意宾客中最年长的生日和最年长的人 id,oldestAlumni,oldestAlumniBirth 别离示意校友中最年长的生日和最年长的人 id,而后在输出 M 个加入的宾客的时候,只有以后人更加年长就更新 oldestBirth,oldestPeople,如果以后人还是校友,更新 oldestAlumni,oldestAlumniBirth,并应用 alumniNum 累计加入的校友人数, 在输出结束之后,只有 alumniNum 不等于 0,阐明有校友加入,输入 alumniNum 和 oldestAlumni,否则输入 0 和 oldestPeople。

提交后果:

AC 代码:

#include<cstdio>
#include <string>
#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;

unordered_map<string,bool> isAlumni;// 统计加入的人是否是校友

int main(){
    int N;
    scanf("%d",&N);
    for (int i = 0; i < N; ++i) {
        string id;
        cin>>id;
        isAlumni[id] = true;
    }
    int M;
    scanf("%d",&M);
    // M 个参会的人数
    string oldestBirth;
    string oldestPeople;
    string oldestAlumni;
    string oldestAlumniBirth;
    int alumniNum = 0;
    for(int i=0;i<M;++i){
        string id;
        cin>>id;
        if(i==0){
            oldestPeople = id;
            oldestBirth = id.substr(6,8);
        } else if(oldestBirth>id.substr(6,8)) {
            oldestPeople = id;
            oldestBirth = id.substr(6,8);
        }
        if(isAlumni[id]){
            ++alumniNum;
            // 以后是校友
            if(alumniNum==1){
                oldestAlumni = id;
                oldestAlumniBirth = id.substr(6,8);
            }else if(oldestAlumniBirth>id.substr(6,8)) {
                oldestAlumni = id;
                oldestAlumniBirth = id.substr(6,8);
            }
        }
    }
    if(alumniNum!=0){printf("%d\n%s",alumniNum,oldestAlumni.c_str());
    } else {printf("0\n%s",oldestPeople.c_str());
    }
    return 0;
}

正文完
 0