PAT 1157 Anniversary(25分)

原题链接:PAT 1157 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

题目大意:

周年纪念日会有很多人来参加,其中有一部分是校友。
现在给出一份校友名单和一份参会人名单,问你参会人中有多少校友,以及返回参会校友中年龄最大的人。
如果没有校友参加,那么就返回参会人中的年龄最大的人。

方法一:模拟

思路:

按照题目说的模拟即可,校友用set存储,判断年龄的时候用substr

C++ 代码:

#include <iostream>
#include <cstring>
#include <set>
using namespace std;

const int maxn = 1e5 + 10;

int n, m, ans = 0; // ans为来访人中校友数 
set<string> st; // 存储校友
string old_1, old_2; // ans_1为校友中年级最大的 ans_2为来访人中素数最大的 

int main(){
	
	// 存储校友 
	cin >> n;
	for(int i = 1; i <= n; i++ ){
		string tmp;
		cin >> tmp;
		st.insert(tmp);
	}
		
	// 遍历来访人 
	cin >> m;
	for(int i = 1; i <= m; i++ ){
		string tmp;
		cin >> tmp;
		if(st.find(tmp) != st.end())
			ans++;
			
		if(i == 1){
			old_1 = tmp;
			old_2 = tmp;
		} 
		
		else{
			// 校友中的最年长者 
			if(st.find(tmp) != st.end() && tmp.substr(6, 8) < old_1.substr(6, 8))
				old_1 = tmp;
			
			// 来访人中的最年长者 
			if(tmp.substr(6, 8) < old_2.substr(6, 8))
				old_2 = tmp; 
		}	
	}
	
	cout << ans << endl;
	if(ans == 0)
		cout << old_2 << endl;
	else
		cout << old_1 << endl; 
	
	return 0;
} 

复杂度分析:

  • 时间复杂度:O(m+n),遍历两个表
  • 空间复杂度:O(m),set存储校友名单

补充:

substr:

// 获得从pos开始的len个字符的拷贝
s.substr(pos, len);
  • pos的默认值是0,len的默认值是s.size() - pos,即不加参数会默认拷贝整个s
  • 若pos的值超过了string的大小,则substr函数会抛出一个out_of_range异常;若pos+n的值超过了string的大小,则substr会调整n的值,只拷贝到string的末尾
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值