1022 Digital Library (30 分)

1022 Digital Library (30 分)

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 (≤10
​4
​​ ) 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

题意:输入n本书的编号,编号,书名,作者,关键词(多个),出版社,出版年份;m个查询,输入任意一个非编号的属性名(如书名,作者。。)求它的编号(可能有多个)。
解题思路

  1. 用map映射是最简单的,关键是映射的方法:这里将属性看做key,Id看做value,这样每输入一个查询的属性即可直接输出其所拥有的Id
  2. 属性用string即可(注意,每一个关键词看做一个key),由于一个属性名(比如书名:The Testing Book)可能对应多个编号,所以编号用set存储
  3. 为什么构建多个map(title, author, key…)而不是只构建一个呢?是为了防止当一个属性名誉另一个属性名相同(比如一本书title为code,另一本书关键词为code,而查询的是title和code中的一个,这样就会输出多余)
  4. 学习总结字符串的各种输入方法
//接收一行字符串
string str;
getline(cin,str);

char c[20];
cin.getline(c,10);

char c[20];
gets(c);//可用于多维数组

//遇到空格,tab,换行符停止
cin >> str;
  1. 先读数字,再字符串,中间一定要getchar()掉换行符
  2. 如何用cin读取一行内多个不含空格的string呢?
 while(cin >> s){ 
          if(getchar()=='\n') break;
       }
  1. 可以这样输入:(确实忘了)
scanf("%d: ",&num);
  1. 迭代器可用auto(c++11特性支持,PAT中C++(g++)编译器支持)
 for(auto it = mp[str].begin(); it != mp[str].end(); it++)
  1. 输出编号注意格式,前导0要输出(共占7位):否则错两个样例
  2. 函数query:若不用引用最后一个样例超时,原因是传值时map要赋值给另一个map,耗时大;string参数可以传值没问题
void query(map<string,set<int> >&mp, string &str)

代码

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

//由于均是查询Id,故可将Id看做value,其余属性每一项看做key 构建map

map<string,set<int> > title, author, key, pub, year;
//比如title
//key                   value
//The Testing Book      1111111
//Another Testing Book  3333333
//The Testing Book      2222222

void query(map<string,set<int> >&mp, string &str){//取消引用最后一个测试用例会超时
    if(mp.find(str) != mp.end()){
        for(set<int>::iterator it = mp[str].begin(); it != mp[str].end(); it++){ //注意迭代器的格式,不是map //也可用 auto c++11特性PAT支持
            printf("%07d\n",*it);//*不确定宽度,错两个样例
        }
    }
    else printf("Not Found\n");
}

int main()
{
    int n,m,id,num;
    scanf("%d",&n);
    string _title,_author,_key,_pub,_year;
    for(int i = 0; i < n; i++){
        scanf("%d",&id);
        getchar();//* 或者直接scanf("%d\n",&id);
        getline(cin,_title); title[_title].insert(id);
        getline(cin,_author); author[_author].insert(id);
        while(cin >> _key){ //*
           key[_key].insert(id);
           if(getchar()=='\n') break;
        }
        getline(cin,_pub); pub[_pub].insert(id);
        getline(cin, _year); year[_year].insert(id);
    }
    scanf("%d",&m);
    while(m--){
        scanf("%d: ",&num);//*
        string str;
        getline(cin,str);
        cout << num << ": " << str << "\n";
        if(num==1) query(title,str);
        else if(num==2) query(author,str);
        else if(num==3) query(key,str);
        else if(num==4) query(pub,str);
        else if(num==5) query(year,str);
    }
    return 0;
}

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值