#include <iostream>
#include <set>
#include <vector>
class InterestTags {
private:
std::vector<std::set<std::string>> userInterestSets;
public:
void addUserInterestSet(const std::set<std::string>& set) {
userInterestSets.push_back(set);
}
std::set<std::string> getIntersection() const {
if (userInterestSets.empty()) {
return std::set<std::string>();
}
std::set<std::string> intersection = userInterestSets[0];
for (size_t i = 1; i < userInterestSets.size(); ++i) {
std::set<std::string> temp;
std::set_intersection(intersection.begin(), intersection.end(),
userInterestSets[i].begin(), userInterestSets[i].end(),
std::inserter(temp, temp.begin()));
intersection = temp;
}
return intersection;
}
std::set<std::string> getUnion() const {
std::set<std::string> unionSet;
for (const auto& set : userInterestSets) {
unionSet.insert(set.begin(), set.end());
}
return unionSet;
}
};
性能问题处理
- 交集计算:当前使用
std::set_intersection
进行交集计算,时间复杂度为$O(n log n)$,其中$n$是集合中元素的总数。如果用户集合数量非常大,可以考虑使用更高效的数据结构,如std::unordered_set
(前提是标签类型支持哈希),其平均时间复杂度为$O(n)$。
- 并集计算:当前实现通过依次插入每个集合的元素到结果集中,时间复杂度为$O(n log n)$,其中$n$是集合中元素的总数。如果元素数量非常大,同样可以考虑使用
std::unordered_set
,平均时间复杂度可以优化到$O(n)$。
- 空间复杂度:当前代码存储了每个用户的兴趣标签集合,如果用户数量和标签数量非常大,空间消耗可能成为问题。可以考虑使用更紧凑的数据结构或进行数据压缩。