PAT甲级 1022 Digital Library (30 分)

该博客讨论了一个用于数字图书馆的检索系统,系统能够根据读者的查询,如书名、作者、关键词、出版社和出版年份,快速找到对应书籍并按ID排序返回结果。文章详细介绍了输入和输出规格,并给出了一段C++代码实现,该代码利用了哈希映射进行高效查找。

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 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
Not Found
#include <iostream>
#include <unordered_map>
#include <vector>
#include <sstream>
#include <algorithm>

using namespace std;

void printError() {
    cout << "Not Found" << endl;
}

int main() {
    int n, m;
    unordered_map<string, vector<string>> title, author, keyWord, publisher, year;
    cin >> n;
    string id, bookTitle, bookAuthor, keyW, BookYear, bookPublisher;
    for (int i = 0; i < n; ++i) {
        cin >> id;
        getchar();
        getline(cin, bookTitle);
        getline(cin, bookAuthor);
        getline(cin, keyW);
        getline(cin, bookPublisher);
        cin >> BookYear;
        string key;
        stringstream ssin(keyW);
        while (ssin >> key) {
            keyWord[key].push_back(id);
        }
        title[bookTitle].push_back(id);
        author[bookAuthor].push_back(id);
        publisher[bookPublisher].push_back(id);
        year[BookYear].push_back(id);
    }
    string query;
    cin >> m;
    getchar();
    for (int i = 0; i < m; ++i) {
        getline(cin, query);
        cout << query << endl;
        vector<string> v;
        if (query[0] == '1') {
            if (!title.count(query.substr(3))) {
                printError();
                continue;
            } else
                v = title[query.substr(3)];
        } else if (query[0] == '2') {
            if (!author.count(query.substr(3))) {
                printError();
                continue;
            } else
                v = author[query.substr(3)];
        } else if (query[0] == '3') {
            if (!keyWord.count(query.substr(3))) {
                printError();
                continue;
            } else
                v = keyWord[query.substr(3)];
        } else if (query[0] == '4') {
            if (!publisher.count(query.substr(3))) {
                printError();
                continue;
            } else
                v = publisher[query.substr(3)];
        } else if (query[0] == '5') {
            if (!year.count(query.substr(3))) {
                printError();
                continue;
            } else
                v = year[query.substr(3)];
        }
        sort(v.begin(), v.end());
        for (const auto &s:v)
            cout << s << endl;
    }
    return 0;
}
### 关于 PAT 甲级真题 1172 的解析 PAT (Programming Ability Test) 是一项针对编程能力的测试,其中甲级难度较高,涉及算法设计与实现。以下是关于题目 **1172** 的相关内容。 #### 题目描述 根据已知的信息[^1]以及公开资源中的整理[^2],PAT 甲级真题 1172 被命名为《Digital Library》。该题目主要考察字符串处理、数据结构的应用以及输入输出优化等内容。具体要求如下: - 输入一系列书籍信息(包括书名、作者、关键词、出版社和出版年份),并按照指定条件查询匹配的结果。 - 查询方式为五种: 1. 按照书名查找; 2. 按照作者查找; 3. 按照关键词查找; 4. 按照出版社查找; 5. 按照出版年份查找。 每本书籍最多被查找到一次,即使它满足多个查询条件。 --- #### 解决方案概述 为了高效解决此问题,可以采用以下方法: ##### 数据存储 定义一个 `Book` 类来表示一本书的主要属性,便于统一管理书籍信息。代码示例如下: ```cpp struct Book { int id; // 图书编号 string title; // 书名 string author; // 作者 vector<string> tags; // 关键词列表 string publisher; // 出版社 int year; // 出版年份 }; ``` ##### 处理逻辑 通过构建多关键字索引来加速查询操作。对于不同的查询类型,别建立哈希表或其他快速检索的数据结构。例如: - 使用 `unordered_map<string, set<int>>` 存储按书名、作者、出版社等字段的映射关系。 - 对于关键词查询,则需遍历所有书籍逐一比较。 下面是完整的 C++ 实现代码片段: ```cpp #include <iostream> #include <vector> #include <string> #include <unordered_map> #include <set> using namespace std; // 定义图书结构体 struct Book { int id; string title; string author; vector<string> tags; string publisher; int year; }; int main() { int N, M; cin >> N; // 总共N本书 vector<Book> books(N); // 初始化书籍信息 for(int i=0;i<N;i++) { cin >> books[i].id >> books[i].title >> books[i].author >> books[i].tags.size(); // 假设先读取标签数量K books[i].tags.resize(books[i].tags.size()); for(auto &tag : books[i].tags){ cin >> tag; } cin >> books[i].publisher >> books[i].year; } cin >> M; // 查询次数M unordered_map<string, set<int>> titleMap, authorMap, pubMap; // 构建索引... } ``` 上述代码仅展示初始化部,实际还需补充详细的索引创建及查询功能。 --- #### 注意事项 在开发过程中需要注意以下几个方面: - 输入规模较大时应考虑性能优化,比如减少不必要的字符串拷贝操作。 - 输出格式严格遵循样例说明,任何多余的空格或换行都会导致评测失败。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

XdpCs

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

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

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

打赏作者

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

抵扣说明:

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

余额充值