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(对话框:消息对话框)

    转自:http://www.jizhuomi.com/software/171.html 前面几节讲了属性页对话框,我们可以根据所讲内容方便的建立自己的属性页对话框.本节讲解Windows系统中最常用 ...

  2. iOS开发UITableView随笔

    1.设置cell的间隔 - (void)setFrame:(CGRect)frame{ frame.size.height -=; [super setFrame:frame]; } 2.刷新row或 ...

  3. 同步+TASK异步请求

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. [SNOI2017]遗失的答案

    题目 首先\(G,L\)肯定会满足\(G|L\),否则直接全部输出\(0\) 之后我们考虑一下能用到的质因数最多只有\(8\)个 同时我们能选择的数\(x\)肯定是\(L\)的约数,还得是\(G\)的 ...

  5. Linux-c线程创建

    { pthread_attr_t attr;//线程属性 , err_sav; if (!pThreadId) { errno = EINVAL; ; } memset(&attr, , si ...

  6. JavaScript特效源码(8、其他特效)

    1.中文日期 中文日期[无须修改][共1步]] ====1.将以下代码加入HEML的<body></body>之间 <script LANGUAGE="Java ...

  7. webpack打包出错分析

    Module not found: Error: Can't resolve 'bundle.js' in 更改打包命令 webpack run1.js  -o bundle.js

  8. linux centos 恢复 还原 备份 Snapper 快照说明

    为什么要使用Snapper快照? 我们可以想像以下场景: 1. 场景一:系统发生意外宕机,工程师无法快速定位问题,业务受到中断,客户十分不满意. 2. 场景二:项目会议上,就是否升级某软件到最新版本, ...

  9. ArrayBlockingQueue 和LinkedBlockQueue

    ArrayBlockingQueue ArrayBlockingQueue是Java多线程常用的线程安全的一个集合,基于数组实现,继承自AbstractQueue,实现了BlockingQueue和S ...

  10. 【转】IOS获取屏窗高度踩坑之window.outerHeight

    近日本人在直接使用window.outerHeight获取屏窗高度时 在iphone 6中出现等于0的情况,从而导致页面发生错误 后找遍代码,测试无数,终于让我逮住了这个该死兼容 window.out ...