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 ...
随机推荐
- 溢出文本省略号表示的css实现及polyfill
需求经常有需要对文字溢出进行处理,通常是在文字显示部分的末尾添加“...”等.如下:
- sqlserver数据库存储汉字出现?
问题:有些相对复杂的汉字在数据库里会变成? 解决办法:原来数据类型是varchar,将数据类型修改为nvarchar
- 热更新(一) 之Lua语法的学习
热更新 如热更新果需要更换UI显示,或者修改游戏的逻辑,这个时候,如果不使用热更新,就需要重新打包,然后让玩家重新下载(浪费流量和时间,体验不好).热更新可以在不重新下载客户端的情况下,更新游戏的内容 ...
- 利用backgroundwork----递归读取网页源代码,并下载href链接中的文件
今天闲着没事,研究了一下在线更新程序版本的问题.也是工作中的需要,开始不知道如何下手,各种百度也没有找到自己想要的,因为我的需求比较简单,所以就自己琢磨了一下.讲讲我的需求吧.自己在IIs上发布了一个 ...
- MvvmLight框架使用入门(三)
本篇是MvvmLight框架使用入门的第三篇.从本篇开始,所有代码将通过Windows 10的Universal App来演示.我们将创建一个Universal App并应用MvvmLight框架. ...
- iOS 获取 UITabViewController 和 UINavigationController 的图标位置
这些图标是放在 UITabBar 和 UINavigationBar 里的.所以只要遍历它们的 subViews,找到类型是 UIButton 的就可以了. 所有想获取它们的相对位置很容易. 获取到相 ...
- 双绞线的制作(常用568B)
EIA/TIA的布线标准中规定了两种双绞线的线序568A与568B 标准568A: 绿白—1 绿—2 橙白—3 蓝—4 蓝白—5 橙—6 棕白—7 棕--8 标准568B: 橙白—1 ...
- redhat linux elk命令
安装es后不能启动原因在elasticsearch.yml中加如下两个配置: bootstrap.memory_lock: falsebootstrap.system_call_filter: fal ...
- 解决org.apache.lucene.store.AlreadyClosedException: this Directory is closed
在Lucene中,关闭一个IndexWriter时抛出AlreadyClosedException异常: org.apache.lucene.store.AlreadyClosedException: ...
- 理解 atime,ctime,mtime (上)
理解 atime,ctime,mtime (上) Unix文件系统会为每个文件存储大量时间戳.这意味着您可以使用这些时间戳来查找任意时间访问到的任何文件或目录(读取或写入),更改(文件访问权限更改)或 ...