首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

PAT甲级-1022 Digital Library (30分)

  • 24-03-03 00:43
  • 4778
  • 11412
blog.csdn.net

点击链接PAT甲级-AC全解汇总

题目:
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 (≤10​4​​ ) 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 haracters without any white space, and the keywords are separated by exactly one pace;
  • 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

题意:
这道题不难,输入N本书,包括六个数据【id,标题,作者,关键词,出版社,出版年份】,然后对于后五个内容,查找相关的id。
只要熟练运用map和set,难点就只有把关键词区分开;
注意: 一开始后两个case报错,发现是id的的问题,题目要求都是7位的,所以前面要补0补齐7位。

我的代码:

#include
using namespace std;

bool flag_none=true;

void my_print_set(set<int> my_set)
{
    if(my_set.size())flag_none=false;
    for(auto it:my_set)printf("%07d\n",it);
}

int main()
{
    map<string,set<int> >title_to_ids;
    map<string,set<int> >author_to_ids;
    map<string,set<int> >key_to_ids;
    map<string,set<int> >publisher_to_ids;
    map<string,set<int> >year_to_ids;

    int N;
    //输入
    cin>>N;
    for(int i=0;i<N;i++)
    {
        //输入一本书的六个信息
        int id;
        string title,author,keys,publisher,year;
        cin>>id;
        getchar();
        getline(cin,title);
        getline(cin,author);
        getline(cin,keys);
        getline(cin,publisher);
        getline(cin,year);

        //存储五个信息到对应的集合,其中keys要分开存
        title_to_ids[title].insert(id);
        author_to_ids[author].insert(id);
        publisher_to_ids[publisher].insert(id);
        year_to_ids[year].insert(id);

        int pos=keys.find(" ");
        while(pos!=-1)
        {
            key_to_ids[keys.substr(0,pos)].insert(id);
            keys.erase(0,pos+1);
            pos=keys.find(" ");
        }
        key_to_ids[keys].insert(id);
    }

    //查询
    cin>>N;
    for(int i=0;i<N;i++)
    {
        int t;
        cin>>t;
        getchar();//':'
        getchar();//' '
        string str;
        getline(cin,str);
        cout<<t<<": "<<str<<endl;
        switch(t)
        {
        case 1://1.title
            {
                my_print_set(title_to_ids[str]);
                break;
            }
        case 2://2.author
            {
                my_print_set(author_to_ids[str]);
                break;
            }
        case 3://3.key
            {
                my_print_set(key_to_ids[str]);
                break;
            }
        case 4://4.publisher
            {
                my_print_set(publisher_to_ids[str]);
                break;
            }
        case 5://5.year
            {
                my_print_set(year_to_ids[str]);
                break;
            }
        }
        if(flag_none)cout<<"Not Found"<<endl;
        flag_none=true;
    }
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
注:本文转载自blog.csdn.net的邂逅模拟卷的文章"https://blog.csdn.net/qq_34451909/article/details/105363014"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top