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. VS2010-MFC(MFC常用类:CTime类和CTimeSpan类)

    转自:http://www.jizhuomi.com/software/230.html 上一节讲了MFC常用类CString类的用法,本节继续讲另外两个MFC常用类-日期和时间类CTime类和CTi ...

  2. joda 获取每个月第一天第一秒和最后一天最后一秒

    /** * 获取每个月第一天的第一秒 * @return */ public static final Date getMouthOfFirst(){ DateTimeFormatter format ...

  3. Mysql优化系列之数据类型优化

    本篇是优化系列的第一篇:数据类型 为了不产生赘述,尽量用简洁的语言来描述. 在选择数据类型之前,首先要知道几个原则: 更小的通常更好 尽量使用可以正确存储数据的最小数据类型.更小的数据类型意味着更快, ...

  4. USACO 2006 November Gold Fence Repair /// 贪心(有意思)(优先队列) oj23940

    题目大意: 输入N ( 1 ≤ N ≤ 20,000 ) :将一块木板分为n块 每次切割木板的开销为这块木板的长度,即将长度为21的木板分为13和8,则开销为21 接下来n行描述每块木板要求的长度Li ...

  5. c# Data = select new{} 返回值的显示

  6. nput="file" 浏览时只显示指定excel文件,筛选特定文件类型

    <p>显示 .xls, .xlsx, .csv 文件...</p><input type="file" accept=".csv, appl ...

  7. 2、docker镜像管理

    Docker镜像管理 镜像是Docker容器的基础,想运行一个Docker容器就需要有镜像.我们上面已经学会了使用search搜索镜像.那么这个镜像是怎么创建的呢? 创建镜像 镜像的创建有以下几种方法 ...

  8. Pthread spinlock自旋锁

    锁机制(lock) 是多线程编程中最常用的同步机制,用来对多线程间共享的临界区(Critical Section) 进行保护. Pthreads提供了多种锁机制,常见的有:1) Mutex(互斥量): ...

  9. Easy Excel导出

    @GetMapping(value = "/down2") public void down2(HttpServletResponse response) throws Excep ...

  10. Jpgraph小应用

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...