PAT 1022 Digital Library (30)(30 分)

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 (<=10000) 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.

解答:该题还是要好好理解题意,题目暗示我们关键词和出版商不超过1000个,那么我们可以用map来实现,不仅存储高效而且查找速度快。记得一个月前我做的时候,傻傻的用结构体存储各部分内容,然后对于每次查询分别在结构体内查找,真的是耗时耗力,自己在进步,Fighting! 方法2 是借鉴了其他CSDNers的代码后的简化版本~

方法1:AC代码如下:

#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<sstream>

//#define LOCAL

using namespace std;

int main()
{
	#ifdef LOCAL
	freopen("E:\\text.txt", "r", stdin);
	#endif
	
	int N;
	map<string, set<int> > titles;  //记录书名和对应的书id 
	map<string, set<int> > authors;  //记录作者及对应的书id
	map<string, set<int> > keys;  //记录关键词及对应的作者id
	map<string, set<int> > publishers;  //记录出版社及对应的id
	map<int, set<int> > years;   //记录年份及对应的id
	 
	scanf("%d", &N);
	for(int i = 0; i < N; ++i){
		int id;
		string title, author, keywords, keyword, publisher;
		int year;
		
		scanf("%d", &id); getchar();  //此处getchar()会吃掉最后的回车换行 
		getline(cin, title); 
		titles[title].insert(id);
		getline(cin, author);  //cout << "222222222:" << author << endl; 测试输出
		authors[author].insert(id);
		getline(cin, keywords);
		stringstream ss(keywords);
		while(ss >> keyword){
			keys[keyword].insert(id);
		}
		getline(cin, publisher);
		publishers[publisher].insert(id);
		scanf("%d", &year);
		years[year].insert(id);
	}
	/*
	for(map<string, set<int> >::iterator iter = publishers.begin(); iter != publishers.end(); ++iter)
	{
		cout << iter->first << endl; 
	}
	*/
	int M;
	scanf("%d", &M);
	for(int i = 0; i < M; ++i){    
		int number; string word;
		scanf("%d:", &number); getchar();
		getline(cin, word);
		printf("%d: %s\n", number, word.c_str());
		switch(number){
			case 1:
				if(titles.find(word) != titles.end()){
					for(set<int>::iterator iter = titles[word].begin(); iter != titles[word].end(); ++iter){
						printf("%07d\n", *iter);
					}
				}else{
					printf("Not Found\n");
				}
				break;
			case 2:
				if(authors.find(word) != authors.end()){
					for(set<int>::iterator iter = authors[word].begin(); iter != authors[word].end(); ++iter){
						printf("%07d\n", *iter);
					}
				}else{
					printf("Not Found\n");
				}
				break;
			case 3:
				if(keys.find(word) != keys.end()){
					for(set<int>::iterator iter = keys[word].begin(); iter != keys[word].end(); ++iter){
						printf("%07d\n", *iter);
					}
				}else{
					printf("Not Found\n");
				}
				break;
			case 4:
				if(publishers.find(word) != publishers.end()){
					for(set<int>::iterator iter = publishers[word].begin(); iter != publishers[word].end(); ++iter){
						printf("%07d\n", *iter);
					}
				}else{
					printf("Not Found\n");
				}
				break;
			case 5:
				stringstream ss(word); int year; ss >> year;
				if(years.find(year) != years.end()){
					for(set<int>::iterator iter = years[year].begin(); iter != years[year].end(); ++iter){
						printf("%07d\n", *iter);
					}
				}else{
					printf("Not Found\n");
				}
				break;
		}
	}
	return 0;
}

方法2: AC代码如下:

#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<sstream>

#define LOCAL

using namespace std;

void query( map<string, set<int> >& tmp, const string& word){
	if(tmp.find(word) != tmp.end()){
		for(set<int>::iterator iter = tmp[word].begin(); iter != tmp[word].end(); ++iter){
			printf("%07d\n", *iter);
		}
	}else{
		printf("Not Found\n");
	}
}
int main()
{
	#ifdef LOCAL
	freopen("E:\\text.txt", "r", stdin);
	#endif
	
	int N;
	map<string, set<int> > titles;  //记录书名和对应的书id 
	map<string, set<int> > authors;  //记录作者及对应的书id
	map<string, set<int> > keys;  //记录关键词及对应的作者id
	map<string, set<int> > publishers;  //记录出版社及对应的id
	map<string, set<int> > years;   //记录年份及对应的id
	 
	scanf("%d", &N);
	for(int i = 0; i < N; ++i){
		int id;
		string title, author, keywords, keyword, publisher, year;
		
		scanf("%d", &id); getchar();  //此处getchar()会吃掉最后的回车换行 
		getline(cin, title); 
		titles[title].insert(id);
		getline(cin, author);  //cout << "222222222:" << author << endl;
		authors[author].insert(id);
		getline(cin, keywords);
		stringstream ss(keywords);
		while(ss >> keyword){
			keys[keyword].insert(id);
		}
		getline(cin, publisher);
		publishers[publisher].insert(id);
		cin >> year;
		years[year].insert(id);
	}
	/*
	for(map<string, set<int> >::iterator iter = publishers.begin(); iter != publishers.end(); ++iter)
	{
		cout << iter->first << endl; 
	}
	*/
	int M;
	scanf("%d", &M);
	for(int i = 0; i < M; ++i){    //1: The Testing Book
		int number; string word;
		scanf("%d:", &number); getchar();
		getline(cin, word);
		printf("%d: %s\n", number, word.c_str());
		//map<string, set<int> > tmp;
		switch(number){
			case 1:
				query(titles, word);
				break;
			case 2:
				query(authors, word);
				break;
			case 3:
				query(keys, word);
				break;
			case 4:
				query(publishers, word);
				break;
			case 5:
				query(years, word);
				break;
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值