PTA (Advanced Level) 1022 Digital Library
Digital Library
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤) 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 (≤) 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个书本信息,每个书本给出id,书名,作者,关键词,出版社,发行年份,这五个信息,要求分别按照五个信息建立索引,之后给出m行查询,查询格式如下:
1:书名
2:作者
3:一个关键词
4:出版商
5:年份
要求输出查询信息所包含的所有书的id。
虽然这是一个30分的题目但是它并不是膨胀困难,书名,作者,关键词,出版社,发行年份我们都可以用string类型保存,对于书籍id我们则用int进行存储,这里可以用map建立string与set<int>的对应关系,set<int>里储存的便是对应string所包含所有书的id(由于书的id一定不重复,且最后要求由小到大输出id,用set会方便很多)。注意关键词有很多个,每个都需要建立对应关系。
AC代码
#include <bits/stdc++.h>
using namespace std;
map<string, set<int> > title, author, keyWord, publisher, year;
//分别对应 同名的书 同作者的书 同关键词的书 同出版社的书 同年的书
int n;
int main()
{
scanf("%d", &n); //输入书的个数;
for(int i = ; i < n; i++){
int id;
string tTitle, tAuthor, tKeyWord, tPublisher, tYear; cin >> id; //输入id
getchar(); //吸收换行符
getline(cin, tTitle); //输入书名
getline(cin, tAuthor); //输入作者
getline(cin, tKeyWord); //输入该书所有关键词
getline(cin, tPublisher); //输入出版社
getline(cin, tYear); //输入年份 title[tTitle].insert(id); //对应书名的集合中加入id
author[tAuthor].insert(id); //对应作者的集合中加入id
string temp; //temp用来获取每一个关键词
istringstream cinKeyWord(tKeyWord); //用tKeyWord作为输入流输入每一个关键词
while(cinKeyWord >> temp){
keyWord[temp].insert(id); //每一个关键词对应的集合中都加入id
}
publisher[tPublisher].insert(id); //对应出版社的集合加入id
year[tYear].insert(id); //对应年份的集合加入id
}
scanf("%d", &n); //输入查询个数
for(int i = ; i < n; i++){
int query;
string temp;
scanf("%d: ", &query); //输入查询控制符
getline(cin, temp); //输入查询内容
cout << query << ": " << temp << endl;
//由于string传送速度较慢,若将查询过程包装为一个函数,最后一个结点会超时
//不过传引用仿佛可以解决这个问题
if(query == ){ //控制符为1 输出 对应书名集合中包含的书
if(title.find(temp) == title.end())
printf("Not Found\n");
else
for(auto i : title[temp])
printf("%07d\n", i);
}else if(query == ){ //控制符为2 输出 对应作者集合中包含的书
if(author.find(temp) == author.end())
printf("Not Found\n");
else
for(auto i : author[temp])
printf("%07d\n", i);
}else if(query == ){ //控制符为3 输出 对应关键词集合中包含的书
if(keyWord.find(temp) == keyWord.end())
printf("Not Found\n");
else
for(auto i : keyWord[temp])
printf("%07d\n", i);
}else if(query == ){ //控制符为4 输出 对应出版社集合中包含的书
if(publisher.find(temp) == publisher.end())
printf("Not Found\n");
else
for(auto i : publisher[temp])
printf("%07d\n", i);
}else if(query == ){ //控制符为5 输出 对应年份集合中包含的书
if(year.find(temp) == year.end())
printf("Not Found\n");
else
for(auto i : year[temp])
printf("%07d\n", i);
}
}
return ;
}
PTA (Advanced Level) 1022 Digital Library的更多相关文章
- PAT (Advanced Level) 1022. Digital Library (30)
		
简单模拟题. 写的时候注意一些小优化,小心TLE. #include<iostream> #include<cstring> #include<cmath> #in ...
 - PAT 1022 Digital Library[map使用]
		
1022 Digital Library (30)(30 分) A Digital Library contains millions of books, stored according to th ...
 - pat 甲级 1022. Digital Library (30)
		
1022. Digital Library (30) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A Di ...
 - PAT 甲级 1022 Digital Library (30 分)(字符串读入getline,istringstream,测试点2时间坑点)
		
1022 Digital Library (30 分) A Digital Library contains millions of books, stored according to thei ...
 - 1022 Digital Library (30 分)
		
1022 Digital Library (30 分) A Digital Library contains millions of books, stored according to thei ...
 - 1022 Digital Library——PAT甲级真题
		
1022 Digital Library A Digital Library contains millions of books, stored according to their titles, ...
 - PAT Advanced 1022 Digital Library (30 分)
		
A Digital Library contains millions of books, stored according to their titles, authors, key words o ...
 - 1022. Digital Library (30)
		
A Digital Library contains millions of books, stored according to their titles, authors, key words o ...
 - 1022. Digital Library (30) -map -字符串处理
		
题目如下: A Digital Library contains millions of books, stored according to their titles, authors, key w ...
 
随机推荐
- Postgresql 9.6 搭建 异步流复制 和 同步流复制 详细教程
			
Basic Replication If you’re feeling overwhelmed, try setting up a slave to see how easy it is! We’ll ...
 - [Erlang33]使用recon从网页查看Erlang运行状态
			
0.需求分析 Erlang最好的卖点之一就是提供了一个非常强大的shell来查看Node运行时的各种状态,可以进行各种各样的内部查看,在运行时调试和分析,热更新代码. 但是总有一些在生产环境下要慎 ...
 - Buffer Pool--内存总结1
			
物理地址空间是处理器用来访问位于总线上的所有部件的集合.在32位处理器上,地址总线为32位,寻址空间为4GB.在使用PAE的32位服务器上,地址总线为36位,寻址空间为64GB.在64位的处理器上生产 ...
 - leetcode 有效的括号
			
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: - 左括号必须用相同类型的右括号闭合. - 左括号必须以正确的顺序闭合. 注意空字符 ...
 - DS博客作业01-日期抽象数据类型的设计与实现
			
1.思维导图和学习体会 1.1绪论知识思维导图 1.2学习体会 通过这几节课数据结构的新学习,让我感到了难度,很多概念性的东西,不是很好理解,老师在讲内容的时候,很容易跟不上节奏,我发现这门课的学习一 ...
 - mongodb 备份还原
			
一.简介 说起来数据库的“备份-还原”,在RDBMS系统中,都有很好的支持,也有很多选项可以设置,功能强大,也能自动完成大部分的备份功能,只要当初设置好了就可以了.对于MongoDB文档型的数据库来说 ...
 - (转)科普:SATA、PCIe、AHCI、NVMe
			
原文链接:https://forum.51nb.com/thread-1670848-1-1.html IT 界总喜欢发明新名词.而且同一个东西,可能有几个不同的名字.同一个名字,又可能指不同的东西. ...
 - 【OCP-12c】CUUG 071题库考试原题及答案解析(20)
			
20.choose two Examine the description of the EMP_DETAILS table given below: Which two statements are ...
 - Spring-JDBDTamplate 的操作
			
基本的 增,删,改:(只演示增加 因为他们调用的方法都是update方法): package com.hxzy.spring_jdbc_template; import org.springfr ...
 - JAVA基本数据类型所占字节数是多少?
			
byte 1字节 short 2字节 int 4字节 long 8字节 ...