思路
用两个map存储输出的两部分id,mapp1存储校友的id,mapp2存储出席的id。当输入出席id时,用mapp1.find()函数搜索是否为校友,若是则sum++,并将该id记为1;否则记为0。
重写mapp2的compare函数,根据id的第7-14位从小到大排,即年纪越大的越靠前。
输出sum和排序后mapp2中第一个值为1的id。
代码
#include<iostream>
#include<map>
#include<string>
using namespace std;
struct MyCmp{
bool operator()(const string& str1,const string& str2)const{
string s1 = str1.substr(6,8);
string s2 = str2.substr(6,8);
return s1 < s2;
}
};
int main(){
int n,m,i,sum = 0;
string str;
cin>>n;
map<string,int> mapp1;
map<string,int,MyCmp> mapp2;
for(i = 0;i < n; ++ i){
cin>>str;
mapp1[str] = 0;
}
cin>>m;
for(i = 0;i < n; ++ i){
cin>>str;
if(mapp1.find(str) != mapp1.end()){
sum ++;
mapp2[str] = 1;
}else mapp2[str] = 0;
}
cout<<sum<<endl;
map<string,int,MyCmp>::iterator it = mapp2.begin();
if(sum > 0){
for(;it != mapp2.end(); ++ it){
if(it->second == 1)cout<<it->first<<endl;
break;
}
}else cout<<it->first<<endl;
return 0;
}