PTA-1022——Digital Library

Posted orangecyh

tags:

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

分析:

模拟题。注意带空格的字符串输入,点击前往

此题更好的方法是用map映射,这边没有用。

代码:

  1 #include<iostream>
  2 #include<algorithm>
  3 #include<vector>
  4 #include<cstring>
  5 using namespace std;
  6 int n;
  7 int m;
  8 struct Book{
  9     int id;
 10     string name;
 11     string author;
 12     vector<string> keywords;
 13     string publisher;
 14     string year;
 15 };
 16 
 17 Book books[10001];    //书的集合 
 18 vector<int> ans;
 19 
 20 bool cmp(Book a,Book b){    //根据ID快排 
 21     return a.id<b.id;
 22 }
 23 
 24 void findByTitle(string query){    //第一种情况,根据书名查询 
 25     for(int i=0;i<n;i++){
 26         if(books[i].name==query){
 27             ans.push_back(books[i].id);
 28         }
 29     }
 30 } 
 31 
 32 void findByAuthor(string query){    //第二种情况,根据作者查询 
 33     for(int i=0;i<n;i++){
 34         if(books[i].author==query){
 35             ans.push_back(books[i].id);
 36         }
 37     }
 38 } 
 39 
 40 void findByWord(string query){    //第三种情况,根据关键字查询 
 41     for(int i=0;i<n;i++){
 42         for(vector<string>::iterator it=books[i].keywords.begin();it!=books[i].keywords.end();it++){
 43             if(*it==query){
 44                 ans.push_back(books[i].id);
 45                 break;
 46             }
 47         }
 48     }
 49 }
 50 
 51 void findByPublisher(string query){    //第四种情况,根据出版社查询
 52     for(int i=0;i<n;i++){
 53         if(books[i].publisher==query){
 54             ans.push_back(books[i].id);
 55         }
 56     }         
 57 } 
 58 
 59 void findByYear(string query){    //第五种情况,根据年份查询    
 60     for(int i=0;i<n;i++){
 61         if(books[i].year==query){
 62             ans.push_back(books[i].id);
 63         }
 64     }         
 65 } 
 66 
 67 int main(){
 68     cin>>n;
 69     for(int i=0;i<n;i++){
 70         cin>>books[i].id;
 71         cin.ignore();    //在cin和getline之间要加ignore函数 
 72         getline(cin,books[i].name);    //输入含有空格的字符串可以使用getline 
 73         getline(cin,books[i].author);
 74         string tempKeywords;
 75         getline(cin,tempKeywords);
 76         int k=0;
 77         while(!tempKeywords.empty()){    //关键字按空格进行划分 
 78             if(tempKeywords[k]== ){
 79                 books[i].keywords.push_back(tempKeywords.substr(0,k));
 80                 tempKeywords=tempKeywords.substr(k+1);
 81                 k=0;
 82             }else if(k==tempKeywords.length()){
 83                 books[i].keywords.push_back(tempKeywords.substr(0,k));
 84                 tempKeywords="";
 85                 k=0;
 86             }else{
 87                 k++;
 88             }
 89         }
 90         getline(cin,books[i].publisher);
 91         getline(cin,books[i].year);
 92     }
 93     sort(books,books+n,cmp);    //对书进行排序 
 94     cin>>m;
 95     for(int i=0;i<m;i++){
 96         int choice;
 97         string query;
 98         scanf("%d: ",&choice);
 99         getline(cin,query);
100         ans.clear();
101         switch(choice){
102             case 1:    findByTitle(query);break;
103             case 2:    findByAuthor(query);break;
104             case 3:    findByWord(query);break;
105             case 4:    findByPublisher(query);break;
106             case 5:    findByYear(query);break;
107         }
108         cout<<choice<<": "<<query<<endl;
109         if(ans.size()==0){
110             cout<<"Not Found"<<endl;
111         }else{
112             for(vector<int>::iterator it=ans.begin();it!=ans.end();it++){
113                 printf("%07d
", *it);
114             }
115         }
116     }
117     return 0;
118 } 

 

 

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

1022 Digital Library (30)(30 分)

1022 Digital Library (30)(30 分)

PAT 甲级 1022 Digital Library

PAT甲级——A1022 Digital Library

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

PAT Advanced 1022 Digital Library (30分)