1022 Digital Library

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 (≤104) 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

题目大意:让你模拟一个图书管理系统,输入图书的编号,名称,作者,关键字,出版社,出版年份。然后给出M个查询,让你根据相应的查询输出对应的图书编号。

大致思路:仔细审题我们可以知道最后只让输出的是对应的图书的编号,因此我们建立名称,作者,关键字,出版社,出版年份和编号之间的映射关系,这个时候就可以考虑到map应为最后要按照大小由小到达输出相应的图书编号,如果在排序的话会超时,但是set和map内部由红黑树实现自带排序功能。所以我们可以用 map<string, set<int>> mpTitle, mpAutor, mpKey, mpPublisher, mpYear 建立映射关系。在执行m次查询操作的时候直接可以用一个查询函数用来查找对应集合 void query(map<string, set<int>> mp, string str) .在都如key_word时我们可以用 while(cin >> key_words) 同时注意吸收空格和换行,当输入换行时结束输入。这道题一定要注意字符串的读入在使用 getline() 之前一定要用 getchar() 吸收 scanf 的换行,同时在输出的时候要注意最后两组测试数据给出的id不是七位数字但我们要输出7位数字,要格式化输出id,在函数传参的时候一定要加引用不然最后一组数据会超时。

代码:

#include <bits/stdc++.h>

using namespace std;

map<string, set<int>> mpTitle, mpAutor, mpKey, mpPublisher, mpYear;

void query(map<string, set<int>> mp, string str) {
if (mp.find(str) == mp.end()) puts("Not Found");
for (auto ite : mp[str]) {
printf("%07d\n", ite);
}
} int main() {
int n;
scanf("%d", &n);
string title, author, key_words, publisher, year;
for (int i = 0; i < n; i++) {
int id;
scanf("%d", &id); getchar();
getline(cin, title);
mpTitle[title].insert(id); //建立书名和Id之间的映射
getline(cin, author);
mpAutor[author].insert(id);
while(cin >> key_words) {
mpKey[key_words].insert(id);
char ch = getchar(); //接受key_words之后的字符
if (ch == '\n') break;
}
getline(cin, publisher);
mpPublisher[publisher].insert(id);
getline(cin, year);
mpYear[year].insert(id);
}
int m; scanf("%d", &m);
while(m--) {
int id; string op;
scanf("%d: ", &id);
getline(cin, op);
cout << id << ": " << op << endl;
if (id == 1) query(mpTitle, op);
else if (id == 2) query(mpAutor, op);
else if (id == 3) query(mpKey, op);
else if (id == 4) query(mpPublisher, op);
else query(mpYear, op);
}
return 0;
}

1022 Digital Library——PAT甲级真题的更多相关文章

  1. PAT 甲级真题题解(1-62)

    准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format  模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...

  2. PAT 甲级真题

    1019. General Palindromic Number 题意:求数N在b进制下其序列是否为回文串,并输出其在b进制下的表示. 思路:模拟N在2进制下的表示求法,“除b倒取余”,之后判断是否回 ...

  3. 1080 Graduate Admission——PAT甲级真题

    1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...

  4. PAT甲级真题及训练集

    正好这个"水水"的C4来了 先把甲级刷完吧.(开玩笑-2017.3.26) 这是一套"伪题解". wacao 刚才登出账号测试一下代码链接,原来是看不到..有空 ...

  5. PAT 甲级真题题解(63-120)

    2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...

  6. PAT甲级真题 A1025 PAT Ranking

    题目概述:Programming Ability Test (PAT) is organized by the College of Computer Science and Technology o ...

  7. Count PAT's (25) PAT甲级真题

    题目分析: 由于本题字符串长度有10^5所以直接暴力是不可取的,猜测最后的算法应该是先预处理一下再走一层循环就能得到答案,所以本题的关键就在于这个预处理的过程,由于本题字符串匹配的内容的固定的PAT, ...

  8. 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs

    前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...

  9. PAT甲级真题打卡:1001.A+B Format

    题目: Calculate a + b and output the sum in standard format -- that is, the digits must be separated i ...

随机推荐

  1. dedecms后台更新网站栏目无反应的解决方法

    dedecms进行第二次模板开发后,遇到在栏目更新的时候没有反应,但是用回原来的初始模板就可以,百度查找了很多的教程也无法进行解决,就这样慢慢的摸索.终于找到了问题的所在,原因可能是该更新的时候无法获 ...

  2. python里正则表达式基础及注意事项

    感觉正则匹配是一件很酷的事,用得好的话可以极大地提高编程效率.虽然在html中BeautifulSoup更好用一些,但有时候还是需要使用正则匹配.所以就此做一些学习和使用过程中的笔记. python有 ...

  3. 全网最硬核 JVM TLAB 分析(单篇版不包含额外加菜)

    今天,又是干货满满的一天.这是全网最硬核 JVM 系列的开篇,首先从 TLAB 开始.由于文章很长,每个人阅读习惯不同,所以特此拆成单篇版和多篇版 全网最硬核 JVM TLAB 分析(单篇版不包含额外 ...

  4. 若依管理系统RuoYi-Vue(一):项目启动和菜单创建

    若依管理系统应该是国内最受欢迎的完全开源的后端管理系统了吧,看看gitee上的star数量,着实惊人.若依系统有很多个版本 版本 gitee地址 说明 前后端不分离版本 https://gitee.c ...

  5. 设计模式(十)——组合模式(HashMap源码解析)

    1 看一个学校院系展示需求 编写程序展示一个学校院系结构:需求是这样,要在一个页面中展示出学校的院系组成,一个学校有多个学院, 一个学院有多个系.如图: 2 传统方案解决学校院系展示 3 传统方案解决 ...

  6. poj3585 Accumulation Degree(树形dp,换根)

    题意: 给你一棵n个顶点的树,有n-1条边,每一条边有一个容量z,表示x点到y点最多能通过z容量的水. 你可以任意选择一个点,然后从这个点倒水,然后水会经过一些边流到叶节点从而流出.问你最多你能倒多少 ...

  7. JVM ZeroTLAB 是什么意思呢?

    ZeroTLAB 是 JVM 的一个布尔型 Flag,意思是是否将新创建的 TLAB 内的所有字节归零. 默认:false 举例:-XX:+ZeroTLAB 当分配出来 TLAB 之后,根据 Zero ...

  8. spark mllib als 参数

    在一定范围内按照排列组合方式对rank,iterations,lambda进行交叉评估(根据均方根误差),找到最小误差的组合,用于建立矩阵分解模型.Signature: ALS.train( rati ...

  9. 图解算法——KMP算法

    KMP算法 解决的是包,含问题. Str1中是否包含str2,如果包含,则返回子串开始位置.否则返回-1. 示例1: Str1:abcd123def Str2:123d 暴力法: 从str1的第一个字 ...

  10. codeforce 849A

    A. Odds and Ends time limit per test 1 second memory limit per test 256 megabytes input standard inp ...