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的更多相关文章

  1. PAT 甲级 1022 Digital Library

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

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

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

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

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

  4. PAT甲级1022 Digital Library

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336 题意: 每一本书有一个id, 书名,作 ...

  5. A1022. Digital Library

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

  6. PAT Advanced 1022 Digital Library (30 分)

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

  7. [PAT] A1022 Digital Library

    [题目大意] 给出几本书的信息,包括编号,名字,出版社,作者,出版年份,关键字:然后给出几个请求,分别按照1->名字,2->出版社等对应信息查询符合要求的书的编号. [思路] 模拟. [坑 ...

  8. PAT A 1022. Digital Library (30)【结构体排序检索】

    https://www.patest.cn/contests/pat-a-practise/1022 直接模拟, 输入,按id排序,检索 #include <iostream> #incl ...

  9. 【算法笔记】A1022 Digital Library

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

随机推荐

  1. FP function programming

    fp是函数式编程,也是一种编程方法,同面向对象的编程方法一样. 所谓的functional programming 就是函数式编程,这也是一种编程方法,或者说是编程模式,同面向过程的编程,以及面向对象 ...

  2. getResourceAsStream的几种路径配置(properties文件的路径问题)

    文章目录 getResourceAsStream有以下几种: getResourceAsStream有以下几种: Class.getResourceAsStream(String path) : pa ...

  3. PAT甲级——A1113 Integer Set Partition

    Given a set of N (>) positive integers, you are supposed to partition them into two disjoint sets ...

  4. Eclipse添加git插件及操作

    注册账号新建仓库 在Github已经注册成功自己的账号 新建一个仓库 创建成功后记住url: 安装Git插件 首先像安装Pydev一样 点击help的Install New Software 点击Ad ...

  5. 【牛客Wannafly挑战赛12】小H和圣诞树

    题目 可以考虑边分治,对于某一种颜色,我们处理出分治边左右两边所有以这个颜色为端点的路径长度,之后随便拼一拼就好了 但是这样对于每一组询问都需要边分一遍,这样做复杂度是\(O(nm+n\log n)\ ...

  6. 笔记:使用Python解析JSON

    使用Python解析JSON json是一种轻量级的数据交换格式,易于阅读和编写. json函数具体作用描述 函数 具体描述作用 json.dumps 将python对象编码为JSON字符串 json ...

  7. LoadRunner参数化详解【转】

    距离上次使用loadrunner 已经有一年多的时间了.初做测试时在项目中用过,后面项目中用不到,自己把重点放在了工具之外的东西上,认为性能测试不仅仅是会用工具,最近又想有一把好的利器毕竟可以帮助自己 ...

  8. 阿里重磅开源首款自研科学计算引擎Mars,揭秘超大规模科学计算

    日前,阿里巴巴正式对外发布了分布式科学计算引擎 Mars 的开源代码地址,开发者们可以在pypi上自主下载安装,或在Github上获取源代码并参与开发. 此前,早在2018年9月的杭州云栖大会上,阿里 ...

  9. Python-网络编程之粘包、UDP

    目录 粘包问题 subprocess模块 struct模块 UDP协议编程 简易qq聊天室 粘包问题 什么是粘包问题呢? 在我们写 tcp socket编程的时候,tcp协议是一个流式的协议,服务端第 ...

  10. Jeecg-Boot前后端分离,针对敏感数据,加密传递方案

    # 针对敏感数据,加密传递方案 第一步: 在vue页面引入aesEncrypt.js encryption方法.示例代码: import { encryption } from '@/utils/en ...