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:

537292819690611871061048119780620221344068419861215041713072819571002001X1507021936041909126530125197901260019150702193604190912220221196701020034610481197806202213440684198612150417370205198709275042

Sample Output:

3150702193604190912

题目限度:

题目粗心:

给出所有校友会成员的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;}