PAT甲级 1022 Digital Library (30 分) 题解

在这里插入图片描述在这里插入图片描述在这里插入图片描述
\quad 建立5个map,用于书名,作者,关键词,出版社,出版日期与书编号之间的映射,然后输出即可。
Tips:获得一句字符串后,比如“test code debug sort keywords”,如何将其按空格分成若干个单词,借用sstream头文件里面的istringstream可以很方便的完成。

#include <sstream>
string s, temp;
vector<string> v;
istringstream is(s);  // 进入字符串流进行重新读入
while(is >> temp) v.push_back(id); // 这样,v里面存放的就是按空格分割好的各个单词

\quad 程序如下所示

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <sstream>
using namespace std;

map<string, vector<string> > mp[6];
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	int N; cin >> N; 
	string id, s, temp;
	getline(cin, s, '\n');
	while(N--)
	{
		getline(cin, id, '\n');
		for (int i = 1; i <= 5; ++i)
		{
			getline(cin, s, '\n');
			if(i!=3) mp[i][s].push_back(id);
			else{
				istringstream is(s);  // 进入字符串流进行重新读入
				while(is >> temp) mp[3][temp].push_back(id);
			}
		}
	}
	int M; cin >> M;
	getline(cin, s, '\n');
	while(M--)
	{
		getline(cin, s, '\n');
		int flag = s[0]-'0';
		cout << s << endl;
		string key = s.substr(3);
		if(mp[flag].count(key)>0){
			vector<string> res = mp[flag][key];
			sort(res.begin(), res.end());
			for (int i = 0; i < res.size(); ++i){
				cout << res[i] << endl;
			}
		}
		else cout << "Not Found" << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值