acwing校庆

题目链接:

题目链接:https://www.acwing.com/problem/content/4272/

此题目同pat 甲级1157

难度评价:易

解题思路:给定两个字符串集合求交集,如果有交集,则统计到来的校友人数,并且如果发现yyyymmdd较小的(则是生日较大的)就更新当前的最大年龄的校友;

如果没有交集,并且发现发现yyyymmdd较小的(则是生日较大的)就更新当前的最大年龄的到来者;

求交集用hash表来实现

参考代码:

 1 #include<bits/stdc++.h>//Acwing每日一题-校庆
 2 using namespace std;
 3 #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
 4 #define int long long
 5 const int N=1e5+10;
 6 int n;
 7 int m;
 8 int ans;
 9 unordered_set<string>hash1;
10 signed main()
11 {
12     IOS;
13     cin>>n;
14     while(n--)
15     {
16         string a;
17         cin>>a;
18         hash1.insert(a);
19     }
20     string xy="";
21     string old="";
22     cin>>m;
23     while(m--)
24     {
25         string a;
26         cin>>a;
27         if(hash1.count(a))//如果在hash表中发现a
28         {
29             ans++;
30             if(xy.empty()||xy.substr(6,8)>a.substr(6,8))//从下标为6的字符串开始向后数8位
31             {
32                 xy=a;//更新a
33             }
34         }
35         if(old.empty()||old.substr(6,8)>a.substr(6,8))
36         {
37             old=a;
38         }
39     }
40     cout<<ans<<endl;
41     if(ans)//存在交集
42         cout<<xy<<endl;
43     else
44         cout<<old<<endl;
45     return 0;
46 }

 

posted @ 2022-07-12 10:55  江上舟摇  阅读(18)  评论(0编辑  收藏  举报