PAT A190302 2019.09.01 【结构体sort map】

7-2 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 (≤10​5​​). 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(≤10​5​​). 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

思路分析

这是19年3月甲级的第2题,也是我当时的考题,9月1号教育超市重新做了一回。

3月考的时候不会map 不会结构体sort 现场找出个substr,做了1个多小时强行循环排序,最后14分(超时、输出0)

这次教育超市做得了20分,最后一个点判断情况后再次sort就不会超时,在前面sort两回再输出会导致超时

还有5分没过应该是当没有校友的时候,也要输出校友的数量:0

等9月8号之后题目更新后再交交看

#include<cstdio>
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
#define MAX 100010
using namespace std;

typedef struct PER{
	string id;
	string b;
}PER;

PER per[MAX];
map<string,int>haveid;
int n;

PER gue[MAX];
int m;

bool cmp(PER a,PER b)
{
	return a.b<b.b;
}

int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		string id;cin>>per[i].id;
		per[i].b=per[i].id.substr(6,8);
		haveid[per[i].id]=i;
	}	
	
	vector<PER>canyu;
	vector<PER>xiaoyou;
	cin>>m;
	for(int i=1;i<=n;i++)
	{
		string id;cin>>gue[i].id;
		gue[i].b=gue[i].id.substr(6,8);
		canyu.push_back(gue[i]);
		if(haveid[gue[i].id]!=0)xiaoyou.push_back(gue[i]);
	}
	
	if(xiaoyou.size()>0)
	{
		sort(xiaoyou.begin(),xiaoyou.end(),cmp);
		cout<<xiaoyou.size()<<endl<<xiaoyou[0].id;
	}

	else
	{
		sort(canyu.begin(),canyu.end(),cmp);
		cout<<"0"<<endl<<canyu[0].id;//output 0
	}

	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值