1022 Digital Library (30 分)

版本2

#include <string>
#include <vector>
#include <set>
#include <unordered_map>
#include <iostream>
#include <algorithm>  
using namespace std;

int main(){
	int n, m;
	cin >> n;
	string id, title, author, publisher, publishyear, key;
	getchar();
	unordered_map<string, set<string>> mp;
	for(int i = 0; i < n; i++){
		getline(cin, id);
		getline(cin, title);
		mp[title].insert(id);
		getline(cin, author);
		mp[author].insert(id);
		while(cin>>key){
			char c = getchar();
			mp[key].insert(id);
			if(c == '\n') break;
		}
		getline(cin, publisher);
		mp[publisher].insert(id);
		getline(cin, publishyear);
		mp[publishyear].insert(id);
	}
	cin >> m;
	int num;
	getchar();
	while(m--){
		getline(cin, key);
		num = key[0] - '0';
		key = key.substr(3);
		cout<<num<<": "<<key<<endl;
		if(mp[key].size() == 0){
			cout<<"Not Found"<<endl;
		}else{
			for(auto it: mp[key]){
				cout<<it<<endl;
			}
		}
	}
	return 0;
}

注意:
1.书名是7位数,需要补零。
2.题目要求按照从小到大输出书名,所以用set比较好。
3.关键字的读入技巧要学到。
4.对参数使用引用会稍微快点。

版本1

//map的应用 
#include<cstdio>
#include<iostream>
#include<map>
#include<set> 
#include<algorithm>
using namespace std;
map<string,set<int> > mpT,mpA,mpK,mpP,mpY;
void query(map<string,set<int> >& mp, string &str){ //听说引用会快点 
	if(mp.find(str) == mp.end()) printf("Not Found\n");
	else{
		for(set<int>::iterator it=mp[str].begin(); it!=mp[str].end();it++){
			printf("%07d\n",*it);
		}
	}
}

int main(){
	int n,m,id,type;
	string title,author,key,pub,year;
	scanf("%d",&n);
	
	for(int i=0;i<n;i++){
		scanf("%d",&id);
		char c = getchar();
		getline(cin, title);
		mpT[title].insert(id);
		getline(cin,author);
		mpA[author].insert(id);
		while(cin>>key){
			mpK[key].insert(id);
			c = getchar();
			if(c == '\n') break;
		}
		getline(cin, pub);
		mpP[pub].insert(id);
		getline(cin,year);
		mpY[year].insert(id);
	}
	string temp;
	scanf("%d",&m);
	for(int i=0;i<m;i++){
		scanf("%d: ",&type);
		getline(cin, temp);
		cout<<type<<": "<<temp<<endl;
		if(type==1) query(mpT, temp);
		else if(type==2) query(mpA, temp);
		else if(type==3) query(mpK, temp);
		else if(type==4) query(mpP, temp);
		else query(mpY, temp);
	}	
	return 0;
} 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值