PAT (Advanced Level) - 1022 Digital Library

在这里插入图片描述

模拟,排序,字符串,STL,stringstream

#include <iostream>
#include <cstring>
#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Akira37

💰unneeded

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

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

打赏作者

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

抵扣说明:

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

余额充值