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. mysql mac客户端: sequel,mysql-workbench

    sequel: https://sequelpro.com/download#auto-start mysql-workbench:https://dev.mysql.com/downloads/fi ...

  2. .NETFramework:Exception

    ylbtech-System.Exception.cs 1.返回顶部 1. #region 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, Public ...

  3. import、export 和 export default

    ES6中 在JavaScript ES6中,export与export default均可用于导出常量.函数.文件.模块等. 你可以在其它文件或模块中通过import+(常量 | 函数 | 文件 | ...

  4. JS while 循环

    while循环:只要条件成立,就重复不断的执行循环体代码 while(条件判断) { 如果条件为true,则执行循环体代码 } while循环结构说明:   在循环开始前,必须要对变量初始化(声明变量 ...

  5. AutowireCapableBeanFactory 根据名称:自动装配的BeanFactory,其实也是对BeanFactory的增强

    //自动装配的Bean 工厂 public interface AutowireCapableBeanFactory extends BeanFactory { //工厂没有自动装配的Bean int ...

  6. SpringBoot--Thymeleaf入门使用

    一.概述 今天学习到了SpringBoot中的WEB开发,SpringBoot提供了spring-boot-stater-web为web开发给予支持,它里面内嵌了以下依赖: <dependenc ...

  7. [kuangbin带你飞]专题一 简单搜索 - B - Dungeon Master

    #include<iostream> #include<cstdio> #include<string> #include<vector> #inclu ...

  8. 17.splash_case06_ScrapySplashTest-master

    taobao.py # -*- coding: utf-8 -*- from scrapy import Spider, Request from urllib.parse import quote ...

  9. JavaScript实现几种常见的图形

    一.四种常见的三角形 第一种三角形: for(var i=1;i<=5;i++){        for( var j=i;j<=5;j++){                 docum ...

  10. java_递归

    递归:方法在有结束条件的情况下调用自己本身 public static void main(String[] args) { int i = shu01(5); System.out.println( ...