首页 > 代码库 > 求圈子用户数,竟然超过时间限制(TLE)了_2
求圈子用户数,竟然超过时间限制(TLE)了_2
接上。
//以电影直接或间接关注A的所有用户,A是user,即关注A的 void followedUsers(unsigned int user_id, unsigned int hobby_id) { unsigned int tempSize = 0; vector<FollowRelation>::iterator iterRela = followRelationVec.begin(); for(;iterRela != followRelationVec.end(); iterRela++) { if(iterRela->userId == user_id && iterRela->hobbyId == hobby_id) { followedSet.insert(iterRela->followerId); } } if(followedSet.empty()) { return; } while(true) { tempSize = followedSet.size(); set<unsigned int>::iterator iterFollowed = followedSet.begin(); for(;iterFollowed != followedSet.end(); iterFollowed++) { vector<FollowRelation>::iterator iterRela2 = followRelationVec.begin(); for(;iterRela2 != followRelationVec.end(); iterRela2++) { if(iterRela2->userId == *iterFollowed && iterRela2->hobbyId == hobby_id) { followedSet.insert(iterRela2->followerId); } } } if(tempSize == followedSet.size()) { return; } } } /* 计算指定(用户,爱好)的圈子的用户数量*/ unsigned int CalcUserNum(unsigned int user_id, unsigned int hobby_id) { //判断指定的用户是否有指定的爱好 if(isExistCheck(user_id,hobby_id) == -1) { return 0; } //每次计算之前清空set if(!followingSet.empty()) { followingSet.clear(); } if(!followedSet.empty()) { followedSet.clear(); } if(!circleSet.empty()) { circleSet.clear(); } //计算指定(用户,爱好)的圈子的用户数量 followingUsers(user_id, hobby_id); followedUsers(user_id, hobby_id); //构造一个有序序列,包含两个序列中所有的不重复元素。重载版本使用自定义的比较操作。 std::set_union(followingSet.begin(), followingSet.end(), followedSet.begin(), followedSet.end(), insert_iterator<set<unsigned int> >(circleSet,circleSet.begin())); circleSet.insert(user_id);//圈子数包括自身 return circleSet.size(); } /* 清空所有数据*/ void Clear(void) { if(!followRelationVec.empty()) { followRelationVec.clear(); } if(!userHobbysMap.empty()) { userHobbysMap.clear(); } if(!followingSet.empty()) { followingSet.clear(); } if(!followedSet.empty()) { followedSet.clear(); } if(!circleSet.empty()) { circleSet.clear(); } return; }
void CExampleTest::TestCase01()
{
unsigned int hobby_id[1] = {2};
AddUser(11, 1, hobby_id);
AddUser(12, 1, hobby_id);
AddUser(13, 1, hobby_id);
CPPUNIT_ASSERT(0 == AddFollowInfo(12, 11, hobby_id[0]));
CPPUNIT_ASSERT(0 == AddFollowInfo(12, 13, hobby_id[0]));
CPPUNIT_ASSERT(2 == CalcUserNum(11, hobby_id[0]));
CPPUNIT_ASSERT(0 == AddFollowInfo(11, 12, hobby_id[0]));
CPPUNIT_ASSERT(0 == AddFollowInfo(11, 13, hobby_id[0]));
CPPUNIT_ASSERT(3 == CalcUserNum(11, hobby_id[0]));
return;
}
求圈子用户数,竟然超过时间限制(TLE)了_2
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。