PAT

A1157 Anniversary (25 分)

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 (≤105). 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 (≤105). 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

题目大意:

浙江大学在2019年要举办122周年的校庆,为了准备这次校庆,校友会收集了所有校友的ID,现在你的任务是:编写一个程序统计参加这个校庆活动中校友的人数。

Input:每一个输入样例分为两部分,第一部分是输入校友的信息;第二部分则是输入参加校庆所有人的信息。

output:

  • 第一行输出有多少位校友来参加校庆了;
  • 第二行则输出校友中年龄最大的校友的ID(ID的第7~14位是出生日期),

    • 如果没有校友来参加,则输出来宾中年龄最大的人

思路

  • 利用一个集合set<string>记录校友的ID,维护两个记录年龄最大的变量:来宾中年龄最大的allmin,来宾中校友年龄最大的tmin(即出生日期最小的)
  • 让后在输入来宾时,判断这个来宾是否是校友,如果是,则与当前记录的ID比较,是否为年龄最大的校友(更新记录最大值信息)

AC代码:

#include <bits/stdc++.h>
using namespace std;

int main() {
    set<string> st;
    int n, m;
    scanf("%d", &n);
    string id;
    for (int i = 0; i < n; ++i) {
        cin >> id;
        st.insert(id);
    }
    int cnt = 0;
    int allmin = INT_MAX, tmin = INT_MAX;
    string s1, s2;
    scanf("%d", &m);
    for (int i = 0; i < m; ++i) {
        cin >> id;
        int year = stoi(id.substr(6, 8));
        if (year < allmin) {
            allmin = year;
            s1 = id;
        }
        if (st.find(id) != st.end()) {
            if (tmin > year) {
                tmin = year;
                s2 = id;
            }
            ++cnt;
        }
    }
    printf("%d\n", cnt);
    if (cnt == 0) cout << s1;
    else cout << s2;
    return 0;
}
This is just a placeholder img.