【题目大意】

给出几本书的信息,包括编号,名字,出版社,作者,出版年份,关键字;然后给出几个请求,分别按照1->名字,2->出版社等对应信息查询符合要求的书的编号。

【思路】

模拟。

【坑】

1) 根据空格判断关键字key,遇到空格存下前一个key,则到最后一个key没有保存,退出while循环后要再存一次。

2) 漏根据id排序

3) Not Found 拼写错了...

4) 题目说年份在[1000, 3000],但是并不是这样的!输出年份一定要保留4位高位填充0补齐,否则测试点1过不了。这个地方我调试了好久,气死人了!

【tips】

1) 输入带空格的string

方法一:

  string s;

  getline(cin, s);

方法二:

  string s;

  char c;

  while((c=cin.get())!='\n')

    s = s + c;

2) 题目中根据id排序,我的处理方法是用集合set存储符合查询条件的id,循环结束后一次输出set里的id值即可,因为set会自动帮你排序。(时间3ms;4ms;4ms;4ms;280ms)

也可以先调用sort排序,后续依次比对输出的时候自然就是按顺序的了,这样会慢一些。(时间5ms;5ms;5ms;5ms;393ms)

3) 网上有人说用map方便,或许今后可以试试?

【AC代码】

 #define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<queue>
#include<vector>
#include<set>
#include<string>
#include <algorithm>
using namespace std;
#define N 10002
struct book {
string id;
string title, author;
vector<string> key;
string publisher;
int year;
};
vector<book> info;
int main()
{
int n;
cin >> n;
int i;
for (i = ; i < n; i++)
{
book tbook;
cin >> tbook.id;
char c;
c = cin.get();
getline(cin,tbook.title);
getline(cin, tbook.author);
string str; while (cin >> str) {
tbook.key.push_back(str);
if (getchar() == '\n')
break;
}
/*
while ((c = cin.get()) != '\n')
{
if (c == ' '){
tbook.key.push_back(str);
str.clear();
}
else{
str += c;
}
}
tbook.key.push_back(str);
*/
getline(cin, tbook.publisher);
cin >> tbook.year;
info.push_back(tbook);
}
int m, op;
cin >> m;
for (i = ; i < m; i++)
{
scanf("%d: ", &op);
if (op == ) {
set<string>ans_id;
string ttitle;
getline(cin, ttitle);
cout << "1: " << ttitle << endl;
int j;
bool find = false;
for (j = ; j < info.size(); j++)
if (ttitle == info[j].title)
{
find = true;
ans_id.insert(info[j].id);
//cout << info[j].id << endl;
}
if (!find)
cout << "Not Found" << endl;
else {
set<string>::iterator it = ans_id.begin();
for (; it != ans_id.end(); it++)
cout << *it << endl;
}
}
if (op == ) {
set<string>ans_id;
string tauthor;
getline(cin, tauthor);
cout << "2: " << tauthor << endl;
int j;
bool find = false;
for (j = ; j < info.size(); j++)
if (tauthor == info[j].author)
{
find = true;
ans_id.insert(info[j].id);
//cout << info[j].id << endl;
}
if (!find)
cout << "Not Found" << endl;
else {
set<string>::iterator it = ans_id.begin();
for (; it != ans_id.end(); it++)
cout << *it << endl;
}
}
if (op == ) {
set<string>ans_id;
string tkey;
getline(cin, tkey);
cout << "3: " << tkey << endl;
int j;
bool find = false;
for (j = ; j < info.size(); j++)
for (int k = ; k < info[j].key.size(); k++)
{
if (tkey == info[j].key[k])
{
find = true;
ans_id.insert(info[j].id);
//cout << info[j].id << endl;
break;
}
}
if (!find)
cout << "Not Found" << endl;
else {
set<string>::iterator it = ans_id.begin();
for (; it != ans_id.end(); it++)
cout << *it << endl;
}
}
if (op == ) {
set<string>ans_id;
string tpub;
getline(cin, tpub);
cout << "4: " << tpub << endl;
int j;
bool find = false;
for (j = ; j < info.size(); j++)
if (tpub == info[j].publisher)
{
find = true;
ans_id.insert(info[j].id);
//cout << info[j].id << endl;
}
if (!find)
cout << "Not Found" << endl;
else {
set<string>::iterator it = ans_id.begin();
for (; it != ans_id.end(); it++)
cout << *it << endl;
}
}
if (op == ) {
set<string>ans_id;
int tyear;
cin >> tyear;
//cout << "5: " << tyear << endl;
printf("5: %04d\n", tyear);
int j;
bool find = false;
for (j = ; j < info.size(); j++)
if (tyear == info[j].year)
{
find = true;
ans_id.insert(info[j].id);
//cout << info[j].id << endl;
}
if (!find)
cout << "Not Found" << endl;
else {
set<string>::iterator it = ans_id.begin();
for (; it != ans_id.end(); it++)
cout << *it << endl;
}
}
}
/*for (i = 0; i < info.size(); i++)
{
cout << info[i].id << endl;
cout << info[i].title << endl;
cout << info[i].author << endl;
for (int j = 0; j < info[i].key.size(); j++)
cout << info[i].key[j] << endl;
cout << endl;
cout << info[i].publisher << endl;
cout << info[i].year << endl;
}*/
return ;
}

[PAT] A1022 Digital Library的更多相关文章

  1. PAT甲级——A1022 Digital Library

    A Digital Library contains millions of books, stored according to their titles, authors, key words o ...

  2. A1022. Digital Library

    A Digital Library contains millions of books, stored according to their titles, authors, key words o ...

  3. PAT 1022 Digital Library[map使用]

    1022 Digital Library (30)(30 分) A Digital Library contains millions of books, stored according to th ...

  4. pat 1022 digital library

    #include <iostream> #include <sstream> #include <string> #include <vector> # ...

  5. 【算法笔记】A1022 Digital Library

    题意 输入n本书的信息:id,书名,作者,关键字,出版社,出版年份.搜索图书,输出id. 思路 定义5个map<string, set<int> >,分别存放Title, Au ...

  6. PAT 甲级 1022 Digital Library

    https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336 A Digital Library cont ...

  7. pat 甲级 1022. Digital Library (30)

    1022. Digital Library (30) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A Di ...

  8. PAT Advanced 1022 Digital Library (30 分)

    A Digital Library contains millions of books, stored according to their titles, authors, key words o ...

  9. PAT 甲级 1022 Digital Library (30 分)(字符串读入getline,istringstream,测试点2时间坑点)

    1022 Digital Library (30 分)   A Digital Library contains millions of books, stored according to thei ...

随机推荐

  1. 第2章 Java并行程序基础(一)

    2.1 有关线程你必须知道的事 进程是系统进行资源分配和调度的基本单位,是程序的基本执行实体. 线程就是轻量级进程,是程序执行的最小单位. 线程的生命周期,如图2.3所示. 线程的所有状态都在Thre ...

  2. 基于 Serverless Component 全栈解决方案

    什么是 Serverless Component Serverless Component 是 Serverless Framework 的,支持多个云资源编排和组织的场景化解决方案. Serverl ...

  3. OpenCV3入门(九)图像几何变换

    1.图像缩放 假设图像x轴的缩放因子Sx, y轴方向的缩放因子Sy,相应的变换表达式为: 函数原型为: CV_EXPORTS_W void resize( InputArray src, Output ...

  4. CVE-2020-0618 SQL 远程代码执行

    CVE-2020-0618 SQL Server远程代码执行 1.简介 SQL Server Reporting Services(SSRS)提供了一组本地工具和服务,用于创建,部署和管理移动报告和分 ...

  5. mac 经常使用的快捷键操作

    ##### touch bar 作用1: 打开项目的一些快捷操作键. 作用2: 右侧的< 打开有一些以前的常规操作. ##### 手势 #####  mac自带的一些操作 cmd + opt + ...

  6. vue路由+vue-cli实现tab切换

    第一步:搭建环境 安装vue-cli cnpm install -g vue-cli安装vue-router cnpm install -g vue-router使用vue-cli初始化项目 vue ...

  7. c语言心形告白代码实现

    c语言心形告白代码实现 1.彩色告白 include<stdio.h> include<math.h> include<windows.h> include< ...

  8. Python3标准库:functools管理函数的工具

    1. functools管理函数的工具 functools模块提供了一些工具来调整或扩展函数和其他callable对象,从而不必完全重写. 1.1 修饰符 functools模块提供的主要工具就是pa ...

  9. 06.JS对象-1

    前言: 学习一门编程语言的基本步骤(01)了解背景知识(02)搭建开发环境(03)语法规范(04)常量和变量(05)数据类型(06)数据类型转换(07)运算符(08)逻辑结构(09)函数(10)对象1 ...

  10. Jmeter后置处理器,正则表达式提取器的使用

    [使用场景]:下一个请求参数需要从上一个请求的响应数据中获取 [jmeter正则表达式说明]:使用perl正则表达式(可参考:http://www.runoob.com/perl/perl-regul ...