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 ...
随机推荐
- chrome一个奇怪的问题
我去........... 这牢骚发完了才发现, 多谢了个e 呃................. ================================= 晚上用bootstrap搭建一 ...
- windows过滤指定IP
通过windows的安全管理策略工具我们可以实现对IP的过滤.整个过程比较复杂.我们以图形演示. 下面我们以windows 8.1作为示例. 1.控制面板=>管理工具=>本地安全策略. 2 ...
- DotNetBar.MetroTilePanel 样式、加载数据、获取数据
描述下:MetroTilePanel包含子集ItemContainer 子集下面又包含子集MetroTileItem 目前我用到的就是这三层 因为需求所以整个模块全部由代码实现 1.ItemCon ...
- C#——Socket
最近公司让我搞个socket小程序(服务端). 主要的功能:客户端发字符串到服务端,服务端接收后在界面上显示. 参照了网上许多代码,自己从中修改整理代码. public Form4() { Initi ...
- 关于SqlServer连接错误
以前用数据库好好的,今天突然就出现连接错误,贴出出错误消息 出现这种错误的原因:服务里面sqlserver服务没有打开. 解决方案 : 计算机右键,打开管理,找到服务,把服务里面的SQL Server ...
- javascript实现八大排序
开学一个月,已经多次梦见笔试出现数据结构算法题,我对数据结构的恐惧已经多于任何“妖魔鬼怪”了.呵呵,看来真的很有必要复习一下常用的数据结构,免得“噩梦”成真. 数据机构等编程基础的重要性不用多说,直接 ...
- kubectl get componentstatus ERROR:HTTP probe failed with statuscode: 503
通过kubectl命令可以查看k8s各组件的状态: [root@wecloud-test-k8s-1 ~]# kubectl get cs NAME STATUS MESSAGE ERROR cont ...
- php中的XML DOM(10)
1.PHP DOM (1) Php中的DOM跟javascript不一样,属性不用另外增加一个节点 2.主要类 DOMDocument :文档类 DOMNodeList :节点列表类 DOMNode ...
- 【Oracle 12c】CUUG OCP认证071考试原题解析(31)
31.choose the best answer Which statement is true regarding the USING clause in table joins? A) It c ...
- “全栈2019”Java第一百章:局部内部类可以实现接口吗?
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...