[PAT] A1022 Digital Library
【题目大意】
给出几本书的信息,包括编号,名字,出版社,作者,出版年份,关键字;然后给出几个请求,分别按照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的更多相关文章
- PAT甲级——A1022 Digital Library
A Digital Library contains millions of books, stored according to their titles, authors, key words o ...
- A1022. Digital Library
A Digital Library contains millions of books, stored according to their titles, authors, key words o ...
- 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
#include <iostream> #include <sstream> #include <string> #include <vector> # ...
- 【算法笔记】A1022 Digital Library
题意 输入n本书的信息:id,书名,作者,关键字,出版社,出版年份.搜索图书,输出id. 思路 定义5个map<string, set<int> >,分别存放Title, Au ...
- PAT 甲级 1022 Digital Library
https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336 A Digital Library cont ...
- pat 甲级 1022. Digital Library (30)
1022. Digital Library (30) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A Di ...
- PAT Advanced 1022 Digital Library (30 分)
A Digital Library contains millions of books, stored according to their titles, authors, key words o ...
- PAT 甲级 1022 Digital Library (30 分)(字符串读入getline,istringstream,测试点2时间坑点)
1022 Digital Library (30 分) A Digital Library contains millions of books, stored according to thei ...
随机推荐
- Jmeter——使用JSR223元件实现RSA登录加密
一.RSA加密简介 RSA加密是一种非对称加密.可以在不直接传递密钥的情况下,完成解密.这能够确保信息的安全性,避免了直接传递密钥所造成的被破解的风险.是由一对密钥来进行加解密的过程,分别称为公钥和私 ...
- Python3(八) 枚举详解
一.枚举其实是一个类 建议标识名字用大写 1.枚举类: from enum import Enum class VIP(Enum): YELLOW = 1 GREEN = 2 ...
- CentOS6.5下部署SVN
查看系统版本,安装SVN软件及创建目录 [root@A-linux ~]# uname -r 2.6.32-431.el6.x86_64 [root@A-linux ~]# cat /etc/redh ...
- Admin后台权限管理、三大认证
目录 APIView的请求生命周期 三大认证规则 权限六表 自定义User表 详细配置演示 models.py setting.py admin.py 使用过程: 控制填写信息的字段 控制添加权限 控 ...
- Apache 的多站点配置
1.修改httpd.conf 文件 Apache的主配置文件路径: D:\phpTools\Apache24\conf 用编辑器打开 httpd.conf 文件,查找 #Include conf/ex ...
- Python应用——自定义排序全套方案
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天的这篇文章和大家聊聊Python当中的排序,和很多高级语言一样,Python封装了成熟的排序函数.我们只需要调用内部的sort函数,就可 ...
- React之JSX的语法细节
带注释 import React, { Component, Fragment } from 'react' import './style.css' class TodoList extends C ...
- 《ASP.NET Core应用开发入门教程》与《ASP.NET Core 应用开发项目实战》正式出版
“全书之写印,实系初稿.有时公私琐务猬集,每写一句,三搁其笔:有时兴会淋漓,走笔疾书,絮絮不休:有时意趣萧索,执笔木坐,草草而止.每写一段,自助覆阅,辄摇其首,觉有大不妥者,即贴补重书,故剪刀浆糊乃不 ...
- Linux运维----03.制作trove-mysql5.7镜像
安装mysql yum install http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm yum remove m ...
- 【算法】蓝桥杯 试题 基础练习 Huffuman树
资源限制 时间限制:1.0s 内存限制:512.0MB 问题描述 Huffman树在编码中有着广泛的应用.在这里,我们只关心Huffman树的构造过程. 给出一列数{pi}={p0, p1, …, ...