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.ios
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.git
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.算法
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.spa
5 372928196906118710 610481197806202213 440684198612150417 13072819571002001X 150702193604190912 6 530125197901260019 150702193604190912 220221196701020034 610481197806202213 440684198612150417 370205198709275042
3 150702193604190912
给出全部校友会成员的id和全部参加庆祝客人的id,计算参加庆祝仪式的人中校友会的成员人数,并输出年龄最大的那个校友会成员的id,若是没有校友会成员参加,则输出客人中年龄最大的人。code
首先使用unordered_map<string,bool> isAlumni
存储全部的校友,其次使用oldestBirth,oldestPeople分别表示宾客中最年长的生日和最年长的人id,oldestAlumni,oldestAlumniBirth分别表示校友中最年长的生日和最年长的人id,而后在输入M个参加的宾客的时候,只要当前人更加年长就更新oldestBirth,oldestPeople,若是当前人仍是校友,更新oldestAlumni,oldestAlumniBirth,并使用alumniNum累计参加的校友人数,在输入完毕以后,只要alumniNum不等于0,说明有校友参加,输出alumniNum和oldestAlumni,不然输出0和oldestPeople。orm
#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; }