欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

**甲级PAT 1022 Digital Library (map映射)

程序员文章站 2022-03-10 20:41:14
...

1022 Digital Library (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 (≤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

题目要求: 

给出N本书,每本书包含7位数的ID,书名,作者名,不超过5个关键字(每个关键字之间用空格隔开),出版社,出版年份这些信息。然后输入M个查询条件。1代表书名,2代表作者名,3代表关键字,4代表出版社,5代表出版年份。输出根据条件查询的所有书的ID号,按从小到大的顺序。若不满足输出Not Found

解题思路:

利用map映射配对除了ID的所有条件,建立一个map<string,set<int> >,把相应的条件插入到搜索的集合里。利用find查找对应条件,如果找到则将set集合的ID全部输出,如果找不到则输出Not Found

注意:

1.读取空格。c++里的cin遇到Tab,空格,回车都会停止。要读取空格需要利用getline(cin,ttitle);可以将一行读取到ttitle字符串中。

2.读取一行的关键字。可以利用循环cin不断输入。每次输入一次关键字之后读取接下来的一个字符char c= getchar()。如果c==‘\n’就说明所有关键字都已经读完,需要跳出循环。

3.读取时要严格按照要求的格式。

之前在输入第一个ID时直接用cin读取,也没有对后面的‘\n’做处理,所以输入数据没有输入完就结束了。这里有两种处理办法,第一种是cin后加一个c = getchar()将‘\n’读取。还有一种利用scanf(“%d\n”,id);

在输入查询条件时也需要按要求读取,可以先scanf("%d: ",&k);将前面的数字读取,然后利用getline(cin,s)读取后面具体输入的条件

4.利用函数查询时需要加引用,否则最后一个测试点会超时。

5.输出ID时由于用int型变量存储,不足7位的要在前面补0。printf("%07d\n",*it);

完整代码:

#include<bits/stdc++.h>
using namespace std;

map<string,set<int> >title,author,keywords,publisher,year;

void findid(map<string,set<int> >&m,string &s){
	if(m.find(s)!=m.end()){
		set<int>::iterator it;
		for(it = m[s].begin(); it != m[s].end(); it++) printf("%07d\n",*it);
	}else{
		cout<<"Not Found\n";
	}
}

int main(){
	string ttitle,tauthor,key,tpublisher,tyear,s;
	int N,id,i,M,k;
	cin>>N;
	for(i=1;i<=N;i++){
		scanf("%d\n", &id);
		getline(cin,ttitle);
		title[ttitle].insert(id);
		getline(cin,tauthor); 
		author[tauthor].insert(id);
		while(cin>>key){
			keywords[key].insert(id);
			char c = getchar();
			if(c =='\n') break;
		}
		getline(cin,tpublisher); 
		publisher[tpublisher].insert(id);
		getline(cin,tyear); 
		year[tyear].insert(id);
	}
	cin>>M;
	for(i=1;i<=M;i++){
		scanf("%d: ",&k);
		getline(cin,s);
		cout<<k<<": "<<s<<endl; 
		switch(k){
			case 1: findid(title,s);break;
			case 2: findid(author,s);break;
			case 3: findid(keywords,s);break;
			case 4: findid(publisher,s);break;
			default: findid(year,s);break;
		}
	}
	
	return 0;
}

 

相关标签: map