PAT甲级真题1022 数字图书馆(STL容器的运用)

数字图书馆包含数以百万计的书籍。

每本书的书名,作者,摘要关键词,出版商和出版年限这五类关键信息都在数据库中有所记录。

每本书被分配一个唯一的 77 位数字作为其 ID。

当读者查询某一关键信息时,你应该找到所有与查询相关的书籍,并将它们按 ID 的升序排序输出。

输入格式
第一行包含整数 NN,表示共有 NN 本书。

接下来包含这 NN 本书的具体信息,每本书的相关信息占 66 行:

第一行:书的 ID,一个 77 位数字。
第二行:书名,一个长度不超过 8080 的字符串。
第三行:作者,一个长度不超过 8080 的字符串。
第四行:关键词,每个关键词的长度均不超过 1010,且关键词中不包含空格,关键词之间用空格隔开。
第五行:出版商,一个长度不超过 8080 的字符串。
第六行:出版年限,一个在 [1000,3000][1000,3000] 范围内的 44 位数字。
一本书,只有一位作者,包含的关键词不超过 55 个。

总共不超过 10001000 个不同的关键词,不超过 10001000 个不同的出版商。

图书信息介绍完毕后,有一行包含一个整数 MM,表示查询次数。

接下来 MM 行,每行包含一个查询,具体格式如下:

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,查询出版年限。注意,这个年限可能包含前导 00。
输出格式
对于每个查询,首先将查询信息输出在一行中。

接下来若干行,每行输出一个查询到的相关书籍的 ID,按升序顺序排列。

如果查询不到相关书籍,则输出 Not Found。

数据范围
1≤N≤1041≤N≤104,
1≤M≤10001≤M≤1000
输入样例:
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

#include <iostream>
#include <map>
#include <set>
using namespace std;
map<string, set<int> > title, author, key, pub, year;
void query(map<string, set<int>>& m, string& str) {
    if(m.count(str)) {
        for(auto it = m[str].begin(); it != m[str].end(); it++)
            printf("%07d\n", *it);
    } else
        cout << "Not Found\n";
}
int main() {
    int n, m, id, num;
    scanf("%d", &n);
    string ttitle, tauthor, tkey, tpub, tyear;
    for(int i = 0; i < n; i++) {
        scanf("%d", &id);
        getchar();
        getline(cin, ttitle);
        title[ttitle].insert(id);
        getline(cin, tauthor);
        author[tauthor].insert(id);
        while(cin >> tkey) {
            key[tkey].insert(id);
            char c = getchar();
            if(c == '\n') break;
        }
        getline(cin, tpub);
        pub[tpub].insert(id);
        getline(cin, tyear);
        year[tyear].insert(id);
    }
    scanf("%d", &m);
    for(int i = 0; i < m; i++) {
        scanf("%d: ", &num);
        string temp;
        getline(cin, temp);
        cout << num << ": " << temp << "\n";
        if(num == 1) query(title, temp);
        else if(num == 2) query(author, temp);
        else if(num == 3) query(key, temp);
        else if(num == 4) query(pub,temp);
        else if(num ==5) query(year, temp);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>

using namespace std;

struct Book
{
    string id, name, author;
    set<string> keywords;
    string publisher, year;
};

int main()
{
    int n, m;
    cin >> n;

    vector<Book> books;
    while (n -- )
    {
        string id, name, author;
        cin >> id;
        getchar();
        getline(cin, name), getline(cin, author);
        string line;
        getline(cin, line);
        stringstream ssin(line);
        string keyword;
        set<string> keywords;
        while (ssin >> keyword) keywords.insert(keyword);
        string publisher, year;
        getline(cin, publisher);
        cin >> year;
        books.push_back({id, name, author, keywords, publisher, year});
    }

    cin >> m;
    getchar();
    string line;
    while (m -- )
    {
        getline(cin, line);
        cout << line << endl;
        string info = line.substr(3);
        char t = line[0];
        vector<string> res;
        if (t == '1')
        {
            for (auto& book : books)
                if (book.name == info)
                    res.push_back(book.id);
        }
        else if (t == '2')
        {
            for (auto& book : books)
                if (book.author == info)
                    res.push_back(book.id);
        }
        else if (t == '3')
        {
            for (auto& book : books)
                if (book.keywords.count(info))
                    res.push_back(book.id);
        }
        else if (t == '4')
        {
            for (auto& book : books)
                if (book.publisher == info)
                    res.push_back(book.id);
        }
        else
        {
            for (auto& book : books)
                if (book.year == info)
                    res.push_back(book.id);
        }

        if (res.empty()) puts("Not Found");
        else
        {
            sort(res.begin(), res.end());
            for (auto id : res) cout << id << endl;
        }
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小王子y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值