PAT2019春7-2 Anniversary(25 分)

tech2026-09-26  1

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

思路

输入n个校友id和m个参加校庆人的id,输出参加校庆的校友个数,参加校庆的校友中年龄最大的id(如果校友一个没来,就输出参加校庆人中年龄最大的id)。

我采用结构体把是否是校友,是否出席,id存下来,然后通过unordered_map来建立id和数组下标的映射。输入时设定cnt,如果是校友参会就将cnt加1.再根据是否出席,是否是校友,年龄大小排序整个数组,输出第一个node的id。

我的代码(只在本地测试了)

#include<stdio.h> #include<iostream> #include<unordered_map> #include<algorithm> using namespace std; struct node{ bool alumni,attend; string id; }a[100005]; int n,m,cnt=0; unordered_map<string,int> mp; bool cmp(node a,node b){ if(a.attend!=b.attend) return a.attend>b.attend; else if(a.alumni!=b.alumni){ return a.alumni>b.alumni; }else return a.id.substr(6,8)<b.id.substr(6,8); } int main(){ scanf("%d",&n); string s; for(int i=0;i<n;i++){ cin>>s; a[i]={true,false,s}; mp[s]=i; } scanf("%d",&m); for(int i=0;i<m;i++){ cin>>s; if(mp.find(s)==mp.end()){ a[n+i]={false,true,s}; mp[s]=n+i; }else{ a[mp[s]].attend=true; cnt++; } } sort(a,a+n+m-cnt,cmp); printf("%d\n%s",cnt,a[0].id.c_str()); return 0; }

 

最新回复(0)