求圈子用户数,竟然超过时间限制(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;
}

原文地址:https://www.cnblogs.com/liuzc/p/6544733.html