PAT(甲)1022 Digital Library (30)(30 分)

本文介绍了一个基于图书信息的检索系统实现方法,该系统能够根据读者提供的查询条件,如书名、作者、关键词等,返回符合条件的图书ID列表,并按照ID进行升序排序。

1022 Digital Library (30)(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.


  • 输入格式
    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

  • 输出格式
    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.

题目大意:
题目就是输入书籍的信息,然后按照要求查找符合搜索条件的相应的书籍ID,并按ID升序输出

解题方法:
这里我是用一个结构体,然后把保存的对象按ID升序排列,最后就是按条件的一个简单查找过程,题目逻辑比较简单,但需要注意几个易错点。


易错点:
1. 输出ID不足7位前面需要补0
2. 对于结构体数组的创建的声明,不能写在主函数中,否则最后一个测试样例会通不过


程序:

#include <cstdio>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;

struct Info
{
    char title[200], autor[200], key[200], pub[200], year[200];
    int ID;
}infos[10001];

bool cmp(Info in1, Info in2)
{
    return in1.ID < in2.ID;
}

int main(int argc, char const *argv[])
{
    int N, M, search, flag;
    char searchInfo[200];
    scanf("%d", &N);
    for (int i = 0; i < N; i++)
    {   /* 输入存储信息 */
        scanf("%d", &infos[i].ID);
        getchar();
        fgets(infos[i].title, 200, stdin);   infos[i].title[strlen(infos[i].title) - 1] = '\0';
        fgets(infos[i].autor, 200, stdin);   infos[i].autor[strlen(infos[i].autor) - 1] = '\0';
        fgets(infos[i].key, 200, stdin);     infos[i].key[strlen(infos[i].key) - 1] = '\0';
        fgets(infos[i].pub, 200, stdin);     infos[i].pub[strlen(infos[i].pub) - 1] = '\0';
        scanf("%s", infos[i].year);     
    }
    sort(infos, infos+N, cmp);  /* 按ID排序 */
    scanf("%d", &M);
    getchar();
    for (int k = 0; k < M; k++)
    {
        scanf("%d: ", &search);
        fgets(searchInfo, 200, stdin);  searchInfo[strlen(searchInfo) - 1] = '\0';
        printf("%d: %s\n", search, searchInfo);
        flag = 0;
        switch(search)
        {
            case 1: 
                for (int i = 0; i < N; i++)
                    if (strcmp(infos[i].title, searchInfo) == 0){
                        printf("%07d\n", infos[i].ID);
                        flag = 1;
                    }
                break;
            case 2:
                for (int i = 0; i < N; i++)
                    if (strcmp(infos[i].autor, searchInfo) == 0){
                        printf("%07d\n", infos[i].ID);
                        flag = 1;
                    }
                break;         
            case 3:
                for (int i = 0; i < N; i++){
                    char *c;
                    c = strstr(infos[i].key, searchInfo);
                    if (c){
                        printf("%07d\n", infos[i].ID);
                        flag = 1;
                    }
                }
                break;
            case 4:
                for (int i = 0; i < N; i++)
                    if (strcmp(infos[i].pub, searchInfo) == 0){
                        printf("%07d\n", infos[i].ID);
                        flag = 1;
                    }
                break;
            case 5:
                for (int i = 0; i < N; i++)
                    if (strcmp(infos[i].year, searchInfo) == 0){
                        printf("%07d\n", infos[i].ID);
                        flag = 1;
                    }
                break;
        }
        if (flag == 0)
            printf("Not Found\n");
    }
    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、付费专栏及课程。

余额充值