PAT甲级 1022 Digital Library(30) (STL+哈希)

该博客介绍了一道PAT甲级编程题,涉及如何建立和查询一个数字化图书馆的数据库。每个书籍由ID、标题、作者、关键词、出版社和出版年份等信息标识。博客提供了输入输出样例,并解释了题意,即通过哈希表实现不同类型的查询(按书名、作者、关键词、出版社、年份),最后给出了部分代码实现。
摘要由CSDN通过智能技术生成

题目

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.

输入

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

输出

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.

样例输入 

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

样例输出 

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号查询的话 查询这个书名的id有哪些

如果2号查询的话 查询这个作者写过的书id有哪些

如果3号查询的话 查询有这些关键单词的书id有哪些

如果4号查询的话 查询这个出版社出版了书的id有哪些

如果5号查询的话 查询这个年份出版的书的id有哪些

我们这里其实用到了哈希的思想,把书id放入对应的哈希表,然后再哈希表里面查询,如果这个符合条件我们就输出

注意这里如果没有查到,也就是size为0的话那么输出Not Found

代码 

#include<bits/stdc++.h> 
using namespace std;
typedef long long LL;
typedef unsigned long long ULL ;
typedef pair<int,int> PII;
#define fx first
#define fy second
const int INF =0x3f3f3f3f;
const double II=0x7f7f7f7f;
const int N=1e4+10,M=2e6+10;
const int MOD=1e5+3;
typedef unordered_map<string,set<string> > MSS;
typedef struct book{
    string id,title,author;
    string key,publisher,year;
}book;
book a[N];
int n,m;
MSS title,author,key,publisher,year;
int main(){
    
    cin>>n;
    getchar();
    for(int i=0;i<n;i++){
        getline(cin,a[i].id);
        getline(cin,a[i].title);
        getline(cin,a[i].author);
        getline(cin,a[i].key);
        stringstream word;
        word<<a[i].key;
        string t;
        while(word>>t)key[t].insert(a[i].id);
    
        getline(cin,a[i].publisher);
        getline(cin,a[i].year);
        
        title[a[i].title].insert(a[i].id);
        author[a[i].author].insert(a[i].id);
  
        publisher[a[i].publisher].insert(a[i].id);
        year[a[i].year].insert(a[i].id);
    }
    cin>>m;
    getchar();
    while(m--){
        string ss;
        getline(cin,ss);
        int k=ss[0]-'0';
        string s1=ss.substr(3,ss.size()-3);

        if(k==1){
            cout<<1<<": "<<s1<<endl;
            if(!title[s1].size()){
                cout<<"Not Found"<<endl;
                continue;
            }
            for(auto tmp:title[s1]){
                cout<<tmp<<endl;
            }
        }
        else if(k==2){
            cout<<2<<": "<<s1<<endl;;
            if(!author[s1].size()){
                cout<<"Not Found"<<endl;
                continue;
            }
            for(auto tmp:author[s1]){
                cout<<tmp<<endl;
            }
        }
        else if(k==3){
            cout<<3<<": "<<s1<<endl;
            if(!key[s1].size()){
                cout<<"Not Found"<<endl;
                continue;
            }
            for(auto tmp:key[s1]){
                cout<<tmp<<endl;
            }
        }
        else if(k==4){
            cout<<4<<": "<<s1<<endl;
            if(!publisher[s1].size()){
                cout<<"Not Found"<<endl;
                continue;
            }
            for(auto tmp:publisher[s1]){
                cout<<tmp<<endl;
            }
        }
        else if(k==5){
            cout<<5<<": "<<s1<<endl;
            if(!year[s1].size()){
                cout<<"Not Found"<<endl;
                continue;
            }
            for(auto tmp:year[s1]){
                cout<<tmp<<endl;
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值