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 ...
随机推荐
- C# 加载配置文件
//加载配置文件 var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .Add ...
- WPF相关资料集锦
微软官方资料 .NET Framework源代码 https://referencesource.microsoft.com/ 微软官方文档 https://docs.microsoft.com/en ...
- [JS&Jquery]实现页面表格中相同内容的行或列合并
详细链接:https://shop499704308.taobao.com/?spm=a1z38n.10677092.card.11.594c1debsAGeak<script type=&qu ...
- Android 获取 content layout
if (findViewById(android.R.id.content) instanceof ViewGroup) { ViewGroup mainView = ((ViewGroup)find ...
- Kubernetes 集群安装部署
etcd集群配置 master节点配置 1.安装kubernetes etcd [root@k8s ~]# yum -y install kubernetes-master etcd 2.配置 etc ...
- Keepalived_vrrp: ip address associated with VRID not present in received packet
keepalived常见的启动报错: 5913 May 16 15:26:04 localhost Keepalived_vrrp: ip address associated with VRID n ...
- Problem I: GJJ的日常之玩游戏(GDC)
Contest - 河南省多校连萌(四) Problem I: GJJ的日常之玩游戏 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 107 Solve ...
- (二)SSO之CAS框架单点退出,自定义退出界面.
用CAS的退出,只能使用它自己的那个退出界面,如果有这样的要求, 要求退出后自动跳转到登录界面, 该如何做呢?下面这篇文章实现了退出后可以自定义跳转界面. 用了CAS,发现退出真是个麻烦事,退出后跳 ...
- python3的全局变量和局部变量
局部变量 定义在函数体内部的变量称为局部变量 函数的形参也是局部变量 局部变量的作用范围只在声明该局部变量的函数体内 局部变量在函数调用时被创建,在函数调用完成后自动销毁 全局变量 定义在函数体外,模 ...
- 什么是马拉车算法(Manacher's Algorithm)?
提出问题 最长回文子串问题:给定一个字符串,求它的最长回文子串长度. 如果一个字符串正着读和反着读是一样的,那它就是回文串.如a.aa.aba.abba等. 暴力解法 简单粗暴:找到字符串的所有子串, ...