Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- N_RESULTS = 5
- n = int(input())
- texts_dics = []
- for _ in range(n):
- text_dic = {}
- for word in input().split():
- word_hash = hash(word)
- text_dic[word_hash] = text_dic.get(word_hash, 0) + 1
- texts_dics.append(text_dic)
- m = int(input())
- queries_dic = {}
- for i in range(m):
- for word in input().split():
- word_hash = hash(word)
- if word_hash not in queries_dic:
- queries_dic[word_hash] = set()
- queries_dic[word_hash].add(i)
- def search_words(m, texts_dics, queries_dic):
- queries_texts_dic = {i: {} for i in range(m)}
- for text_i, text_dic in enumerate(texts_dics):
- for w in text_dic.keys():
- if w in queries_dic:
- for query_i in queries_dic[w]:
- queries_texts_dic[query_i][text_i] = \
- queries_texts_dic[query_i].get(text_i, 0) + text_dic[w]
- result = []
- for query_i in range(m):
- counts_inds = sorted([[-count, i]
- for i, count in queries_texts_dic[query_i].items()])
- result.append([i + 1 for _, i in counts_inds][:N_RESULTS])
- return result
- [print(' '.join([str(x) for x in inds])) for inds in search_words(m, texts_dics, queries_dic)]
Add Comment
Please, Sign In to add comment