1. 程式人生 > >PTA (Advanced Level)1022 Digital Library

PTA (Advanced Level)1022 Digital Library

incr == c++ 作者 sed ngs then eve debug

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

題目解析
  本題給出n個書本信息,每個書本給出id,書名,作者,關鍵詞,出版社,發行年份,這五個信息,要求分別按照五個信息建立索引,之後給出m行查詢,查詢格式如下:

1:書名

2:作者

3:一個關鍵詞

4:出版商

5:年份

  要求輸出查詢信息所包含的所有書的id。

  雖然這是一個30分的題目但是它並不是膨脹困難,書名,作者,關鍵詞,出版社,發行年份我們都可以用string類型保存,對於書籍id我們則用int進行存儲,這裏可以用map建立string與set<int>的對應關系,set<int>裏儲存的便是對應string所包含所有書的id(由於書的id一定不重復,且最後要求由小到大輸出id,用set會方便很多)。註意關鍵詞有很多個,每個都需要建立對應關系。

AC代碼

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 map<string, set<int> > title, author, keyWord, publisher, year;
 4 //分別對應 同名的書 同作者的書 同關鍵詞的書 同出版社的書 同年的書
 5 int n;
 6 int main()
 7 {
 8     scanf("%d", &n);    //輸入書的個數;
 9     for(int i = 0; i < n; i++){
10         int id;
11         string  tTitle, tAuthor, tKeyWord, tPublisher, tYear;
12 
13         cin >> id;  //輸入id
14         getchar();  //吸收換行符
15         getline(cin, tTitle);   //輸入書名
16         getline(cin, tAuthor);  //輸入作者
17         getline(cin, tKeyWord); //輸入該書所有關鍵詞
18         getline(cin, tPublisher); //輸入出版社
19         getline(cin, tYear);    //輸入年份
20 
21 
22         title[tTitle].insert(id);   //對應書名的集合中加入id
23         author[tAuthor].insert(id); //對應作者的集合中加入id
24         string temp;    //temp用來獲取每一個關鍵詞
25         istringstream cinKeyWord(tKeyWord); //用tKeyWord作為輸入流輸入每一個關鍵詞
26         while(cinKeyWord >> temp){
27             keyWord[temp].insert(id);   //每一個關鍵詞對應的集合中都加入id
28         }
29         publisher[tPublisher].insert(id);   //對應出版社的集合加入id
30         year[tYear].insert(id); //對應年份的集合加入id
31     }
32     scanf("%d", &n);    //輸入查詢個數
33     for(int i = 0; i < n; i++){
34         int query;
35         string temp;
36         scanf("%d: ", &query);  //輸入查詢控制符
37         getline(cin, temp); //輸入查詢內容
38         cout << query << ": " << temp << endl;
39         //由於string傳送速度較慢,若將查詢過程包裝為一個函數,最後一個結點會超時
40         //不過傳引用仿佛可以解決這個問題
41         if(query == 1){ //控制符為1 輸出 對應書名集合中包含的書
42             if(title.find(temp) == title.end())
43                 printf("Not Found\n");
44             else
45                 for(auto i : title[temp])
46                     printf("%07d\n", i);
47         }else if(query == 2){   //控制符為2 輸出 對應作者集合中包含的書
48             if(author.find(temp) == author.end())
49                 printf("Not Found\n");
50             else
51                 for(auto i : author[temp])
52                     printf("%07d\n", i);
53         }else if(query == 3){   //控制符為3 輸出 對應關鍵詞集合中包含的書
54             if(keyWord.find(temp) == keyWord.end())
55                 printf("Not Found\n");
56             else
57                 for(auto i : keyWord[temp])
58                     printf("%07d\n", i);
59         }else if(query == 4){   //控制符為4 輸出 對應出版社集合中包含的書
60             if(publisher.find(temp) == publisher.end())
61                 printf("Not Found\n");
62             else
63                 for(auto i : publisher[temp])
64                     printf("%07d\n", i);
65         }else if(query == 5){   //控制符為5 輸出 對應年份集合中包含的書
66             if(year.find(temp) == year.end())
67                 printf("Not Found\n");
68             else
69                 for(auto i : year[temp])
70                     printf("%07d\n", i);
71         }
72     }
73     return 0;
74 }

PTA (Advanced Level)1022 Digital Library