1022 Digital Library (30分)-STL大杂烩

A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID's.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤10​4​​) which is the total number of books. Then N blocks follow, each contains the information of a book in 6 lines:

  • Line #1: the 7-digit ID number;
  • Line #2: the book title -- a string of no more than 80 characters;
  • Line #3: the author -- a string of no more than 80 characters;
  • Line #4: the key words -- each word is a string of no more than 10 characters without any white space, and the keywords are separated by exactly one space;
  • Line #5: the publisher -- a string of no more than 80 characters;
  • Line #6: the published year -- a 4-digit number which is in the range [1000, 3000].

It is assumed that each book belongs to one author only, and contains no more than 5 key words; there are no more than 1000 distinct key words in total; and there are no more than 1000 distinct publishers.

After the book information, there is a line containing a positive integer M (≤1000) which is the number of user's search queries. Then M lines follow, each in one of the formats shown below:

  • 1: a book title
  • 2: name of an author
  • 3: a key word
  • 4: name of a publisher
  • 5: a 4-digit number representing the year

Output Specification:

For each query, first print the original query in a line, then output the resulting book ID's in increasing order, each occupying a line. If no book is found, print Not Found instead.

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

法一:

将书籍的标题、作者、关键字、出版社、出版时间等信息封装为一个结构体,书籍编号作为map的关键字,信息结构体作为map的第二项,构建map容器Library,后期根据查找要求,遍历Library查找符合要求的书籍,输出书籍编号。这种方法的查找效率有点低,即便将cout改为printf,最后一个测试点需要尝试三四次才有一次通过。

代码一:

测试结果

#include <iostream>
#include <map>
#include <set>
#include <cstdio>
using namespace std;
struct book{
	string title,author,publisher,publishyear;
	set<string> keyword;	
};
int main(){
	int N;
	cin>>N;
	map<string,struct book> library;
	getchar();
	while(N--){
		struct book B;
		string ID,str;
		getline(cin,ID);
		getline(cin,str);
		B.title=str;
		getline(cin,str);
		B.author=str;
		getline(cin,str);
		int i;
		string keyword;
		keyword.clear();
		for(i=0;i<str.size();i++){
			if(str[i]!=' '){
				keyword+=str[i];
				if(i==str.size()-1){
					B.keyword.insert(keyword);
					keyword.clear();
				}	
			}	
			else {
				B.keyword.insert(keyword);
				keyword.clear();			
			}		
		}
		getline(cin,str);
		B.publisher=str;
		getline(cin,str);
		B.publishyear=str;
		library[ID]=B;
	//	cout<<ID<<" "<<B.title<<" "<<B.author<<" "<<B.publisher<<" "<<B.publishyear<<endl;	
	} 
	cin>>N;
	while(N--){
		int k;
		string str;
		cin>>k;
		getchar();
		getchar();
		getline(cin,str);
		printf("%d: %s\n",k,str.c_str());	
		map<string,struct book>::iterator t;
		set<string>::iterator tt;
		int flag=0,IsFound=0;
		for(t=library.begin();t!=library.end();t++){
			flag=0;
			switch(k){
			case 1 : 
						if(t->second.title==str)
							flag=1;
						break;
			case 2 :
						if(t->second.author==str)
							flag=1;
						break;
			case 3 :	
						for(tt=t->second.keyword.begin();tt!=t->second.keyword.end();tt++)
							if(*tt==str){
								flag=1;
								break;
							}
						break;
			case 4 :	
						if(t->second.publisher==str)
							flag=1;
						break;
			default:	
						if(t->second.publishyear==str)
							flag=1;
						break;
			}
			if(flag){
				printf("%s\n",t->first.c_str());
				IsFound=1;
			}				
		}
		if(!IsFound)
			printf("Not Found\n");
	}
	return 0;	
}

法二:

为了提高查找效率,可以利用map容器内部的红黑树特性,将N缩减为logN。具体做法为设置一个map数组,每个元素存储一种书籍信息,第一项为对应信息的字符串,第二项为队列,存储符合这一字符串的书籍编号,找到符合要求的字符串,输出所有书籍编号。

代码二:

测试

#include <iostream>
#include <map>
#include <set>
#include <cstdio>
using namespace std;	
int main(){
	int N;
	cin>>N;
	map<string,set<string> >library[5];
	getchar();
	while(N--){
		int i;
		string ID,str;
		getline(cin,ID);
		for(i=0;i<5;i++){
			getline(cin,str);
			if(i!=2)
				library[i][str].insert(ID);
			else {
				string keyword;
				keyword.clear();
				int j;
				for(j=0;j<str.size();j++){
					if(str[j]!=' '){
						keyword+=str[j];
						if(j==str.size()-1){
							library[2][keyword].insert(ID); 
							keyword.clear();
						}	
					}	
					else {
						library[2][keyword].insert(ID); 
						keyword.clear();			
					}		
				}	
			}
		}
	} 
	cin>>N;
	while(N--){
		int k;
		string str;
		cin>>k;
		getchar();
		getchar();
		getline(cin,str);
		printf("%d: %s\n",k,str.c_str());
		int i;
		if(library[k-1].find(str)!=library[k-1].end()){
			set<string>::iterator t;
			for(t=library[k-1][str].begin();t!=library[k-1][str].end();t++)
				printf("%s\n",(*t).c_str());
		}
		else
			printf("Not Found\n");	
	}
	return 0;	
}

 

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值