别想太复杂,map统计一下就可以了,虽然时间有点慢(接近5s)
-
首先使用map数组计数,来的人的次数,当来的次数等于2时,说明该人是来宾中的校友,记录下来,之后需要输出
-
单独使用一个数组记录来宾(包括校友)中的所有人的身份信息,(如果来宾中没有校友时,输出第一个)
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n, m;
cin >> n;
map<string, int> sc;
string temp;
while(n--){
cin >> temp;
sc[temp]++;
}
cin >> m;
map<string, string> st2; //st2来宾
while(m--){
cin >> temp;
sc[temp]++;
string date = temp.substr(6, 8);
st2[date] = temp;
}
int ans = 0;
map<string, string> st1; //st1来宾中的校友
for(auto & [k, v] : sc){
if(v == 2){
ans++;
temp = k.substr(6, 8);
st1[temp] = k;
}
}
cout << ans << '\n';
if(ans)
cout << st1.begin()->second;
else
cout << st2.begin()->second;
return 0;
}
这题有啥好麻烦不麻烦的 只是有人手写哈希表 咱用的stl