1022 Digital Library(搜索,map和set)

悠悠 2024-04-08 09:52 159阅读 0赞

1022 Digital Library

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

1、大致题意

输入一本书的各个数据,建立图书查询系统,最后对书目进行查询。

2、基本思路

使用 map 来映射书和书的各个数据,用 set 来存储书的编号,set 可以自动去重并且按从小到大的顺序排列。

在写这题前,我复习了 map的相关知识

3、解题过程

  1. 对除了 id 之外的其他信息都建立一个 map<string, set> ,把相应的id插入对应搜索词的 map 的集合里面,形成一个信息对应一个集合,集合里面是复合条件的书的 id
  2. 因为对于输入的关键词(可以重复,算是书本对应的tag标签吧~)没有给定关键词的个数,可以使用 while(cin >> s) 并且判断 c = getchar()c 是否等于\n,如果是再退出循环~
  3. 建立 query ,通过传参的形式可以将不同的 map 名称统一化,先要判断 map.find()m.end() 是否相等,如果不等再去遍历整个 map ,输出所有满足条件的 id,如果相等就说明不存在这个搜索词对应的id,那么就要输出 Not Found
  4. 传参一定要用引用,否则最后一组数据可能会超时~

    include

    include

    include

    using namespace std;
    map > title, author, key, pub, year;
    void query(map > &m, string &str) {

    1. if(m.find(str) != m.end()) {
    2. for(auto it = m[str].begin(); it != m[str].end(); it++)
    3. printf("%07d\n", *it);
    4. } else
    5. cout << "Not Found\n";

    }
    int main() {

    1. int n, m, id, num;
    2. scanf("%d", &n);
    3. string ttitle, tauthor, tkey, tpub, tyear;
    4. for(int i = 0; i < n; i++) {
    5. scanf("%d\n", &id);
    6. getline(cin, ttitle);
    7. title[ttitle].insert(id);
    8. getline(cin, tauthor);
    9. author[tauthor].insert(id);
    10. while(cin >> tkey) {
    11. key[tkey].insert(id);
    12. char c = getchar();
    13. if(c == '\n') break;
    14. }
    15. getline(cin, tpub);
    16. pub[tpub].insert(id);
    17. getline(cin, tyear);
    18. year[tyear].insert(id);
    19. }
    20. scanf("%d", &m);
    21. for(int i = 0; i < m; i++) {
    22. scanf("%d: ", &num);
    23. string temp;
    24. getline(cin, temp);
    25. cout << num << ": " << temp << "\n";
    26. if(num == 1) query(title, temp);
    27. else if(num == 2) query(author, temp);
    28. else if(num == 3) query(key, temp);
    29. else if(num == 4) query(pub,temp);
    30. else if(num ==5) query(year, temp);
    31. }
    32. return 0;

    }

发表评论

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

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

相关阅读

    相关 SetMap

    Set 一、创建Set对象实例 Set 对象允许你存储任何类型的唯一值,无论是原始值或者是对象引用 1.构造函数 语法:new Set(\[iterable\]

    相关 MapSet

    List Map Set是Java集合的三大类。它们都有各自的实现类。 List的主要实现类为 ArrayList 和LinkedList Map主要实现类为HashM