PAT 1022 Digital Library (30分)从踩坑到满分

PAT甲级 1022 Digital Library 【代码】PAT 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 (≤10​4 ) 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

题目解读

给你n本书的信息,每本书都包括id,title,author,keywords,publisher,publishyear六部分信息,然后有m次查询,每次查询要求输出所有满足这个查询条件的书的id,如果有多个,按照id大小顺序输出,如果一个都没有,输出"Not found",查询可以是根据title,author,keyword,publisher,publishyear这五种条件任何一种查询。

题解及注意事项

这个题其实不难,只是有好几处细节需要注意,我们先来看思路:

  • 首先,每一类查询条件都有可能得到多个id,所以肯定要有一个map<k, v>吧,k就是输入的查询条件字段,比如输入的title,author什么的,至于v肯定是保存多本书。

  • 其次,我需不需要创建一个数据结构来保存每本书的信息。当然不用,我们每种查询条件下只需要输出书的id就可以了,所以map集合的V只需要保存多个id。那采用什么数据结构呢?当然是set,因为set既能存储多个,又能自动去重,还能自动排序,多么完美的适合这个题目。所以我们用的是map<string, set<int>>。

  • 因为有五类查询条件,所以我创建五个引射表 map<string, set<int>> titlemap, authormap, keywordmap, publishermap, publishyearmap;然后在录入每本书的信息时,将书的id插入到对应的每个map中,

		// 以录入一本书信息为例
        // cin >> id; 
        scanf("%d\n", &id);
        // 这些信息可能中间包含空格,所以需要读整行
        getline(cin, title);
        // 在对应的映射集中加入这本书id
        titlemap[title].insert(id);
        getline(cin, author);
        // 在对应的映射集中加入这本书id
        authormap[author].insert(id);
        // 关键字与之不同,一本书有多个关键字,空格分隔
        while (cin >> keyword) {
            keywordmap[keyword].insert(id);
            char ch = getchar();
            // 读完本行最后一个关键字
            if (ch == '\n') break;
        }
        // 读出版社
        getline(cin, publisher);
        // 在对应的映射集中加入这本书id
        publishermap[publisher].insert(id);
        // 读出版年
        getline(cin, publishyear);
        // 在对应的映射集中加入这本书id
        publishyearmap[publishyear].insert(id);
  • 查询的时候,我们只需要根据输入的查询类别和参数,去对应的映射集找相应的键值就可以了。
踩坑1,读一整行

看到我上面代码中的读取id字段了吧,为什么要注释cin>>id,而用scanf("%d,\n", &id),因为id下面是title,而title中可能包含空格,如 hello c++ 这种,scanf和cin在遇到空格时都会结束,所以我们要用getLine()来读取title,author,publisher等信息,但是我读完id才能读title,我如果用cin读id,它遇到行末\n就结束了,不会自己换行,接着用getline()去读title,就只能读进去一个这一行剩下的\n。肯定出错。所以这里要用scanf把那个\n读走,换到下一行。

踩坑2,读 多个关键词

上面说了,读title,author,publisher,publishyear要读取整行(publishyear其实是个整数,我们是为了操作统一把它读成字符串,不然五种查询条件,四种参数是字符串,一种是整数,我岂不是要写两个函数),但是一个文章的多个关键字在同一行,用空格分隔,这要怎么读?读取一行,再字符串分隔吗?c++好像没有现成的split()函数,所以我们可以这样读取

		// 关键字与之不同,一本书有多个关键字,空格分隔
        while (cin >> keyword) {
            keywordmap[keyword].insert(id);
            // 在对应的映射集中加入这本书id
            char ch = getchar();
            // 读完本行最后一个关键字
            if (ch == '\n') break;
        }

cin遇到空格或\n结束,刚好一次读到一个单词,然后最后一个单词的末尾是换行符。

踩坑3,查询时输入数据的空格

请看题目给出的查询时的输入,6表示有6次查询,每个查询,数字代表是按照那个字段查询,然后它后面有一个空格。千万记得把这个空格读走,剩下的才是参数,而且参数是一个长的字符串,中间可能包含空格,还是要用getLine()。

6
1: The Testing Book
2: Yue Chen
3: keywords
4: ZUCS Print
5: 2011
3: blablabla
踩坑4,函数传参不加&造成超时

其实这个不算坑了,就是一个细节问题,我们知道如果是值传递的时候会有一个值拷贝的过程,当这个参数特别大(占用空间多)的时候,这个拷贝过程会浪费空间和时间,而c++的引用就很好的避免了这个问题,所以记得加&

// kvmap是键值对映射集,key是键,也就是输入的参数
void query(map<string, set<int>> &kvmap, string &key) {

完整代码

注意事项都说完了,就贴一下我的代码吧,注释日常详细!!!

#include <iostream>
#include <map>
#include <set>
using namespace std;

// 为每个指标建立一对多映射集合
// 因为既要既可能包含多个,又要求找到多个多个时按bookid递增输出,那么用set就好了,既能去重,又能自动排序
map<string, set<int>> titlemap, authormap, keywordmap, publishermap, publishyearmap;

// kvmap是键值对映射集,key是键,也就是输入的参数
void query(map<string, set<int>> &kvmap, string &key) {
    // 这个键是否有值
    auto res = kvmap.find(key);
    // 有值,
    if (res != kvmap.end()) {
        // 注意输出为7位id
        for (auto id : kvmap[key])
            printf("%07d\n", id);
    // 无值
    } else {
        cout << "Not Found" << endl;
    }
}

int main() {

    // n本书
    int n;
    cin >> n;
    int id; // 当前书id
    // 当前书 标题、作者、关键词、出版社、出版年
    string title, author, keyword, publisher, publishyear;
    while (n-- > 0) {
        // cin >> id;
        scanf("%d\n", &id);
        // 这些信息可能中间包含空格,所以需要读整行
        getline(cin, title);
        // 在对应的映射集中加入这本书
        titlemap[title].insert(id);
        getline(cin, author);
        authormap[author].insert(id);
        // 关键字与之不同,一本书有多个关键字,空格分隔
        while (cin >> keyword) {
            keywordmap[keyword].insert(id);
            char ch = getchar();
            // 读完本行最后一个关键字
            if (ch == '\n') break;
        }
        // 读出版社
        getline(cin, publisher);
        publishermap[publisher].insert(id);
        getline(cin, publishyear);
        publishyearmap[publishyear].insert(id);
    }
    int m;
    // m次查询
    cin >> m;
    // 查询格式 num: xxxxx xxxx
    int num; // 查询类别
    // 查询参数
    string temp;
    while (m-- > 0) {
        scanf("%d: ", &num);
        getline(cin, temp);
        cout << num << ": " << temp << endl;
        // 根据标题查
        if (num == 1) query(titlemap, temp);
        // 根据作者查
        else if (num == 2) query(authormap, temp);
        // 根据关键字查
        else if (num == 3) query(keywordmap, temp);
        // 根据出版社查
        else if (num == 4) query(publishermap, temp);
        // 根据出版年份查
        else if (num == 5) query(publishyearmap, temp);
    }

    return 0;

}
PAT甲级真题 1022 Digital Library (30分) C++实现(建立结构体数组,注意测试点3、4的坑) 题目 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 numb... 阅读详情

相关推荐

【PAT甲级】1022 Digital Library (30 分)测试点1、3、4

【PAT甲级】1022 Digital Library (30 分)测试点1、3、4

D_D_L_L_的博客 752

PAT 1022 Digital Library(测试点3,4是输出格式问题)

#include <iostream> #include <string> #include <map> #include <set> using namespace std; void Find(map<string, set<int>>&list, string &x) { if (list.find(x) == list.end()) cout << "Not Found" << en

qq_40737596的博客 537

思博伦Spirent TestCenter创建单播流uni-stream

Traffic Generator列表中会出现刚才创建的uni-stream,创建完成。通常测试情况下,主要对Ethenet II和IPv4 Header进行配置。(通常情况下,如果流配置比较规范,选择任意端口接收,也没问题。编辑uni-stream基本信息。配置本条流由STC哪个端口接收。

辗转反测的博客 1683

PAT甲级 - 1022 Digital Library (30 分)

题目链接:PAT甲级 - 1022 Digital Library (30 分) A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each b...

284

滑模控制理论(SMC)概述

滑模控制理论(SMC)概述一、背景二、数学理论三、高维拓展四、分析 一、背景 滑模控制理论(Sliding Mode Control, SMC)是一种建立在现代控制理论基础上的控制理论,主要数学核心为李雅普诺夫函数。 滑模控制的核心思想是建立一个滑模面,将被控系统拉取到滑模面上来,使系统沿着滑模面运动。滑模控制的一个优势是无视外部扰动和不确定参数,采用一种比较“暴力”的方式达到控制的目的。 其思想和反步法有些类似,但数学实现起来更加方便而不抽象。相比反步法,滑模控制没有复杂的数学计算,也不会出现“微分爆炸”

高能阿博特的博客 6万+

”COMSOL“竟然如此简单!我悟了!!

COMSOL Multiphysics具有强大的多物理场全耦合仿真分析功能、高效的计算性能,可以保证数值仿真的高度精确,已被广泛应用于各个学科领域,近年来运用COMSOL来解决电化学实际工程问题也越来越普遍。

C1012562281的博客 3448

PAT习题解:1022. Digital Library (30)

题目链接:http://www.patest.cn/contests/pat-a-practise/1022 这道题目我没想到什么好办法做,最后参考了闲云阁的文章,用map 和 set 做出来,算是顺便学习了一下 STL 里面 map 和 set 的用法。话说C++的输入输出真是麻烦啊,这题用Python做的话写起来会很简单吧。 #define _CRT_SECURE_NO_WARNING

by21010的专栏 760

R数据分析:潜在转化分析LTA的做法和解释(一)

之前给大家写了很多潜在类别分析的教程Mplus教程:如何做潜在类别分析LCAR数据分析:用R语言做潜类别分析LCAMplus数据分析:潜在类别分析(LCA)流程(详细版)R数据分析:再写潜在类别分析LCA的做法与解释,今天继续给大家拓展一步。 今天要介绍的就是潜在转换分析,这个东西就是LCA的纵向版本。是一个专门用来研究质变的统计技巧。有一句话叫做量变起质变,你怎么知道质变到底发生没有?就用潜在转换分析。 Latent transition analysis is an extension of...

Codewar的博客 7703

PAT甲级 1022 Digital Library (30分)

1022 Digital Library (30分) 题目链接:PAT A 1022 题目大意:有一数字图书馆,对于每一本书给出如下信息:书的id,书的名字,书的作者,书的关键字,书的出版社以及书的出版年份。要求根据输入的查找信息输出相应书的ID(7位数字)。 思路分析:这道题题意很直观,但是需要用到的知识点比较多。首先根据题目要求可以分析出这道题一定是使用STL中的map去解决,而且由于题...

weixin_43684054的博客 217

Oracle EBS(R12+)通过Multi-Org 多组织架构、Multi-Ledger 多账簿、SLA 子分类账会计、多币种引擎、多会计政策并行五大核心能力,为跨国企业提供一体化、自动化、合规的

Oracle EBS(R12+)提供全球会计核算解决方案,通过Multi-Org多组织架构实现跨国集团数据隔离与共享,支持四层组织结构(业务组→法律实体→经营单位→库存组织)。Multi-Ledger多账簿功能允许一笔业务多准则自动核算,包含主账簿、辅助账簿和报告币种。系统内置多币种处理引擎,覆盖全流程货币业务,支持自动重估、再计量和折算。通过SLA子分类账会计实现多会计政策并行处理,满足不同国家会计准则和税制要求。该方案可同时支持本地法定、集团合并及管理分析需求,实现全球财务数据集中管理,提升合规性和运营

金牌架构师 553

基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的水果识别系统(Python+PySide6界面+训练代码)

本文详细介绍了基于YOLO系列目标检测算法(包括YOLOv5、YOLOv6、YOLOv7和YOLOv8)的水果识别系统的设计与实现。系统采用Python语言开发,结合PySide6构建用户界面,实现了从数据准备、模型训练到部署应用的全流程。本文将提供完整的代码实现、详细的训练过程分析以及性能对比实验,并给出推荐的数据集资源。该系统可以准确识别多种水果类型,为智能农业、自动售货、食品加工等领域提供技术支持。

YOLO 565

PAT 甲级 1022(Digital Library)

PAT 甲级 1022题目要求翻译代码思路 题目要求 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 fro

qq_42865772的博客 306

YOLOv8【第十一章:视频追踪与流处理篇·第3节】匈牙利算法(Hungarian Algorithm)与 KM 算法匹配机制详解!

🏆 本文收录于 《YOLOv8实战:从入门到深度优化》 专栏。该专栏系统复现并梳理全网各类 YOLOv8 改进与实战案例(当前已覆盖分类 / 检测 / 分割 / 追踪 / 关键点 / OBB 检测等方向),坚持持续更新 + 深度解析,质量分长期稳定在 97 分以上,可视为当前市面上 覆盖较全、更新较快、实战导向极强 的 YOLO 改进系列内容之一。

**My Coding Family** 445

[C++] PAT 1022 Digital Library (30分)

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 Tes.

快乐de约定的博客 2211

vue支付项目-APP支付宝支付功能

如果用户已安装支付宝客户端,商家 App 唤起支付宝完成支付,支付完后跳回到商家 App 内,最后展示支付结果。如果用户没有安装支付宝客户端,商家 App 内会调起支付宝网页支付收银台,用户登录支付宝账号,支付完后展示支付结果。1.点击按钮开通或者续费,会验证是否选择会员商品以及对协议是否查看选中(这里根据项目需要自行验证,可忽略),验证通过打开选择支付方式弹窗页面,选择微信支付或者支付宝支付;4.可取消支付,放弃支付会返回会员页面,页面提示支付取消;1.判断支付方式,如果是2,则是支付宝支付方式。

总结前端开发中遇到的问题,分享前端知识 1万+

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

PAT 1022 Digital Library

原文链接:欢迎大家访问我的个人博客 原题链接   PAT 1022 Digital Library (30分) 考点   数据输入 map 思路   我想大家拿到题目首先想到的方法肯定是用结构体来进行数据的存储,在根据查询的条件进行筛选输出。但是我在这篇博客中获得了新的思路。   使用map<string,set<int> >将每一个查询的条件与其相对应的集合...

我是没有梦想的小白 132

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Day47: [PAT甲级] 1022 Digital Library (30分)

Day47: [PAT甲级] 1022 Digital Library (30分) 题源: 来自PAT真题库: https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336 代码: dirty code凑合看吧 #include<iostream> #include<string&gt...

qq2215459786的博客 207

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

1022 Digital Library (30 分)-PAT甲级

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 suppose

神笔码农.的博客 535

PAT 甲级 1022  Digital Library

1022 Digital Library (30 point(s)) 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...

漫浸天空的雨色的备忘录~ 704

PAT A 1022 Digital Library 题目 答案

1022 Digital Library (30 分) 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 assigne...

qq_30633489的博客 268

【PAT甲级】1022 Digital Library

题目链接:1022Digital Library #include <iostream> #include <unordered_map> #include <vector>//换成set可能更好 #include <algorithm> using namespace std; class solution{ public: unordered_map<string,vector<string>> mvi[5];...

五百零一 122

【PAT 甲级】1022 Digital Library

此题目没有难点,主要有很多使用错误问题,需要注意易错点

AmiaYukiAchami的博客 745

PAT 1022 Digital Library (30 分)

1022 Digital Library (30 分) 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 assigne...

peanWang的博客 226

1022 Digital Library (30 分)

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 a...

weixin_42582136的博客 483

PAT甲级 1022 Digital Library (30 分) 题解

\quad建立5个map,用于书名,作者,关键词,出版社,出版日期与书编号之间的映射,然后输出即可。 Tips:获得一句字符串后,比如“test code debug sort keywords”,如何将其按空格分成若干个单词,借用sstream头文件里面的istringstream可以很方便的完成。 #include <sstream> string s, temp; vector...

qq_40438165的博客 1569
上一篇: PAT 1013 Battle Over Cities (25分) 图的连通分量+DFS
下一篇: CCF认证试题 2017-12-02游戏——java实现
wwang_dev
博客等级 码龄1天 27粉丝 76原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值