PAT甲级刷题记录——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 (≤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 Foundinstead.

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

思路

这题其实挺简单的,但是因为自己的蠢导致一直AC不了……(几乎所有测试点都格式错误,抓狂……但是样例输出又是完全正确的,我也检查过有没有空格之类的,后来自己随便测了几个未找到的数据才发现,Not Found后面没加换行符-_-||,要是在最后一行的话,Not Found换行符不加也没事,但是要是在中间的话,不加换行符,下一行的查询数据就会接在后面……以后还是养成好习惯输出完就打个换行符叭,反正最后一行多打个换行符是不会算格式错误的)。

还有另外一个坑点是,书的ID可能是从0开头的,也就是0012345之类的,因此,不能用int型存储。所以为了避免这种东一个西一个小问题,还是干脆全部用string来存叭,不管是ID还是年份,统统用string,你输入什么样子,我存进去就什么样子,那写的时候就能自然而然避开很多坑了~

思路还是很简单的,我在写之前没看书,因此,也就没用晴神的string映射set的方法(不过思路好像是一致的??大概)。总的想法就是:因为一个查询的信息可能对应多本书,因此,肯定不能用查询的信息作为key,但是题目又是要查询信息,然后得到相应书的ID,于是呢,很自然而然地就能想到,可以建立一本书对应一堆信息的方式,然后你查找相应信息,就输出对应书的ID。

因此,我们就用书的ID作为key,然后相应的信息作为value。这里key要用string类型,因为上面说到了,书的ID可能是0开头的,如果是int型,那么输出的时候最自动略去前导0。

那么相应的信息怎么存储呢?我这里用了结构体,因为STL的容器尖括号里面肯定是能用基本类型、结构体类型、容器的嘛,那么我就把第二个参数写成结构体,这样的话,就建立出了一个ID对应多个信息的关系。这里要注意的是,因为书本的关键词keywords的个数是不确定的,因此,我在结构体里用了vector(用法和普通树存储孩子的用法相同),然后在输入完之后(无论是输入什么,都要读取一行,因为可能存在空格),每找到一个关键词,就压入一个就好啦~

这里的注意点还是一样的,如果在getline之前有cin操作的话,一定要在cin后面跟一个cin.ignore(),把换行符忽略掉(getchar()也行)。

最后就很简单了,根据给出的信息,切割一下子串(因为查询信息前面带有数字和冒号),然后用迭代器去对这个map依次查找,如果找到某个value符合所要查找的信息,那么就输出(当value是结构体时用起来也很简单,就是it->second.xxx,点号后面跟的是你定义的属性,比如,it->second.title),这里要设置一个bool型变量flag,初始化为false,如果查询成功,就把它置为true,如果所有操作都结束了,它依然是false,那么说明查询失败,输出Not Found(记得换行……我在这里被格式错误坑了半个小时)。

总的来说,这题还是蛮简单的,如果一开始就用string写ID以及顺手打上Not Found后面的换行符的话(好习惯真的能自然而然避开不少坑),应该一遍就能AC了。

代码

#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<map>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
struct book{
    string title;
    string author;
    vector<string> keywords;
    string publisher;
    string publishYear;
};
map<string, book> MyBooks;
int main(){
    int N;
    cin>>N;
    cin.ignore();
    for(int i=0;i<N;i++){
        string tmpID, tmpTitle, tmpAuthor, tmpPublisher, tmpKeywords, tmpPublishYear;
        getline(cin, tmpID);
        getline(cin, tmpTitle);
        getline(cin, tmpAuthor);
        getline(cin, tmpKeywords);
        getline(cin, tmpPublisher);
        getline(cin, tmpPublishYear);
        MyBooks[tmpID].title = tmpTitle;
        MyBooks[tmpID].author = tmpAuthor;
        MyBooks[tmpID].publisher = tmpPublisher;
        MyBooks[tmpID].publishYear = tmpPublishYear;
        string tmp;
        for(int j=0;j<tmpKeywords.length();j++){
            if(tmpKeywords[j]==' '){
                MyBooks[tmpID].keywords.push_back(tmp);
                tmp.clear();
                continue;
            }
            else if(j==tmpKeywords.length()-1){
                tmp += tmpKeywords[j];
                MyBooks[tmpID].keywords.push_back(tmp);
                tmp.clear();
                break;
            }
            tmp += tmpKeywords[j];
        }
    }
    int M;
    cin>>M;
    cin.ignore();
    for(int i=0;i<M;i++){
        string tmp;
        getline(cin, tmp);
        string info = tmp.substr(3);
        bool flag = false;//记录是否有输出
        cout<<tmp<<'\n';
        if(tmp[0]=='1'){
            for(map<string, book>::iterator it=MyBooks.begin();it!=MyBooks.end();it++){
                if(it->second.title==info){
                    flag = true;
                    cout<<it->first<<'\n';
                }
            }
        }
        else if(tmp[0]=='2'){
            for(map<string, book>::iterator it=MyBooks.begin();it!=MyBooks.end();it++){
                if(it->second.author==info){
                    flag = true;
                    cout<<it->first<<'\n';
                }
            }
        }
        else if(tmp[0]=='3'){
            for(map<string, book>::iterator it=MyBooks.begin();it!=MyBooks.end();it++){
                for(int j=0;j<it->second.keywords.size();j++){
                    if(it->second.keywords[j]==info){
                        flag = true;
                        cout<<it->first<<'\n';
                    }
                }
            }
        }
        else if(tmp[0]=='4'){
            for(map<string, book>::iterator it=MyBooks.begin();it!=MyBooks.end();it++){
                if(it->second.publisher==info){
                    flag = true;
                    cout<<it->first<<'\n';
                }
            }
        }
        else{
            for(map<string, book>::iterator it=MyBooks.begin();it!=MyBooks.end();it++){
                if(it->second.publishYear==info){
                    flag = true;
                    cout<<it->first<<'\n';
                }
            }
        }
        if(flag==false) cout<<"Not Found"<<'\n';
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值