PAT甲级——A1022 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 (≤) which is the total number of books. Then Nblocks 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 (≤) 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
第一种方法
以时间换空间,就是个ID保留一组信息,查询时遍历查询
但可能导致时间复杂度过大
第二种方法[推荐使用方法二]
以空间换时间,每种信息对应一个ID,查找时,时间复杂度为O(1)
但可能导致空间复杂度太大
注意一些字符输入的细节
#include <iostream>
#include <map>
#include <unordered_map>
#include <set>
#include <string>
using namespace std; 方法一:
struct node
{
string name, author, keywords, publisher, year;
};
int main()
{
int N, M;
string ID;
cin >> N;
map<string, node>data;
for (int i = ; i < N; ++i)
{
node book;
cin >> ID;
getchar();//去除回车键
getline(cin, book.name);
getline(cin, book.author);
getline(cin, book.keywords);
getline(cin, book.publisher);
cin >> book.year;
data[ID] = book;
}
cin >> M;
getchar();//去除回车键
for (int i = ; i < M; ++i)
{
string str;
bool is = true;
getline(cin, str);
cout << str << endl;
int index = str[] - '';
str.assign(str.begin() + , str.end());
for (auto ptr = data.begin(); ptr != data.end(); ++ptr)
{
switch (index)
{
case :
if (ptr->second.name == str)
{
is = false;
cout << ptr->first << endl;
}
break;
case :
if (ptr->second.author== str)
{
is = false;
cout << ptr->first << endl;
}
break;
case :
if (ptr->second.keywords.find(str) !=-)
{
is = false;
cout << ptr->first << endl;
}
break;
case :
if (ptr->second.publisher == str)
{
is = false;
cout << ptr->first << endl;
}
break;
case :
if (ptr->second.year == str)
{
is = false;
cout << ptr->first << endl;
}
break;
default:
break;
}
}
if (is)
cout << "Not Found" << endl;
}
return ;
} //方法二
void findInfo(unordered_map<string, set<int>>&data,string &str)//传参一定要用引用,否则最后一组数据可能会超时
{
if(data.find(str)==data.end())
printf("Not Found\n");
else
{
for (auto ptr = data.find(str)->second.begin(); ptr != data.find(str)->second.end(); ++ptr)
printf("%07d\n", *ptr);
}
}
int main()
{
int N, M, ID;
scanf("%d", &N);
string til, aut, keys, pub, yea;
unordered_map<string, set<int>>title, author, keywords, publisher, year;//因为key不唯一
for (int i = ; i < N; ++i)
{
scanf("%d\n", &ID);//不用清除回车键
getline(cin, til);
title[til].insert(ID);
getline(cin, aut);
author[aut].insert(ID);
while (cin >> keys)
{
keywords[keys].insert(ID);
char c = getchar();
if (c == '\n')break;
}
getline(cin, pub);
publisher[pub].insert(ID);
getline(cin, yea);
year[yea].insert(ID);
}
scanf("%d\n", &M);
for (int i = ; i < M; ++i)
{
string str;
getline(cin, str);
cout << str << endl;
int index = str[] - '';
str.assign(str.begin() + , str.end());
if (index == ) findInfo(title, str);
else if (index == ) findInfo(author, str);
else if (index == ) findInfo(keywords, str);
else if (index == ) findInfo(publisher, str);
else if (index == ) findInfo(year, str);
else printf("Not Found\n");
}
return ;
}
PAT甲级——A1022 Digital Library的更多相关文章
- 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 甲级 1022 Digital Library (30 分)(字符串读入getline,istringstream,测试点2时间坑点)
1022 Digital Library (30 分) A Digital Library contains millions of books, stored according to thei ...
- PAT甲级1022 Digital Library
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336 题意: 每一本书有一个id, 书名,作 ...
- A1022. Digital Library
A Digital Library contains millions of books, stored according to their titles, authors, key words o ...
- PAT Advanced 1022 Digital Library (30 分)
A Digital Library contains millions of books, stored according to their titles, authors, key words o ...
- [PAT] A1022 Digital Library
[题目大意] 给出几本书的信息,包括编号,名字,出版社,作者,出版年份,关键字:然后给出几个请求,分别按照1->名字,2->出版社等对应信息查询符合要求的书的编号. [思路] 模拟. [坑 ...
- PAT A 1022. Digital Library (30)【结构体排序检索】
https://www.patest.cn/contests/pat-a-practise/1022 直接模拟, 输入,按id排序,检索 #include <iostream> #incl ...
- 【算法笔记】A1022 Digital Library
题意 输入n本书的信息:id,书名,作者,关键字,出版社,出版年份.搜索图书,输出id. 思路 定义5个map<string, set<int> >,分别存放Title, Au ...
随机推荐
- ImageMagick convert多张照片JPG转成pdf格式,pdfunite合并PDF文件
在认识ImageMagick之前,我***的图像浏览软件是KuickShow,截图软件是KSnapShot,这两款软件都是KDE附带的软件,用起来也是蛮方便的.在一次偶然的机会中,我遇到了Imag ...
- EJB(Enterprise JavaBean)科普
该文章是引用的,主要用于自己的学习,然后是记载免得忘记的时候到处乱找.结尾有引用地址. 到底EJB是什么?被口口相传的神神秘秘的,百度一番,总觉得没有讲清楚的,仍觉得一头雾水.百度了很久,也从网络的文 ...
- Python入门教程 超详细1小时学会Python
Python入门教程 超详细1小时学会Python 本文适合有经验的程序员尽快进入Python世界.特别地,如果你掌握Java和Javascript,不用1小时你就可以用Python快速流畅地写有用的 ...
- day22_2-sys模块
# ********************day22_2-sys模块 *******************# ********************day22_2-sys模块 ********* ...
- day 86 Vue学习之八geetest滑动验证
Vue学习之八geetest滑动验证 本节目录 一 geetest前端web中使用 二 xxx 三 xxx 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 geetest前端web ...
- 深夜Python - 第2夜 - 爬行
深夜Python - 第2夜 - 爬行 我曾经幻想自己是一只蜗牛,有自己的一只小壳,不怕风,不怕雨,浪荡江湖,游历四方……夜猫兄一如既往地打断了我不切实际的幻想:“浪荡?游历?等你退休了都爬不出家门口 ...
- neo4j中cypher语句多个模糊查询
总结一下经验: neo4j中,cypher语句的模糊查询,好像是个正则表达式结构. 对于一个属性的多个模糊查询,可以使用如下写法: 比如,查询N类型中,属性attr包含'a1'或者'a2'的所有节点. ...
- LeetCode 19.删除链表的倒数第N个节点(Python)
题目: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点 ...
- LUOGU P1505 [国家集训队]旅游 (树链剖分+线段树)
传送门 解题思路 快被调死的码农题,,,其实就是一个边权下放到点权的线段树+树剖. #include<iostream> #include<cstdio> #include&l ...
- System.Web.Mvc.FileStreamResult.cs
ylbtech-System.Web.Mvc.FileStreamResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, P ...