当前位置: 首页>>代码示例>>C++>>正文


C++ unordered_set::size方法代码示例

本文整理汇总了C++中unordered_set::size方法的典型用法代码示例。如果您正苦于以下问题:C++ unordered_set::size方法的具体用法?C++ unordered_set::size怎么用?C++ unordered_set::size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在unordered_set的用法示例。


在下文中一共展示了unordered_set::size方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: ladderLength

 //If there is no '&' before set, the time cost will be tripled;
 int ladderLength(unordered_set<string>& bList, unordered_set<string>& eList, unordered_set<string>& wordList, int len)
 {
     if(bList.empty())
         return 0;
     if(bList.size() > eList.size())
         return ladderLength(eList, bList, wordList, len);
     unordered_set<string> tmpList;
     for(auto it=bList.begin(); it!=bList.end(); it++)
     {
         string cur = *it;
         for(int i=0; i<cur.size(); i++)
         {
             char c0 = cur[i];
             for(char c='a'; c<='z'; c++)
             {
                 cur[i]=c;
                 if(eList.count(cur)) return len+1;
                 if(wordList.count(cur))
                 {
                     tmpList.insert(cur);
                     wordList.erase(cur);
                 }
             }
             cur[i] = c0;
         }
     }
     return ladderLength(tmpList, eList, wordList, len+1);
 }
开发者ID:Hearen,项目名称:AlgorithmHackers,代码行数:29,代码来源:WordLadder.cpp

示例2: helper

 bool helper(unordered_set<string>& word1, unordered_set<string>& word2, unordered_set<string>& dict){
     if(word1.empty()) return 0;
     if(word1.size() > word2.size()) return helper(word2, word1, dict);
     for(auto v : word1) dict.erase(v);
     for(auto v : word2) dict.erase(v);
     
     ++res;
     bool reach = false;
     unordered_set<string> word3;
     for(auto it = word1.begin(); it != word1.end(); ++it){
         string word = *it;
         for(int i = 0; i < word.size(); ++i){
             char letter = word[i];
             for(int k = 0; k < 26; ++k){
                 word[i] = 'a'+k;
                 if(word2.count(word)){
                     reach = true;
                     return reach;
                 }
                 if(dict.count(word) && !reach){
                     word3.insert(word);
                 }
             }
             word[i] = letter;
         }
     }
     return reach || helper(word2, word3, dict);
 }
开发者ID:vincivans,项目名称:LeetCode,代码行数:28,代码来源:127wordLadder.cpp

示例3: buildTree

 bool buildTree(unordered_set<string> &forward, unordered_set<string> &backward, unordered_set<string> &dict, 
 unordered_map<string, vector<string>> &tree, bool reversed){
       if (forward.empty())
             return false;
       if (forward.size() > backward.size())
       	return buildTree(backward, forward, dict, tree, !reversed);
       for (auto &word : forward)
       	dict.erase(word);
       for (auto &word : backward)
       	dict.erase(word);
       unordered_set<string> nextLevel;
       bool done = false; // guarantee shortest ladder
       for (auto &it : forward){ //traverse each word in the forward -> the current level of the tree;
             string word = it;
             for (auto &c : word){
                   char c0 = c; //store the original;
                   for (c = 'a'; c <= 'z'; ++c) //try each case;
                   if (c != c0){ //avoid futile checking;
                         //using count is an accelerating method;
                         if (backward.count(word)){    // count is O(1) while isChangedByOne is O(size(word)* size(backward))
                               done = true;
                               //keep the tree generation direction consistent;
                               !reversed ? tree[it].push_back(word) : tree[word].push_back(it); 
                         }
                         else if (!done && dict.count(word)){
                               nextLevel.insert(word);
                               !reversed ? tree[it].push_back(word) : tree[word].push_back(it);
                         }
                   }
                   c = c0; //restore the word;
       	}
       }
       return done || buildTree(nextLevel, backward, dict, tree, reversed);
 }
开发者ID:wszk1992,项目名称:LeetCode-Team,代码行数:34,代码来源:126.+Word+Ladder+II.cpp

示例4: ladderLengthHelper

 int ladderLengthHelper(unordered_set<string>& words1, unordered_set<string>& words2, unordered_set<string>& wordList, int level) {
     if (words1.empty())
         return 0;
     if (words1.size() > words2.size())
         return ladderLengthHelper(words2, words1, wordList, level);
     unordered_set<string> words3;  // the new layer
     for (auto it = words1.begin(); it != words1.end(); ++it)
     {
         string word = *it;
         for (auto ch = word.begin(); ch != word.end(); ++ch)
         {
             char tmp = *ch;
             for (*ch = 'a'; *ch <= 'z'; ++(*ch))
             {
                 if (*ch == tmp)
                     continue;
                 if (words2.find(word) != words2.end())
                     return level + 1;
                 if (wordList.find(word) != wordList.end())
                 {
                     wordList.erase(word);
                     words3.insert(word);
                 }
             }
             *ch = tmp;
         }
     }
     return ladderLengthHelper(words2, words3, wordList, level + 1);  // put words2 first
 }
开发者ID:TakuyaKimura,项目名称:Leetcode,代码行数:29,代码来源:127.cpp

示例5: ladderLength

    int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) {
        if (beginWord.size() != endWord.size()) return 0;
        if (beginWord.empty() || endWord.empty()) return 1;
        if (wordList.size() == 0) return 0;

        int distance = 1;
        queue<string> queToPush, queToPop;
        queToPop.push(beginWord);
        while (wordList.size() > 0 && !queToPop.empty()) {
            while (!queToPop.empty()) {
                string str(queToPop.front());
                queToPop.pop();
                for (int i = 0; i < str.size(); i++) {
                    for (char j = 'a'; j <= 'z'; j++) {
                        if (j == str[i]) {
                            continue;
                        }
                        char temp = str[i];
                        str[i] = j;
                        if (str == endWord) return distance + 1;
                        if (wordList.count(str) > 0) {
                            queToPush.push(str);
                            wordList.erase(str);
                        }
                        str[i] = temp;
                    }
                }
            }
            swap(queToPush, queToPop);
            distance++;
        }
        return 0;
    }
开发者ID:Hangjun,项目名称:LeetCode_Haochen,代码行数:33,代码来源:127.cpp

示例6: searchwithDoubleBFS

// 双向BFS搜索
// --------                     ----------
// |       |                    |        |
// |words1 |  <---------------> | words2 |
// |       |                    |        |
// ---------                    ----------   
// 不断地从两端向中心搜索,直至搜索到中间单词temp在另一个词袋中
int searchwithDoubleBFS(unordered_set<string>& words1, unordered_set<string>& words2,
	unordered_set<string>& dict, int level) {
	
	if (words1.empty()) return 0;		
	if (words1.size() > words2.size()) 
		return searchwithDoubleBFS(words2, words1, dict, level);
	
	unordered_set<string> words3;
	for (auto it = words1.begin(); it != words1.end(); ++it) {
		string word = *it;
		for (size_t i = 0; i < word.size(); ++i) {
			int ch = word[i];
			for (int k = 0; k < 26; ++k) {
				int ch2 = 'a' + k;
				if (ch2 != ch) {
					string temp = word;
					temp[i] = ch2;
					if (words2.find(temp) != words2.end()) {
						return level+1;
					} else {
						if (dict.find(temp) != dict.end()) {
							dict.erase(temp);
							words3.insert(temp);
						}
					}
					
				}
			}
		}
	}
	return searchwithDoubleBFS(words3, words2, dict, level+1);
}
开发者ID:python27,项目名称:AlgorithmSolution,代码行数:39,代码来源:127+Word+Ladder_2.cpp

示例7: ladderLength

 int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) {
 	vector<string> candidates;
 	
 	if (valid_next(beginWord, endWord))
 	{
 		// Found it!
 		return 2;
 	}
     for (auto w_iter: wordList)
     {
     	if (valid_next(w_iter, beginWord))
     	{
     		candidates.push_back(w_iter);
     	}
     }
     if (candidates.empty()) return 0;
     
     int min_length = wordList.size() + 1;
     for (auto can_iter: candidates)
     {
     	wordList.erase(can_iter);
     	int from_here = ladderLength(can_iter, endWord, wordList);
     	if (from_here != 0 && from_here < min_length)
     	{
     		min_length = from_here;
     	}
     	wordList.insert(can_iter);
     }
     return (min_length == wordList.size() + 1)? 0: min_length + 1;
 }
开发者ID:devastating,项目名称:misc,代码行数:30,代码来源:word_ladder.cpp

示例8: bfs

    // looking for connection from both ends
    bool bfs(unordered_set<string> &forward, unordered_set<string> &backward, bool isForward, 
                unordered_set<string> &wordList, unordered_map<string, vector<string>> &transMap) {
        if (forward.empty() || backward.empty())
        {
            return false;
        }

        // always do bfs with less nodes
        if (forward.size() > backward.size())
        {
            return bfs(backward, forward, !isForward, wordList, transMap);
        }

        // remove added node in the wordList to avoid duplication
        unordered_set<string>::iterator it;
        for (it = forward.begin(); it != forward.end(); it++)
        {
            wordList.erase(*it);
        }

        for (it = backward.begin(); it != backward.end(); it++)
        {
            wordList.erase(*it);
        }

        unordered_set<string> nextlevel;
        bool isConnected = false;
        for (it = forward.begin(); it != forward.end(); it++)
        {
            string word = *it;
            for (int i = 0; i < word.size(); i++)
            {
                for (char ch = 'a'; ch <= 'z'; ch++)
                {
                    if (word[i] != ch)
                    {
                        // word is in forward list, str is in backward list
                        string str(word);
                        str[i] = ch;
                        if (backward.find(str) != backward.end())
                        {
                            isConnected = true;
                            connect(word, str, isForward, transMap);
                        }
                        else if (!isConnected && wordList.find(str) != wordList.end())
                        {
                            nextlevel.insert(str);
                            connect(word, str, isForward, transMap);
                        }
                    }
                }
            }
        }

        return isConnected || bfs(nextlevel, backward, isForward, wordList, transMap);
    }
开发者ID:Agrithom-Universe,项目名称:LeetCode-Sol-Res,代码行数:57,代码来源:126_Word_Ladder_II.cpp

示例9: ladderLength

    int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) {
//        int min = wordList.size()+3;
//        bool* visited = new bool[wordList.size()];
//        for (int i = 0; i < wordList.size(); ++i) {
//            visited[i] = false;
//        }
//        unordered_set<string>::iterator it;
//        
//        recusiveSearch(beginWord,endWord,wordList,visited,0,2,min);
//        delete visited;
//        return ((min == wordList.size()+3)?0:min);
        
        int min = 2;
        bool* visited = new bool[wordList.size()];
        for (int i = 0; i < wordList.size(); ++i) {
            visited[i] = false;
        }
        queue<string> searchQue;
        string curr = beginWord;
        searchQue.push(beginWord);
        unordered_set<string>::iterator it;
        
        int layer = 1;
        int layerRec = 1;
        int visitCount = 0;
        while (searchQue.size() != 0){

            while (visitCount < layerRec) {
                
                string word = searchQue.front();
                searchQue.pop();
                ++visitCount;
                if (isValidTrans(word, endWord)) {
                    return min;
                }
                
                for (int p = 0; p < (int)word.length(); p++) {
                    char letter = word[p];
                    for (int k = 0; k < 26; k++) {
                        word[p] = 'a' + k;
                        if (letter != word[p] && wordList.find(word) != wordList.end()) {
                            searchQue.push(word);
                            wordList.erase(word);
                            ++layer;
                        }
                    }
                    word[p] = letter;
                }
            }
            layerRec = layer;
            ++min;
        }
    
        
        return 0;
    }
开发者ID:xiaofeng94,项目名称:AlgorithmTrial,代码行数:56,代码来源:WordLadder.cpp

示例10: ladderLength

    int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList)
    {
        const int beginLen = beginWord.length();
        const int endLen = endWord.length();
        if (beginLen != endLen)
        {
            return 0;
        }
        if (beginLen == 0 || endLen == 0)
        {
            return 1;
        }
        if (wordList.size() == 0)
        {
            return 0;
        }

        int distance = 1;
        queue<string> queToPush, queToPop;
        queToPop.push(beginWord);
        while (wordList.size() > 0 && !queToPop.empty())
        {
            while (!queToPop.empty())
            {
                string str(queToPop.front());
                queToPop.pop();
                for (int curInd = 0; curInd < beginLen; curInd++)
                {
                    for (char curChar = 'a'; curChar <= 'z'; curChar++)
                    {
                        if (curChar == str[curInd])
                        {
                            continue;
                        }
                        char temp = str[curInd];
                        str[curInd] = curChar;
                        if (str == endWord)
                        {
                            return distance + 1;
                        }
                        if (wordList.count(str) > 0)
                        {
                            queToPush.push(str);
                            wordList.erase(str);
                        }
                        str[curInd] = temp;
                    }
                }
            }
            swap(queToPush, queToPop);
            distance++;
        }
        return 0;
    }
开发者ID:ShuaiHuang,项目名称:MyLeetCode,代码行数:54,代码来源:Solution.hpp

示例11: values

void values(unordered_set<int> &s,int a,int b)
{
    s.insert(a);
    s.insert(b);
    if(s.size()==1) return;
    int n;
    std::vector<int> src,dst;
    std::vector<int>::iterator iti,itj;
    std::unordered_set<int> v;
    size_t lastsz=0;
    while(true){
        copy(s.begin(),s.end(),std::back_inserter(src));

        for(iti=src.begin();iti!=src.end();++iti){
            for(itj=src.begin();itj!=src.end();++itj){
                n = *iti-*itj;
                if(n>0)
                    v.insert(n);
            }
        }
        /*
        cout<<"差值[原始]:";
        std::for_each(v.begin(),v.end(),[](int n){cout<<n<<'\t';});
        cout<<endl;
        */
        src.clear();
        copy(v.begin(),v.end(),std::back_inserter(dst));
        copy(s.begin(),s.end(),std::back_inserter(src));

        std::sort(dst.begin(),dst.end());
        std::sort(src.begin(),src.end());
        //unordered_set difference
        std::vector<int> tmp(a/(a-b)+1);
        iti = std::set_difference(dst.begin(),dst.end(),src.begin(),src.end(),tmp.begin());
        tmp.resize(iti-tmp.begin());
        /*
        cout<<"差值:";
        std::for_each(tmp.begin(),tmp.end(),[](int n){cout<<n<<'\t';});
        cout<<endl;
        */
        if(tmp.empty()) break;
        s.insert(*tmp.begin());
        if(s.size()==lastsz) break;
        lastsz = s.size();

        //std::for_each(v.begin(),v.end(),[&s](int n){cout<<n<<'\t';s.insert(n);});
        v.clear();
        src.clear();
        dst.clear();
    }
}
开发者ID:linghutf,项目名称:topcoder,代码行数:51,代码来源:addunique.cpp

示例12: us_isect

void us_isect(unordered_set<int> &out,
    	const unordered_set<int> &in1,
    	const unordered_set<int> &in2)
{
    out.clear();
    if (in2.size() < in1.size()) {
    	us_isect(out, in2, in1);
    	return;
    }
    for (unordered_set<int>::const_iterator it = in1.begin(); it != in1.end(); ++it)
    {
    	if (in2.find(*it) != in2.end())
    		out.insert(*it);
    }
}
开发者ID:alexandrutomescu,项目名称:MixedPerfectPhylogeny,代码行数:15,代码来源:draw_tree.cpp

示例13: ladderLength

//这题很有意思,不知道从哪里下手
//BFS
//不能这么循环,因为会删光了
int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList)
{
    if(beginWord.size() != endWord.size()) return 0;
    if(beginWord.empty() || endWord.empty()) return 0;

    queue<string> path;
    path.push(beginWord);
    int level = 1;
    wordList.erase(beginWord);
    while(wordList.size() >= 0 && !path.empty()){
        int len = path.size();
        for(int count = 0; count<len; count++){
            string curWord = path.front();
            path.pop();
            for(int i = 0; i<curWord.size(); i++){
                string tmp = curWord;
                for(char j = 'a'; j<='z'; j++){
                    if(tmp[i] == j) continue;
                    tmp[i] = j;
                    if(tmp == endWord) return level+1;
                    if(wordList.find(tmp) != wordList.end()) path.push(tmp);
                    wordList.erase(tmp);
                }
            }
        }
        level++;
    }
    return 0;
}
开发者ID:zzfan,项目名称:leetCode,代码行数:32,代码来源:ladderLength.cpp

示例14: wordBreak

    bool wordBreak(string s, unordered_set<string> &dict) {
        if(dict.size()==0) return false;

        int longestWord = 0;
        for(string word : dict) {
            longestWord = max(longestWord, (int)word.size());
        }

        vector<bool> dp(s.size()+1,false);
        dp[0]=true;

        for(int i=1; i<=s.size(); i++)
        {
            for(int j=i-1; j>=max(i-longestWord, 0); j--)
            {
                if(dp[j])
                {
                    string word = s.substr(j,i-j);
                    if(dict.find(word)!= dict.end())
                    {
                        dp[i]=true;
                        break; //next i
                    }
                }
            }
        }

        return dp[s.size()];
    }
开发者ID:imAArtist,项目名称:simIr,代码行数:29,代码来源:code_746.cpp

示例15: if

	vector<string> wordBreak(string s, unordered_set<string>& wordDict, int start)
	{
		vector<string> res;
		if (s.length() == 0 || wordDict.size() == 0){
			return res;
		}
		else{
			vector<string> tmp;
			unordered_set<string>::iterator it;;
			string sub;
			for (int i = start; i < s.length(); i++){
				sub = s.substr(start, i + 1 - start);
				it = wordDict.find(sub);
				if (it != wordDict.end()){
					if (i == s.length() - 1)
						res.push_back(sub);
					else{
						tmp = wordBreak(s, wordDict, i + 1);
						for (int j = 0; j < tmp.size(); j++){
							res.push_back(sub + " " + tmp.at(j));
						}
					}
				}
				else if (i == s.length() - 1)
					return res;
			}
			return res;
		}
	}
开发者ID:psc0606,项目名称:algorithm,代码行数:29,代码来源:WordBreakII.cpp


注:本文中的unordered_set::size方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。