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)
但可能导致空间复杂度太大

注意一些字符输入的细节

  1 #include <iostream>
  2 #include <map>
  3 #include <unordered_map>
  4 #include <set>
  5 #include <string>
  6 using namespace std;
  7
  8 方法一:
  9 struct node
 10 {
 11     string name, author, keywords, publisher, year;
 12 };
 13 int main()
 14 {
 15     int N, M;
 16     string ID;
 17     cin >> N;
 18     map<string, node>data;
 19     for (int i = 0; i < N; ++i)
 20     {
 21         node book;
 22         cin >> ID;
 23         getchar();//去除回车键
 24         getline(cin, book.name);
 25         getline(cin, book.author);
 26         getline(cin, book.keywords);
 27         getline(cin, book.publisher);
 28         cin >> book.year;
 29         data[ID] = book;
 30     }
 31     cin >> M;
 32     getchar();//去除回车键
 33     for (int i = 0; i < M; ++i)
 34     {
 35         string str;
 36         bool is = true;
 37         getline(cin, str);
 38         cout << str << endl;
 39         int index = str[0] - ‘0‘;
 40         str.assign(str.begin() + 3, str.end());
 41         for (auto ptr = data.begin(); ptr != data.end(); ++ptr)
 42         {
 43             switch (index)
 44             {
 45             case 1:
 46                 if (ptr->second.name == str)
 47                 {
 48                     is = false;
 49                     cout << ptr->first << endl;
 50                 }
 51                 break;
 52             case 2:
 53                 if (ptr->second.author== str)
 54                 {
 55                     is = false;
 56                     cout << ptr->first << endl;
 57                 }
 58                 break;
 59             case 3:
 60                 if (ptr->second.keywords.find(str) !=-1)
 61                 {
 62                     is = false;
 63                     cout << ptr->first << endl;
 64                 }
 65                 break;
 66             case 4:
 67                 if (ptr->second.publisher == str)
 68                 {
 69                     is = false;
 70                     cout << ptr->first << endl;
 71                 }
 72                 break;
 73             case 5:
 74                 if (ptr->second.year == str)
 75                 {
 76                     is = false;
 77                     cout << ptr->first << endl;
 78                 }
 79                 break;
 80             default:
 81                 break;
 82             }
 83         }
 84         if (is)
 85             cout << "Not Found" << endl;
 86     }
 87     return 0;
 88 }
 89
 90 //方法二
 91 void findInfo(unordered_map<string, set<int>>&data,string &str)//传参一定要用引用,否则最后一组数据可能会超时
 92 {
 93     if(data.find(str)==data.end())
 94         printf("Not Found\n");
 95     else
 96     {
 97         for (auto ptr = data.find(str)->second.begin(); ptr != data.find(str)->second.end(); ++ptr)
 98             printf("%07d\n", *ptr);
 99     }
100 }
101 int main()
102 {
103     int N, M, ID;
104     scanf("%d", &N);
105     string til, aut, keys, pub, yea;
106     unordered_map<string, set<int>>title, author, keywords, publisher, year;//因为key不唯一
107     for (int i = 0; i < N; ++i)
108     {
109         scanf("%d\n", &ID);//不用清除回车键
110         getline(cin, til);
111         title[til].insert(ID);
112         getline(cin, aut);
113         author[aut].insert(ID);
114         while (cin >> keys)
115         {
116             keywords[keys].insert(ID);
117             char c = getchar();
118             if (c == ‘\n‘)break;
119         }
120         getline(cin, pub);
121         publisher[pub].insert(ID);
122         getline(cin, yea);
123         year[yea].insert(ID);
124     }
125     scanf("%d\n", &M);
126     for (int i = 0; i < M; ++i)
127     {
128         string str;
129         getline(cin, str);
130         cout << str << endl;
131         int index = str[0] - ‘0‘;
132         str.assign(str.begin() + 3, str.end());
133         if (index == 1) findInfo(title, str);
134         else if (index == 2) findInfo(author, str);
135         else if (index == 3) findInfo(keywords, str);
136         else if (index == 4) findInfo(publisher, str);
137         else if (index == 5) findInfo(year, str);
138         else printf("Not Found\n");
139     }
140     return 0;
141 }

原文地址:https://www.cnblogs.com/zzw1024/p/11247296.html

时间: 2024-10-29 15:40:26

PAT甲级——A1022 Digital Library的相关文章

PAT 甲级 1022 Digital Library

https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336 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 assigne

PAT甲级1022 Digital Library

题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336 题意: 每一本书有一个id, 书名,作者,至多五个关键字,一个出版社名,出版社年份. 现在根据给定的书名或作者或关键字或出版社名或年份,按照id字典序大小输出符合条件的书. 思路: 对每一个属性都用map维护. 一个坑点是,前面说书的年份一定在1000-3000之间,但是查询的时候的年份不一定满足,而且这里输出的时候也要满足4位.测试点

A1022. Digital Library (30)

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 s

pat advanced level1022 digital library

1022 Digital Library (30)(30 分) 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 an

PAT Advanced 1022 Digital Library (30分)

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 s

PAT:1022. Digital Library (30) (部分正确,错最后两个)

1 #include<stdio.h> 2 #include<string.h> 3 #include<map> 4 #include<string> 5 #include<set> 6 #include<iostream> 7 using namespace std; 8 int n; //n本书 9 //标题.作者.关键字.出版社.出版年份与ID的映射 10 map<string,set<int>> mpT

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

https://www.patest.cn/contests/pat-a-practise/1022 直接模拟, 输入,按id排序,检索 #include <iostream> #include <string> #include <algorithm> using namespace std; struct book //图书结构 { string id; string title; string author; int k; //关键词数量 string key[5

C++ STL map A1022. Digital Library(30) (注意字符串的读入)

#include <bits/stdc++.h> #include<math.h> #include <string> using namespace std; //5个map变量分别建立书名,作者,关键词,出版社及出版年份与id的映射关系 map<string,set<int>> mpTitle,mpAuthor,mpKey,mpPub,mpYear; void query(map<string,set<int>>&am

A1022 Digital Library (30分)

一.技术总结 首先这是一个map,STL类型的题目,前面的一个问题是存储,首先可能会想到的是,把每个信息存储下来然后通过输入想要查询的方式进行查询.可是,如果这里会发现,是通过关键词然后查询输出,相关book的id号.这个样其实就可以使用map,把相关的关键词作为一个string,然后id使用一个set容器进行存储. 所以存储的形式就是map<string, set > 还有一点需要注意的是,查询函数query(),参数注意要使用引用,不然会有超时. 还有就是在输入一行中的单个字符串的时候,包