AcWing 4269. 校庆
原题链接
简单
作者:
Java同学
,
2022-06-12 11:17:57
,
所有人可见
,
阅读 146
哈希表的做法 具体在注释
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;
// 直接暴力会超时
// vector<string> all;
// vector<string> come;
//用来表示 来参加的校友
vector<string> both;
// 1 表示出现
unordered_map<string, int> is_come;
int main()
{
int n, m;
string a;
cin >> n;
//初始化为最年轻的人
int old = 99999999;
// 记录来宾最年长
string old_people;
// 记录两者交集最年长
string old_student;
for (int i = 0; i < n; i++)
{
cin >> a;
is_come[a] = 1;
}
cin >> m;
for (int i = 0; i < m; i++)
{
cin >> a;
if (is_come[a] == 1)
{
both.push_back(a);
}
//计算来宾中最年长的
if (stoi(a.substr(6, 8)) < old)
{
old = stoi(a.substr(6, 8));
old_people = a;
}
}
cout << both.size() << endl;
// 因为要计算来的校友中最年长的 所以需要初始化年龄
old = 99999999;
if (both.size() > 0)
{
for (auto i : both)
{
// 若出现比他老的
if (stoi(i.substr(6, 8)) < old)
{
old = stoi(i.substr(6, 8));
old_student = i;
}
}
cout << old_student << endl;
}
else
{
cout << old_people << endl;
}
return 0;
}