PAT A1022Digital Library(***map映射set容器 + 字符串读入)

问题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336

题意:给出N本书的编号,书名,作者,关键词(多个),出版社,出版年份;并给出M个查询,每个查询给出书名,作者,关键词(单个),出版社及出版年份中的一个,要求输出满足该给出信息的所有书的编号。

Note:

1.map<string, set<int>>

把字符串string映射到一个容器set<int>,即把一个string映射到一个元素均为整数且有序的集合

2 特别注意本题中的关键词提取,用cin读入单个关键词,然后用getchar()接收在这个关键词后面的字符;若是换行符则结束关键词输入,若是空格则继续读入关键字。

3 getline(cin, str)整行读入

4 scanf("%d:  ");读入查询类型及其后的,冒号,空格。

#include<cstdio>
#include<iostream>
#include<map>
#include<set>
using namespace std;
void query(map<string, set<int> > &mp, string &temp){
	if(mp.find(temp) == mp.end())
	    printf("Not Found\n");
	else{
		for(set<int>::iterator it = mp[temp].begin(); it != mp[temp].end(); it++)
		    printf("%07d\n", *it);
	}
}
int main(){
	int n, m, id;
	map<string, set<int> > mpTitle, mpAuthor, mpKey, mpPub, mpYear;
	string Title, Author, Key, Pub, Year;
	
	cin >> n;
	char c;
	while(n--){
		cin >> id;
	    c = getchar();
		
		getline(cin, Title);
		mpTitle[Title].insert(id);
		
		getline(cin, Author);
		mpAuthor[Author].insert(id);
		
		while(cin >> Key){
			mpKey[Key].insert(id);
			c = getchar();
			if(c == '\n')
			    break;
		}
		
		getline(cin, Pub);
		mpPub[Pub].insert(id);
		
		getline(cin, Year);
		mpYear[Year].insert(id);
	}	
	
	string temp;
	int k;
    cin >> m;
    while(m--){
    	scanf("%d: ", &k);
    	getline(cin, temp);
    	cout << k <<": "<<temp<<endl;
    	if(k == 1)
    	    query(mpTitle, temp);
    	else if(k == 2)
    	    query(mpAuthor, temp);
    	else if(k == 3)
    	    query(mpKey, temp);
    	else if(k == 4)
    	    query(mpPub, temp);
    	else
    	    query(mpYear, temp);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值