PAT 2019-3 7-2 Anniversary

Description:

Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the celebration, the alumni association (校友会) has gathered the ID's of all her alumni. Now your job is to write a program to count the number of alumni among all the people who come to the celebration.

Input Specification:

Each input file contains one test case. For each case, the first part is about the information of all the alumni. Given in the first line is a positive integer N (≤). Then N lines follow, each contains an ID number of an alumnus. An ID number is a string of 18 digits or the letter X. It is guaranteed that all the ID's are distinct.

The next part gives the information of all the people who come to the celebration. Again given in the first line is a positive integer M (≤). Then M lines follow, each contains an ID number of a guest. It is guaranteed that all the ID's are distinct.

Output Specification:

First print in a line the number of alumni among all the people who come to the celebration. Then in the second line, print the ID of the oldest alumnus -- notice that the 7th - 14th digits of the ID gives one's birth date. If no alumnus comes, output the ID of the oldest guest instead. It is guaranteed that such an alumnus or guest is unique.

Sample Input:

5
372928196906118710
610481197806202213
440684198612150417
13072819571002001X
150702193604190912
6
530125197901260019
150702193604190912
220221196701020034
610481197806202213
440684198612150417
370205198709275042

Sample Output:

3
150702193604190912

Keys:

  • 字符串处理

Code:

 1 /*
 2 统计校友会到场的人员信息
 3 校友人数N<=1e5
 4 N个人的ID号
 5 到场人数M<=1e5
 6 M个人的ID号
 7 
 8 到场校友的人数
 9 年龄最大的校友ID
10 如果没有校友到场,打印非校友中年龄最大的ID
11 */
12 #include<cstdio>
13 #include<string>
14 #include<unordered_map>
15 #include<iostream>
16 using namespace std;
17 unordered_map<string,int> mp;
18 
19 int main()
20 {
21 #ifdef ONLINE_JUDGE
22 #else
23     freopen("Test.txt", "r", stdin);
24 #endif // ONLINE_JUDEG
25 
26     int n;
27     scanf("%d", &n);
28     for(int i=0; i<n; i++)
29     {
30         string s;
31         cin >> s;
32         mp[s] =1;
33     }
34     scanf("%d", &n);
35     string s,s1,s2;
36     int cnt=0;
37     for(int i=0; i<n; i++)
38     {
39         cin >> s;
40         if(mp[s] == 1)
41         {
42             cnt++;
43             if(s1.size()==0 || s1.substr(6,8)>s.substr(6,8))
44                 s1 = s;
45         }
46         else
47         {
48             if(s2.size()==0 || s2.substr(6,8)>s.substr(6,8))
49                 s2 = s;
50         }
51     }
52     printf("%d\n", cnt);
53     if(cnt)
54         printf("%s", s1.c_str());
55     else
56         printf("%s", s2.c_str());
57 
58     return 0;
59 }

 

posted @ 2019-09-01 19:52  林東雨  阅读(220)  评论(0编辑  收藏  举报