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
题目大意:为了准备校庆,收集了n位校友的身份证信息:一个18位的字符串,其中第7-14位是出生年月日。来参加校庆的共m人,给出他们的身份证信息。问来了多少校友,如果有校友来了,输出他们中年龄最大者的身份证信息,否则输出来的人中年龄最大者的身份证信息。
分析:对校友的身份信息排序。对每个参加校庆的人的身份信息,用二分查找查看是否是校友。每次查找同时记录年龄最大的人的下标。最后根据是否有校友,输出对应的年龄最大的人的身份证信息。
#include<algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#define INF 0xffffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;
int findpos(string ss,string *num,int n)
{
int l=0,r=n,m;
while(l<r)
{
m=(l+r)/2;
if(num[m]==ss)return m;
else if(num[m]>ss)r=m;
else l=m+1;
}
return -1;
}
int main(void)
{
#ifdef test
freopen("in.txt","r",stdin);
//freopen("in.txt","w",stdout);
clock_t start=clock();
#endif //test
int n;scanf("%d",&n);
string num[n+5];
for(int i=0;i<n;++i)cin>>num[i];
sort(num,num+n);
int m;scanf("%d",&m);
string ss[m+5];
int date_c=100000000,index_c=-1,cnt=0,date_a=100000000,index_a=-1;
for(int i=0;i<m;++i)
{
cin>>ss[i];
int temp=0;
for(int j=6;j<13;++j)
temp=temp*10+ss[i][j]-'0';
if(temp<date_c)
date_c=temp,index_c=i;
int pos=findpos(ss[i],num,n);
if(pos!=-1)
{
cnt++;temp=0;
for(int j=6;j<13;++j)
temp=temp*10+ss[i][j]-'0';
if(temp<date_a)
date_a=temp,index_a=i;
}
}
printf("%d\n",cnt);
if(cnt)cout<<ss[index_a]<<endl;
else cout<<ss[index_c]<<endl;
#ifdef test
clockid_t end=clock();
double endtime=(double)(end-start)/CLOCKS_PER_SEC;
printf("\n\n\n\n\n");
cout<<"Total time:"<<endtime<<"s"<<endl; //s为单位
cout<<"Total time:"<<endtime*1000<<"ms"<<endl; //ms为单位
#endif //test
return 0;
}