(PAT) 1022 Digital Library (利用映射map存储数据,以及输入输出控制)

忘是亡心i 2022-03-26 13:46 196阅读 0赞

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 (≤104) 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 (≤1000) 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:

  1. 3
  2. 1111111
  3. The Testing Book
  4. Yue Chen
  5. test code debug sort keywords
  6. ZUCS Print
  7. 2011
  8. 3333333
  9. Another Testing Book
  10. Yue Chen
  11. test code sort keywords
  12. ZUCS Print2
  13. 2012
  14. 2222222
  15. The Testing Book
  16. CYLL
  17. keywords debug book
  18. ZUCS Print2
  19. 2011
  20. 6
  21. 1: The Testing Book
  22. 2: Yue Chen
  23. 3: keywords
  24. 4: ZUCS Print
  25. 5: 2011
  26. 3: blablabla

Sample Output:

  1. 1: The Testing Book
  2. 1111111
  3. 2222222
  4. 2: Yue Chen
  5. 1111111
  6. 3333333
  7. 3: keywords
  8. 1111111
  9. 2222222
  10. 3333333
  11. 4: ZUCS Print
  12. 1111111
  13. 5: 2011
  14. 1111111
  15. 2222222
  16. 3: blablabla
  17. Not Found

解题思路:

我们使用 map>这种数据结构来存储数据,比如现在有3个人(编号1001,1002,1007)姓名均为Mike,那么当查询所有姓名为mike的人的编号时,输出1001,1002,1007,这样只需遍历这个集合就可以获取所有姓名为mike的人的编号。

使用Map[“Mike”].insert(1001) 就可以实现加入

接下来是读入数据的问题

使用cin读取字符串的话,空格会被当成结束符,所以如果输入The Testing book的话,cin只会读入The

要读入空格的话,必须使用getline(cin,string)

同时,在用scanf()读取数据后,必须用getchar()将换行吸收掉,否则getline会把换行读入

关于这题,创建5个map存储对应的ID,然后根据用户的输入数据查询即可

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<map>
  4. #include<string.h>
  5. #include<string>
  6. #include<set>
  7. using namespace std;
  8. map<string, set<int>> titleMap, authorMap, keyWordsMap, publisherMap, publisherYearMap; //存5张map
  9. void searchInfo(map<string, set<int>>& MAP, string& content) {
  10. if (MAP.find(content) == MAP.end()) {
  11. cout << "Not Found" << endl;
  12. }
  13. else {
  14. for (auto itr = MAP[content].begin(); itr != MAP[content].end(); ++itr) {
  15. printf("%07d\n", *itr);
  16. }
  17. }
  18. }
  19. int main() {
  20. int N, M;
  21. char c; //用来吸收掉空格
  22. scanf("%d", &N);
  23. for (int i = 0; i < N; ++i) {
  24. string input;
  25. int ID;
  26. scanf("%d", &ID);
  27. c = getchar();
  28. getline(cin, input);
  29. titleMap[input].insert(ID);
  30. getline(cin, input);
  31. authorMap[input].insert(ID);
  32. while (cin >> input) {
  33. keyWordsMap[input].insert(ID);
  34. c = getchar();
  35. if(c == '\n') break; //如果是换行的话,退出
  36. }
  37. getline(cin, input);
  38. publisherMap[input].insert(ID);
  39. getline(cin, input);
  40. publisherYearMap[input].insert(ID);
  41. }
  42. scanf("%d", &M);
  43. for (int i = 0; i < M; ++i) {
  44. int queryId;
  45. string queryContent;
  46. scanf("%d: ", &queryId);
  47. getline(cin, queryContent);
  48. cout << queryId << ": " << queryContent << endl;
  49. switch (queryId)
  50. {
  51. case 1:
  52. searchInfo(titleMap, queryContent);
  53. break;
  54. case 2:
  55. searchInfo(authorMap, queryContent);
  56. break;
  57. case 3:
  58. searchInfo(keyWordsMap, queryContent);
  59. break;
  60. case 4:
  61. searchInfo(publisherMap, queryContent);
  62. break;
  63. case 5:
  64. searchInfo(publisherYearMap, queryContent);
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. system("PAUSE");
  71. return 0;
  72. }

发表评论

表情:
评论列表 (有 0 条评论,196人围观)

还没有评论,来说两句吧...

相关阅读