PAT甲级1022 Digital Library

题目https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336

题意:

每一本书有一个id, 书名,作者,至多五个关键字,一个出版社名,出版社年份。

现在根据给定的书名或作者或关键字或出版社名或年份,按照id字典序大小输出符合条件的书。

思路:

对每一个属性都用map维护。

一个坑点是,前面说书的年份一定在1000-3000之间,但是查询的时候的年份不一定满足,而且这里输出的时候也要满足4位。测试点1就是和年份有关的。

  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<map>
  4 #include<set>
  5 #include<iostream>
  6 #include<cstring>
  7 #include<algorithm>
  8 #include<vector>
  9 #include<cmath>
 10 #include<stack>
 11 #include<queue>
 12
 13 #define inf 0x7fffffff
 14 using namespace std;
 15 typedef long long LL;
 16 typedef pair<string, string> pr;
 17
 18 int n, m;
 19 const int maxn = 1e4 + 5;
 20
 21 struct node{
 22     string id;
 23     string title;
 24     string author;
 25     string keyword[5];
 26     string publisher;
 27     int year;
 28     int keynum = 0;
 29 }book[maxn];
 30
 31 map<string, int>titlemp;
 32 map<string, int>authormp;
 33 map<string, int>keymp;
 34 map<string, int>publishermp;
 35 map<int, string>idmp;
 36 int titletot, authortot, keytot, publishertot, idtot;
 37
 38 vector<int> title[maxn], author[maxn], keyword[1005], publisher[1005], year[2500];
 39
 40 bool cmp(int a, int b)
 41 {
 42     int ida = stoi(idmp[a]), idb = stoi(idmp[b]);
 43     return ida < idb;
 44 }
 45
 46 int main()
 47 {
 48     scanf("%d", &n);
 49     for(int i = 0; i < n; i++){
 50         getchar();getline(cin, book[i].id);
 51         idmp[i] = book[i].id;
 52
 53         getline(cin, book[i].title);
 54         if(titlemp.find(book[i].title) == titlemp.end()){
 55             titlemp[book[i].title] = titletot++;
 56         }
 57         title[titlemp[book[i].title]].push_back(i);
 58         getline(cin, book[i].author);
 59         if(authormp.find(book[i].author) == authormp.end()){
 60             authormp[book[i].author] = authortot++;
 61         }
 62         author[authormp[book[i].author]].push_back(i);
 63         char ch;
 64         while(1){
 65             cin>>book[i].keyword[book[i].keynum];
 66             //cout<<book[i].keyword[book[i].keynum]<<endl;
 67             if(keymp.find(book[i].keyword[book[i].keynum]) == keymp.end()){
 68                 keymp[book[i].keyword[book[i].keynum]] = keytot++;
 69             }
 70             keyword[keymp[book[i].keyword[book[i].keynum++]]].push_back(i);
 71             ch = getchar();
 72             if(ch == ‘\n‘)break;
 73         }
 74         getline(cin, book[i].publisher);
 75         if(publishermp.find(book[i].publisher) == publishermp.end()){
 76             publishermp[book[i].publisher] = publishertot++;
 77         }
 78         publisher[publishermp[book[i].publisher]].push_back(i);
 79         cin>>book[i].year;
 80         year[book[i].year - 1000].push_back(i);
 81     }
 82
 83     scanf("%d", &m);
 84     while(m--){
 85         int type;
 86         scanf("%d: ", &type);
 87         string s;
 88         int id, y;
 89         //getchar();
 90         switch(type){
 91             case 1:
 92                 getline(cin, s);
 93                 printf("1: ");cout<<s<<endl;
 94                 if(titlemp.find(s) == titlemp.end()){
 95                     printf("Not Found\n");
 96                 }
 97                 else{
 98                     id = titlemp[s];
 99                     sort(title[id].begin(), title[id].end(), cmp);
100                     for(int i = 0; i < title[id].size(); i++){
101                         cout<<idmp[title[id][i]]<<endl;
102                     }
103                 }
104                 break;
105             case 2:
106                 getline(cin, s);
107                 printf("2: ");cout<<s<<endl;
108                 if(authormp.find(s) == authormp.end()){
109                     printf("Not Found\n");
110                 }
111                 else{
112                     id = authormp[s];
113                     sort(author[id].begin(), author[id].end(), cmp);
114                     for(int i = 0; i < author[id].size(); i++){
115                         cout<<idmp[author[id][i]]<<endl;
116                     }
117                 }
118                 break;
119             case 3:
120                 getline(cin, s);
121                 printf("3: ");cout<<s<<endl;
122                 if(keymp.find(s) == keymp.end()){
123                     printf("Not Found\n");
124                 }
125                 //cout<<id<<endl;
126                 else{
127                     id = keymp[s];
128                     sort(keyword[id].begin(), keyword[id].end(), cmp);
129                     for(int i = 0; i < keyword[id].size(); i++){
130                         cout<<idmp[keyword[id][i]]<<endl;
131                     }
132                 }
133                 break;
134             case 4:
135                 getline(cin, s);
136                 printf("4: ");cout<<s<<endl;
137                 if(publishermp.find(s) == publishermp.end()){
138                     printf("Not Found\n");
139                 }
140                 else{
141                     id = publishermp[s];
142                     sort(publisher[id].begin(), publisher[id].end(), cmp);
143                     for(int i = 0; i < publisher[id].size(); i++){
144                         cout<<idmp[publisher[id][i]]<<endl;
145                     }
146                 }
147                 break;
148             case 5:
149                 cin>>s;
150                 printf("5: ");cout<<s<<endl;
151                 y = stoi(s);
152                 if( y<1000 || y >3000 || year[y - 1000].size() == 0 ){
153                     printf("Not Found\n");
154                 }
155                 else{
156                     sort(year[y - 1000].begin(), year[y - 1000].end(), cmp);
157                     for(int i = 0; i < year[y - 1000].size(); i++){
158                         cout<<idmp[year[y - 1000][i]]<<endl;
159                     }
160                 }
161                 break;
162         }
163     }
164     return 0;
165 }

原文地址:https://www.cnblogs.com/wyboooo/p/10661217.html

时间: 2024-10-19 02:53:41

PAT甲级1022 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甲级——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 s

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 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

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 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

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

1022. Digital Library (30) -map -字符串处理

题目如下: 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

1022 Digital Library (30)(30 point(s))

problem 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, y