【题目大意】

给出几本书的信息,包括编号,名字,出版社,作者,出版年份,关键字;然后给出几个请求,分别按照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. Java 添加OLE对象到Excel文档

    本文介绍通过Java程序添加OLE对象到Excel文档.OLE分为两种形式,一种通过嵌入(Embed),方式,一种通过链接(Link)方式.前者是将对象嵌入到文档中,外部对该对象的更改不影响嵌入操作时 ...

  2. ajax 解决中文乱码问题

    最近遇到了ajax 中文乱码的问题.下面总结一下 1. HTTP协议的编码规定 在HTTP协议中,浏览器不能向服务器直接传递某些特殊字符,必须是这些字符进行URL编码后再进行传送.url编码遵循的规则 ...

  3. Apache 相关 mod_rewrite ,RewriteCond,{HTTP_HOST}

    1.给子域名加www标记  RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC] RewriteCond %{HTTP_HOST} !^www\ ...

  4. javascript console对象 常用的方法

    console对象 var o = {name:'3'} console.assert(o.name === '3', "name 的值应该为:string 3"); consol ...

  5. html input元素的所有type属性

    <input /> 属性 type="text" 输入框的类型为文本 type="password" 输入框的类型为密码 type="ra ...

  6. pinpoint 修改hbase表TTL值

    操作步骤 查找出数据大的hbase表 root@990fb5560f64:/opt/hbase/hbase-# ls CHANGES.txt LICENSE.txt README.txt conf h ...

  7. 为什么你精通CRUD,却搞不懂数据库的基本原理?

    原创声明 本文作者:黄小斜 转载请务必在文章开头注明出处和作者. 本文思维导图

  8. JDK1.8_HashMap源码__构造函数

    HashMap的底层实现是一个Node类型的数组,也就是说使用put(key, value)方法的时候就把key和value根据hashcode值存在table数组相应的下标中,源码如下: /** * ...

  9. Nginx三大主要功能

    1.做静态资源服务器,可以用于前端项目发布,图片文件文件等静态服务器. 2.做反向代理服务器,域名往往配置在Nginx上,真正的业务服务器躲在其身后. 3.做负载均衡服务器,作为负载集群的入口网关. ...

  10. Android中使用getDrawable时提示:Call requires API level 21(current min is 15)

    场景 在通过getDrawable方法获取照片资源时提示: Call requires API level 21(current min is 15) 注: 博客: https://blog.csdn ...