PAT甲级1022 Digital Library

Posted wyboooo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 == 
)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
");
 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
");
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
");
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
");
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
");
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 }

 

以上是关于PAT甲级1022 Digital Library的主要内容,如果未能解决你的问题,请参考以下文章

PAT甲级1022 Digital Library

pat advanced level1022 digital library

PAT Advanced 1022 Digital Library (30分)

pat1022 Digital Library

PAT-1022 Digital Library (30 分) 字符串处理

PAT (Advanced Level) 1022. Digital Library (30)