1. 程式人生 > >PTA-1022——Digital Library

PTA-1022——Digital Library

contains sam fin nal vector 有用 pub clas 年份

題目:

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\n", *it);
114             }
115         }
116     }
117     return 0;
118 } 

 

PTA-1022——Digital Library