PTA Digital Library

链接

https://pintia.cn/problem-sets/994805342720868352/problems/994805480801550336

题解

这题我用c++写的话,
最起码要150行+
但是py竟然用32行就实现了
太强了太强了
简单来说就是用一个dict(哈希)快速查找

代码

N = int(input())
dic = [{} for i in range(5)]
# title:0 author:1 puhlisher:2 year:3 key_words:4
for i in  range(N):
    index, title, author, key_words, publisher, year = [input() for i in range(6)]
    i=0
    for s in (title,author,publisher,year):
        if s in dic[i]:
            dic[i][s].append(index)
        else:
            dic[i][s]=[index]
        i=i+1
    for s in key_words.split():
        if s in dic[4]:
            dic[4][s].append(index)
        else:
            dic[4][s]=[index]
M = int(input())
for i in range(M):
    s = input()
    print(s)
    typ = int(s[0])
    s = s[3:]
    if typ==1: index=0
    if typ==2: index=1
    if typ==3: index=4
    if typ==4: index=2
    if typ==5: index=3
    if s in dic[index]:
        print(*sorted(dic[index][s]),sep='\n')
    else:
        print('Not Found')

猜你喜欢

转载自blog.csdn.net/FSAHFGSADHSAKNDAS/article/details/86572033