1022. Digital Library (30)-PAT甲级真题

本文介绍了一个基于图书多种属性如标题、作者、关键词等进行检索的系统实现方案。使用C++结合map和set数据结构,有效地实现了图书信息的存储与查询功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 (<=10000) 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

  • 分析
    这道题就是输入每本图书的作者啊名字啊年份啥的信息,还有借书人的id,最后就是让你通过图书各种属性都行,查找到这本书借的人。然后研究了大神的做法,其实就是用map,每一个属性都是一个map,列名是这个属性的值,然后其内容就是一个set集合包含着借阅的人的id。
#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <string>
using namespace std;
map<string , set<int> > title , author , key , pub , year;
void query (map< string , set<int> > &m , string &str)
{
    if (m.find (str) != m.end ())
    {
        for (set<int>::iterator 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_s ("%d" , &n);
    string ttitle , tauthor , tkey , tpub , tyear;
    for (int i = 0; i < n; i++)
    {
        scanf_s ("%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;
            c = getchar ();
            if (c == '\n') break;
        }
        getline (cin , tpub);
        pub[tpub].insert (id);
        getline (cin , tyear);
        year[tyear].insert (id);
    }
    scanf_s ("%d" , &m);
    for (int i = 0; i < m; i++)
    {
        scanf_s ("%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;
}
### 关于 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值