打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
1022 Digital Library (30 分)python

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 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 title2: name of an author3: a key word4: name of a publisher5: 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.

这道题我使用python来写,和大家学习一下,首先我们先补充几个知识点判断字典的key是否存在我们使用dict.get(key)是返回1,不是返回0

我们还可以直接使用setdefault方法,如果key存在我们就什么都不做,如果key不存在就创建一个key

要知道value是可以一个列表,所以我们将符合这个要搜索的添加到这个列表中


逻辑分析:
首先我们创建一个列表字典,及有多个字典类型的列表,我们将搜索的关键字作为键(key)所映射出的对应书的ID号,这样我们只要访问关键字就可以得到对应的结果。因为每本书都有六个标签,所以我们创建五个字典。代表五种属性。

当我们每一本输入,就将对应的属性中添加书的特性并映射书名,比如输入
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

保存在字典里的数据是这样的
1: a book title
{‘The Testing Book’: [‘1111111’, ‘2222222’], ‘Another Testing Book’: [‘3333333’]}

2: name of an author
{‘Yue Chen’: [‘1111111’, ‘3333333’], ‘CYLL’: [‘2222222’]}

3: a key word
{‘test’: [‘1111111’, ‘3333333’], ‘code’: [‘1111111’, ‘3333333’], ‘debug’: [‘1111111’, ‘2222222’], ‘sort’: [‘1111111’, ‘3333333’], ‘keywords’: [‘1111111’, ‘3333333’, ‘2222222’], ‘book’: [‘2222222’]}

4: name of a publisher
{‘ZUCS Print’: [‘1111111’], ‘ZUCS Print2’: [‘3333333’, ‘2222222’]}

5: a 4-digit number representing the year
{‘2011’: [‘1111111’, ‘2222222’], ‘2012’: [‘3333333’]}

我们处理a key word需要单独判断,因为一本书可能有不同的关键词,不像其他属性只有唯一个,我们使用split将关键分开。

最后我将要输出的ID号先排序,再逐个输出。

n = int(input(""))data = [{}, {}, {}, {}, {}]#数据存放for i in range(n):#输入书籍    id = input("")#书籍的id号    for j in range(5):#书籍对应的属性        belong = input("")#书籍属性        if j == 2:#单独判断a key word ,其他属性对于书是唯一的,但写法一样。            for keys in belong.split(' '):#a key word分成若干关键字                try:                    data[j][keys].append(id)#存在将id添加对应关键字中                except:                    data[j].setdefault(keys, [id])#不存在就创建关键字            continue        try:            data[j][belong].append(id)#同上        except :            data[j].setdefault(belong,[id])            n=int(input(""))for i in range(n):#查找    find=input("")    print(find)    if(data[int(find[0])-1].get(find[3:])):#如果找到才输出        data[int(find[0])-1][find[3:]].sort()#先排序        for id in data[int(find[0])-1][find[3:]]:#逐个输出            print(id)    else:#找不到就输出Not Found        print("Not Found")


希望对大家有帮助吧

来源:https://www.icode9.com/content-1-849901.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
python 简易学生管理系统(使用函数、字典)
Selenium2 python自动化36-判断元素存在
标准C语言实现一个图书管理系统-C语言技术文档 - Firnow
python脚本模拟浏览器实现学习通自动刷网课
文本单词one-hot编码
DLPrinter.cab范例
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服