PAT甲1022 Digital Library map的妙用/模拟/数据存储/数据查询

题目

题目

Sample Input

3
1111111
The Testing Book
Yue Chen
test code debug sort keywords
ZUCS Print
2011
3333333
Another Testing Book
Yue Chen
test code sort keywords
ZUCS Print2
2012
2222222
The Testing Book
CYLL
keywords debug book
ZUCS Print2
2011
6
1: The Testing Book
2: Yue Chen
3: keywords
4: ZUCS Print
5: 2011
3: blablabla

Sample Output

1: The Testing Book
1111111
2222222
2: Yue Chen
1111111
3333333
3: keywords
1111111
2222222
3333333
4: ZUCS Print
1111111
5: 2011
1111111
2222222
3: blablabla
Not Found

思路

坑点

  • 若id不足7位要补零
  • getline()来接收输入的信息
  • 如果int后跟着字符串记得吸收换行符

进化过程

  • 弱弱版:原本把它当成一个模拟数据库查询的题来写的,构建了一个struct Book,用于存储一条书籍记录,包括id,书名等信息
    • 优点:容易想,思路顺
    • 缺点:代码啰嗦,速度慢
  • map版:后来查到大佬写的map版代码。。。好的是我太弱了
    • 优点:速度快,代码简洁
    • 缺点:如果记录很多或者某书关键词很多时,需要的空间较大

放上两种运行效果的对比

弱弱版
弱弱版

map版
在这里插入图片描述

AC代码

弱弱版

#include <bits/stdc++.h>
using namespace std;
struct Book{
	int id;
	string title,author,publisher;
	vector<string> keywords;
	int year;
} Books[10010];
void output(set<int>& results){
	for(set<int>::iterator it = results.begin();it != results.end();++it)
		printf("%07d\n",(*it)); 
}
int main(){
	int N,M;
	cin>>N;
	//Input books 
	for(int i = 1;i<=N; ++i){
		cin>>Books[i].id;
        getchar();//吸收回车
		getline(cin,Books[i].title);
		getline(cin,Books[i].author);
		string keys,temp;
		getline(cin,keys);
		//分割keyword
		istringstream inputKeys(keys);
   		while ( inputKeys >> temp )
        	Books[i].keywords.push_back(temp);
        getline(cin,Books[i].publisher);
        cin>>Books[i].year;
        getchar();
	}
	//查询 
	set<int> results;
	string qTitle, qAuthor, qKey, qPublisher; 
		int qYear;
	bool found = false;
	cin>>M;getchar();
	int option;
	while(M--){
		results.clear();
		string query;
        getline(cin,query);
        char option = query[0];
		switch(option){
			case '1'://a book title
                qTitle = query.substr(3);
				for(int i = 1;i<=N;++i)
					if(Books[i].title == qTitle)
						results.insert(Books[i].id);
				break;
			case '2'://name of an author
				qAuthor = query.substr(3);
				for(int i = 1;i<=N;++i)
					if(Books[i].author == qAuthor)
						results.insert(Books[i].id);
				break;
			case '3'://a key word
				qKey = query.substr(3);
				for(int i = 1;i<=N;++i)
					if(find(Books[i].keywords.begin(),Books[i].keywords.end(),qKey)!=Books[i].keywords.end())
						results.insert(Books[i].id); 
				break;
			case '4'://name of a publisher
				qPublisher = query.substr(3);
				for(int i = 1;i<=N;++i)
					if(Books[i].publisher == qPublisher)
						results.insert(Books[i].id);
				break;
			case '5'://a 4-digit number representing the year
				qYear = stoi(query.substr(3));
				for(int i = 1;i<=N;++i)
					if(Books[i].year == qYear)
						results.insert(Books[i].id);
				break;
		}
        cout<<query<<endl;
		if(!results.empty())
			output(results);
		else
			cout<<"Not Found"<<endl;
	}
	return 0;
} 

map版

参考1022 Digital Library

#include <bits/stdc++.h> 
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值