题解 | 小红书推荐算法
小红书推荐算法
https://www.nowcoder.com/practice/2408a65ee1c1433284fdbe1ab8c79245
#include <iostream>
#include <queue>
#include <unordered_set>
#include <map>
using namespace std;
int main() {
int n, q;
cin >> n >> q;
unordered_set<string> hash_set;
while (q > 0) {
string tmp;
cin >> tmp;
hash_set.insert(tmp);
--q;
}
map<int, vector<string>> aMap;
while (n > 0) {
int cnt = 0;
string name;
int num_words = 0;
cin >> name >> num_words;
while (num_words > 0) {
string tmp;
cin >> tmp;
if (hash_set.find(tmp) != hash_set.end())
{
++cnt;
}
--num_words;
}
aMap[cnt].push_back(name);
--n;
}
for (auto iter = aMap.rbegin(); iter != aMap.rend(); ++iter) {
for (string& str : iter->second) {
cout << str << endl;
}
}
return 0;
}
// 64 位输出请用 printf("%lld")
map会根据key自动进行排序,value则设置为一个vector,这样可以保证关键词数量一致时,先输入的商品在前面


