zoukankan      html  css  js  c++  java
  • A1022. 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 (<=10000) 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:

    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

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<map>
     5 #include<string>
     6 #include<set>
     7 using namespace std;
     8 map<string, set<int>> mp1, mp2, mp3, mp4, mp5;
     9 void show(map<string, set<int>> &mp, string &key){
    10     set<int> ::iterator it;
    11     int find = 0;
    12     for(it = mp[key].begin(); it != mp[key].end(); it++){
    13         printf("%07d
    ", *it);
    14         find = 1;
    15     }
    16     if(find == 0)
    17         printf("Not Found
    ");
    18 }
    19 int main(){
    20     int N, M, id;
    21     string ss, ss2;
    22     scanf("%d ", &N);
    23     for(int i = 0; i < N; i++){
    24         scanf("%d ", &id);
    25         getline(cin, ss);
    26         mp1[ss].insert(id);
    27         getline(cin, ss);
    28         mp2[ss].insert(id);
    29         while(cin >> ss){
    30             char c = getchar();
    31             mp3[ss].insert(id);
    32             if(c == '
    ')
    33                 break;
    34         }
    35         getline(cin, ss);
    36         mp4[ss].insert(id);
    37         getline(cin, ss);
    38         mp5[ss].insert(id);
    39     }
    40     scanf("%d ", &M);
    41     for(int i = 0; i < M; i++){
    42         getline(cin, ss2);
    43         cout << ss2 + "
    ";
    44         ss = ss2.substr(3);
    45         if(ss2[0] == '1'){
    46             show(mp1, ss);
    47         }else if(ss2[0] == '2'){
    48             show(mp2, ss);
    49         }else if(ss2[0] == '3'){
    50             show(mp3, ss);
    51         }else if(ss2[0] == '4'){
    52             show(mp4, ss);
    53         }else if(ss2[0] == '5'){
    54             show(mp5, ss);
    55         }
    56     }
    57     cin >> N;
    58     return 0;
    59 }
    View Code

    总结:

    1、题意:先输入书的id、名字、作者、关键词、出版社、出版时间。然后根据除书id以外的信息来查询书的id。注意其中书的关键词在输入时为一行字符串,其实是多个关键词以空格分开。由于一个信息可能对多本书(一个出版社有多本书...),且在查询结果时需要有序输出,所以可以使用 map<string ,  set<int>> 存储。

    2、对于下面信息的第4行,需要分别读出每个单词,可以这么做

    1111111
    The Testing Book
    Yue Chen
    test code debug sort keywords
    ZUCS Print
    2011

      while(cin >> ss){
        char c = getchar();
        mp3[ss].insert(id);
        if(c == ' ')
        break;
      }

      而当要用cin 读入带空格的一行时,可以 getline(cin , str);

    3、在函数传参时,如果参数中有map、set、string等,应该传引用,否则可能会超时。

    
    
  • 相关阅读:
    PostgreSQL数据库逻辑复制实践
    CentOS7通过yum安装postgreSQL
    MongoDB动态建表方案(官方原生驱动)
    7大常用开源数据库利弊全对比
    错误:由于系统时间错误证书验证失败导致更新不成功
    deppin更新提示“由于没有公钥,无法验证下列签名”
    Debian 9 Vim无法使用鼠标右键复制 解决方法
    PHP版滑动时间窗口算法
    RabbitMQ PHP 代码示例
    创建或修改 docker 容器内部文件
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8528870.html
Copyright © 2011-2022 走看看