[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 ...
随机推荐
- 如何在git搭建自己博客
1.安装Node.js和配置好Node.js环境,打开cmd命令行输入:node v.2.安装Git和配置好Git环境,打开cmd命令行输入:git --version.3.Github账户注册和新建 ...
- 申请Let’s Encrypt通配符HTTPS证书(certbot ACME v2版)
1.获取certbot-auto# 下载 # 下载 wget https://dl.eff.org/certbot-auto # 设为可执行权限 chmod a+x certbot-auto 2.开始 ...
- 论文《Entity Linking with Effective Acronym Expansion, Instance Selection and Topic Modeling》
Entity Linking with Effective Acronym Expansion, Instance Selection and Topic Modeling 一.主要贡献 1. pro ...
- 向C++之父Bjarne Stroustrup致敬
2013-04-25 21:30 (分类:社会人生) 非常好的文章 C ++ 的 背 影 ——C++之父Bjarne Strou ...
- jmeter与jdk的安装
1.第一步:下载jdk的安装包 下载链接: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151 ...
- C#的静态工厂方法与构造函数对比
最近,在与同事进行协同编程时,我们开始讨论在C#中初始化新对象的最佳方法.我一直是使用构造函数实现,尽管他倾向于静态工程方法.这引起了关于每种类型的利弊的大量来来回回的讨论. 为了说明我所说的内容,这 ...
- width、height为auto或者100%的区别
一.规则 1. 某div不设置宽度,那么width默认为auto. 2. 某子元素div的width为100%(或者设置为等于父元素宽度的具体值,比如父元素width为100px,子元素width也设 ...
- Go语言基础之接口(面向对象编程下)
1 接口 1.1 接口介绍 接口(interface)是Go语言中核心部分,Go语言提供面向接口编程,那么接口是什么? 现实生活中,有许多接口的例子,比如说电子设备上的充电接口,这个充电接口能干什么, ...
- 进阶之路 | 奇妙的View之旅
前言 本文已经收录到我的Github个人博客,欢迎大佬们光临寒舍: 我的GIthub博客 学习清单: View是什么 View的位置参数 View的触控 View的滑动 涉及以下各个知识点: View ...
- Linux运维--15.OpenStack vm使用keepalived 实现负载均衡
外接mariadb集群 实现负载均衡 实验环境 10.0.1.27 galera1 10.0.1.6 galera2 10.0.1.23 galera3 10.0.1.17 harpoxy1 hapr ...