首页 > 试题广场 >

小红书推荐系统

[编程题]小红书推荐系统
  • 热度指数:3258 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
小红书有一个推荐系统,可以根据用户搜索的关键词推荐用户希望获取的内容。
现在给定小红的搜索记录(记录为分词后的结果),我们认为当一个单词出现的次数不少于3次时,该单词为“用户期望搜索的单词”,即称为关键词。请你根据小红的记录,输出小红的用户画像对应的所有关键词。

输入描述:
一行字符串,仅由小写字母和空格组成。代表小红的搜索记录。
字符串长度不超过100000。


输出描述:
小红所有的关键词。每行输入一个。你需要按照搜索频次从高到低输出。频次相同的,你需要按字典序升序输出。
示例1

输入

kou red game red ok who game red karaoke yukari kou red red nani kou can koukou ongakugame game

输出

red
game
kou
s = input().split()
dict_s = {}
for _ in s:
n = s.count(_)
if n > 2:
if _ in dict_s:
continue
dict_s[_] = n
list_s = sorted(dict_s.items(),key=lambda x:(-x[1],x[0]))
for _ in list_s:
print(_[0])
发表于 2025-12-17 17:22:13 回复(0)
import sys

for line in sys.stdin:
    a = line.split()
    strs=list(map(str,a))
    s_dict={}
    for s in strs:
        if s in s_dict:
            s_dict[s]+=1
        else: s_dict[s]=1

num_dict={}
for k,v in s_dict.items():
    if v>=3:
        if v not in num_dict:
            num_dict[v]=[k]
        else:
            num_dict[v].append(k)

for v in sorted(num_dict.keys())[::-1]:
    for s in sorted(num_dict[v]):
        print(s)
发表于 2025-09-14 19:00:47 回复(0)
s = input().split()

dic = {}
for item in s:
    if item not in dic.keys():
        dic[item] = 1
    else:
        dic[item] += 1
sorted_dic_keys = sorted(dic.keys(), key=lambda x:(-dic[x],x))

for key in sorted_dic_keys:
    if dic[key]>=3:
        print(key)
    else:
        break
发表于 2025-08-26 12:18:01 回复(0)
import sys
words =  input().strip().split()
words.sort()
out = {}
for word in words:
    if word not in out.keys():
        out[word] = 1
    else:
        out[word] =  out[word] + 1

outsort = sorted(out.items(),key = lambda x: x[1],reverse=True)
for o in outsort:
    if o[1] >=3:
        print(o[0])


发表于 2025-08-15 11:56:49 回复(0)