模拟校友即可
题库之前有一题类似,y总讲过
substr()截取生日的灵活运用
/**
判断校友是否到场, 然后判断校友到场人数
找出最老的校友,若没有校友,找最老的那一个
*/
#include <iostream>
#include <cstring>
#include <unordered_map>
using namespace std;
int main()
{
int m, n;
cin >> m;
unordered_map<string,bool> ext;
for(int i = 0; i < m; i++)
{
string s;
cin >> s;
ext[s] = true;
}
cin >> n;
int cnt = 0;
string max_stu = "99999999", max_all = "99999999", res, res_all;
for(int i = 0; i < n; i++)
{
string s, str;
cin >> s;
str = s.substr(6, 8);
if (ext[s])
{
cnt++;
if (str < max_stu) max_stu = str, res = s;
}
if (str < max_all) max_all = str, res_all = s;
}
printf("%d\n", cnt);
if (!cnt) cout << res_all;
else cout << res;
}
之前做的
#include <iostream>
#include <cstring>
#include <vector>
#include <unordered_map>
using namespace std;
unordered_map<string,bool> visit;
int main()
{
int m, n;
cin >> m;
string str;
bool flag = true, f1 = true;
while (m --)
{
cin >> str;
visit[str] = true;
}
cin >> n;
vector<string> a;
string id_all, id_ali, min, min_aln;
while (n --)
{
cin >> str;
string sstr = str.substr(6, 8);
if (flag) min = sstr, flag = false, id_all = str;
else{
if (sstr < min) min = sstr, id_all = str;
}
if (visit[str])
{
a.push_back(sstr);
if (f1) min_aln = sstr, f1 = false, id_ali = str;
else
{
if (sstr < min_aln) min_aln = sstr, id_ali = str;
}
}
}
cout << a.size() << "\n";
if (a.size() > 0) cout << id_ali;
else cout << id_all;
}