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


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

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


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

示例1: 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

示例2: ladderLength

    int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) {

        queue<pair<string, int> > que;
        if (wordDict.find(beginWord) == wordDict.end()){
            return -1;
        }        

        que.push(make_pair(beginWord, 1));
        wordDict.erase(beginWord);

        while (!que.empty()){

            auto front = que.front();
            que.pop();

            if (front.first == endWord) return front.second;

            for (int i = 0; i < front.first.length(); i++){
                string tmp = front.first;
                for (char j = 'a'; j <= 'z'; j++){
                    tmp[i] = j;
                    if (wordDict.find(tmp) != wordDict.end()){
                        que.push(make_pair(tmp, front.second + 1));
                        wordDict.erase(tmp);
                    }
                }
            }

        }

        return 0;

    }
开发者ID:Lamzin,项目名称:LeetCode,代码行数:33,代码来源:127+Word+Ladder.cpp

示例3: 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

示例4: ladderLength

 int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) {
     unordered_map<string, int> hash;
     if (wordDict.find(beginWord) != wordDict.end()) wordDict.erase(beginWord);
     if (wordDict.find(endWord) == wordDict.end()) wordDict.insert(endWord);
     queue<string> q;
     q.push(beginWord);
     hash[beginWord] = 1;
     while (!q.empty()) {
         string cur = q.front();
         q.pop();
         if (cur == endWord) return hash[cur];
         int dis = hash[cur];
         for (int i = 0; i < cur.size(); i ++) {
             for (int j = 0; j < 26; j ++) {
                 char ch = 'a' + j;
                 string temp = cur;
                 temp[i] = ch;
                 if (temp != cur && wordDict.find(temp) != wordDict.end() && hash.find(temp) == hash.end()) {
                     hash[temp] = dis + 1;
                     wordDict.erase(temp);
                     q.push(temp);
                 }
             }
         }
     }
     return 0;
 }
开发者ID:jinmj1993,项目名称:Leetcode,代码行数:27,代码来源:Word-Ladder.cpp

示例5: ladderLength

	int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList)
	{
		if (beginWord == endWord) return 2;
		int depth = 1;
		unordered_set<string> forward, backward;
		forward.insert(beginWord);
		backward.insert(endWord);
		while (!forward.empty())
		{
			unordered_set<string> nextLevel;
			for (auto& w : forward) wordList.erase(w);
			for (auto& w : backward) wordList.erase(w);
			for (auto& word : forward)
			{
				string cur(word);
				for (auto& c : cur)
				{
					char c0 = c;
					for (c = 'a'; c <= 'z'; ++c)
					{
						if (c != c0)
						{
							if (backward.count(cur)) return depth + 1;
							if (wordList.count(cur)) nextLevel.insert(cur);
						}
					}
					c = c0;
				}
			}
			depth++;
			forward.swap(nextLevel);
			if (forward.size() > backward.size()) backward.swap(forward);
		}
		return 0;
	}
开发者ID:colinlee1999,项目名称:leetcode,代码行数:35,代码来源:127_3.cpp

示例6: ladderLength

    int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) {
        int len = beginWord.length(), level = 1, nodesATLevel;  // return the length of the ladder, not # transformation
        string str;
        char orig;
        deque<string> qStart, qEnd, *q1, *q2;
        unordered_set<string> startFrontier, endFrontier, *f1, *f2;

        qStart.push_back(beginWord);
        qEnd.push_back(endWord);
        startFrontier.insert(beginWord);
        endFrontier.insert(endWord);
        wordList.erase(beginWord);  // mark every visited node by removing it from the dict
        wordList.erase(endWord);
        while (!qStart.empty() && !qEnd.empty())
        {
            ++level;
            if (qStart.size() < qEnd.size())
            {
                q1 = &qStart;  // iterate through q1, to match strings in q2
                q2 = &qEnd;
                f1 = &startFrontier;
                f2 = &endFrontier;
            }
            else
            {
                q1 = &qEnd;
                q2 = &qStart;
                f1 = &endFrontier;
                f2 = &startFrontier;
            }
            f1->clear();  // pointer notation
            nodesATLevel = q1->size();
            for (int i = 0; i < nodesATLevel; ++i)
            {
                str = q1->front();
                q1->pop_front();
                for (int j = 0; j < len; ++j)
                {
                    orig = str[j];
                    for (char c = 'a'; c <= 'z'; ++c)
                    {
                        //if (c == orig)  // if start == end, length should be 1 or 2??
                        //    continue;
                        str[j] = c;
                        if (f2->find(str) != f2->end())
                            return level;
                        if (wordList.find(str) != wordList.end())
                        {
                            q1->push_back(str);
                            f1->insert(str);
                            wordList.erase(str);  // mark as visited
                        }
                    }
                    str[j] = orig;
                }
            }
        }
        return 0;
    }
开发者ID:TakuyaKimura,项目名称:Leetcode,代码行数:59,代码来源:127.cpp

示例7: findLadders

    vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dic) {
      dic.erase(start); dic.erase(end);
      vector<string> dict(dic.begin(), dic.end());
      dict.push_back(end);
      dict.insert(dict.begin(), start);
      unordered_map<string, vector<int> > map;

      int starti = 0, endi = dict.size() - 1;

      for (int i = 1; i < dict.size(); i++) {  /* record the variations of every word in dict */
          string tmp = dict[i];
          for (int j = 0; j < tmp.length(); j++) {
            tmp[j] = '\0';
            if (map.find(tmp) == map.end())
              map[tmp] = vector<int>();
            map[tmp].push_back(i);
            tmp[j] = dict[i][j];
          }
      }

      queue<TransNode*> trans;
      vector<vector<string> > res;
      vector<int> set(dict.size(), -1);
      trans.push(new TransNode(starti));
      set[starti] = 0;

      while (!trans.empty()) {
        TransNode *tran = trans.front(); trans.pop();
        int tmpi = tran->str;
        string tmp = dict[tmpi];

        if (!res.empty() && tran->lvl >= res[0].size()) /* the current level is greater than the shortest path's length */
          break;

        if (tmp == end) {   /* meet another shortest path, add to result */
          res.push_back(vector<string>(tran->lvl+1));
          for (TransNode *t = tran; t; t = t->parent)
            res.back()[t->lvl] = dict[t->str];
          continue;
        }

        for (int i = 0; i < tmp.length(); i++) {
          tmp[i] = '\0';
          for (auto s = map[tmp].begin(); s != map[tmp].end(); s++) {
            if (dict[*s] == dict[tmpi])
              continue;
              
            if (set[*s] == -1 ||  set[*s] == tran->lvl+1) {
              trans.push(new TransNode(*s, tran));
              set[*s] = tran->lvl+1;
            }
          }

          tmp[i] = dict[tmpi][i];
        }
      }

      return res;
    }
开发者ID:jizhilong,项目名称:leetcodeoj-solutions,代码行数:59,代码来源:word_ladder2.cpp

示例8: ladderLength

 int ladderLength(string start, string end, unordered_set<string> &dict) {
     // IMPORTANT: Please reset any member data you declared, as
     // the same Solution instance will be reused for each test case.
     
     queue<string> queueToPop;
     queue<string> queueToPush;
     int           distance  = 1;
     
     dict.erase (start);
     dict.erase (end);
     
     queueToPush.push (start);
     
     while (queueToPush.size () > 0)
     {
         swap (queueToPop, queueToPush);
         vector<string> toBeDelete;
         
         while (queueToPop.size () > 0)
         {
             string current = queueToPop.front ();
             
             queueToPop.pop ();
             
             for (int i = 0; i < current.size (); i++)
             {
                 for (char c = 'a'; c <= 'z'; c++)
                 {
                     if (current[i] != c)
                     {
                         string temp = current;
                     
                         temp[i] = c;
                         
                         if (temp.compare (end) == 0)
                         {
                             return (distance + 1);
                         }
                         else if (dict.count (temp) > 0)
                         {
                             queueToPush.push (temp);
                             dict.erase (temp);
                         }
                     }
                 }
             }
         }
         
         for (auto s : toBeDelete)
         {
             dict.erase (s);
         }
         
         distance++;
     }
     
     return 0;
 }
开发者ID:zzqcraft,项目名称:leetcode,代码行数:58,代码来源:Word+Ladder.cpp

示例9: ladderLength

    int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) 
    {
		if(wordDict.empty())
			return 0;
		stepWord *tempStep = new stepWord();
		tempStep->word = beginWord;
		tempStep->step = 1;
        queue<stepWord*> sBegin;		
		sBegin.push(tempStep);
		stepWord *tempStepend = new stepWord();
		tempStepend->word = endWord;
		tempStepend->step = 1;
		queue<stepWord*> sEnd;
		sEnd.push(tempStepend);
		stepWord *ptrBegin;
		stepWord *ptrEnd;
		while(!sBegin.empty() || !sEnd.empty())
		{
			if(!sBegin.empty())
			{
				ptrBegin = sBegin.front();
				sBegin.pop();
			}
			if(!sEnd.empty())
			{
				ptrEnd = sEnd.front();
				sEnd.pop();
			}
			cout << "Õ»¶¥µ¥´Ê£º" << ptrBegin->word << "\t" << ptrEnd->word <<endl;
			if(isSimilar(ptrBegin->word, ptrEnd->word))
				return ptrBegin->step + ptrEnd->step;						
			if(wordDict.empty())
				continue;
			for(unordered_set<string>::iterator p = wordDict.begin(); p != wordDict.end();)
			{
				if(isSimilar(*p, ptrBegin->word))
				{
					stepWord *tempStep = new stepWord();
					tempStep->word = *p;
					tempStep->step = ptrBegin->step + 1;
					sBegin.push(tempStep);
					p = wordDict.erase(p);
				}
				else if(isSimilar(*p, ptrEnd->word))
				{
					stepWord *tempStep = new stepWord();
					tempStep->word = *p;
					tempStep->step = ptrEnd->step + 1;
					sEnd.push(tempStep);
					p = wordDict.erase(p);
				}
				else
					++ p;
			}
		}
		return 0;
    }
开发者ID:Wulala1119,项目名称:myLeetCode,代码行数:57,代码来源:127_WordLadder.cpp

示例10: ladderLength

int ladderLength(string beginWord, string endWord, unordered_set<string>& dict) {
	if (beginWord == endWord) return 1;
	unordered_set<string> words1, words2;
	words1.insert(beginWord);
	dict.erase(beginWord);
	words2.insert(endWord);
	dict.erase(endWord);
	return searchwithDoubleBFS(words1, words2, dict, 1);
}
开发者ID:python27,项目名称:AlgorithmSolution,代码行数:9,代码来源:127+Word+Ladder_2.cpp

示例11: 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

示例12: findLadders

    vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict){
        dict.emplace(start);
        dict.emplace(end);
        unordered_map<string, vector<string>> from;
        queue<string> que;
        que.push(start);
        dict.erase(start);

        int endLevel = -1;
        int level = 0;

        while(!que.empty()){
            if(endLevel != -1) break;
            int size = que.size();
            unordered_set<string> toPush;//this is acctually what the next level is
            for(int ttt = 0; ttt < size; ttt++){
                string front = que.front();
                if(front == end){
                    endLevel = level;
                    break;
                }

                for(unsigned int i = 0; i < front.size(); i++){
                    string neighbor = front;
                    for(int j = 0; j < 26; j++){
                        neighbor[i] = (char)((int)'a' + j);
                        if(neighbor == front) continue;

                        if((dict.count(neighbor) != 0)){
                            from[neighbor].push_back(front);
                            toPush.emplace(neighbor);
                        }
                    }
                }
                que.pop();
            }
            level++;
            for(unordered_set<string>::iterator itr = toPush.begin(); itr != toPush.end(); itr++){
                string temp = *itr;
                que.push(temp);
                dict.erase(temp);
                //every time after next level is determined, erase them from dict
                //since they won't be at the next next level(we need to exclude them from the future check)
            }
        }

        vector<vector<string>> result;
        if(from[end].size() == 0) return result;
        vector<string> tempRes;

        tempRes.push_back(end);
        help(result, tempRes, endLevel, start, end, from);
        return result;
    }
开发者ID:hellodada,项目名称:LeetCode,代码行数:54,代码来源:126_eyan.cpp

示例13: findLadders

   vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
        vector<vector<string> > ret;
        unordered_map<string,string> previous;
     
        queue<pair<string,int> > coada;
        coada.push(make_pair(start,0));
		dict.erase(start);
		dict.erase(end);
        if(start==end && dict.empty()==true ) 
            return ret;
        pair<string,int> curent;
		previous[start]="###";
        int n;
		int lungime = 0;
        while(coada.empty()==false){
			curent = coada.front();
			coada.pop();
			if(lungime !=0 ){
				if(curent.second+1 !=lungime) break;
			}
            n = curent.first.length()-1;
            string aux = curent.first;
            char c ;
            for(int i=0;i<=n;++i){
                c = aux[i];
                for(int j='a';j<='z';++j){
                    aux[i]=j;
                    if(curent.first == aux) continue;
                    if(aux==end){
                        vector<string> forret;
                        forret.push_back(end);
						forret.push_back(curent.first);
						string prev = previous[curent.first];
						
						while(prev!="###"){
							forret.push_back(prev);
							prev = previous[prev];
						}
						reverse(forret.begin(),forret.end());
						ret.push_back(forret);
						lungime = curent.second+1;
                    }else
                    if(dict.find(aux)!=dict.end()){
                        coada.push(make_pair(aux,curent.second+1));
						previous[aux]=curent.first;
						dict.erase(aux);
                    }
                }
				 aux[i] = c;
            }
        }
		
        return ret;
    }
开发者ID:contrapct,项目名称:onlinej,代码行数:54,代码来源:wordladder2.cpp

示例14: ladderLength

 int ladderLength(string start, string end, unordered_set<string> &dict) {
     // Start typing your C/C++ solution below
     // DO NOT write int main() function
     if(start.size()!=end.size() || dict.empty())
         return 0;
     //dict.insert(end);
     queue<string> q[2];
     int cur = 0;
     q[cur].push(start);
     int length = 0;
     bool find = false;
     while(!q[cur].empty())
     {
         while(!q[cur].empty())
         {
             string s = q[cur].front();
             q[cur].pop();
             int ssize = s.size();
             for(int i = 0; i < ssize && !find; i++)
             {
                 string tmp = s;
                 for(int j = 'a'; j <= 'z'; j++)
                 {
                     tmp[i] = j;
                     if(tmp == s)
                     {
                         dict.erase(s);
                         continue;
                     }
                     if(tmp == end && length != 0)
                     {
                         find = true;
                         break;
                     }
                     if(dict.find(tmp) != dict.end())
                     {
                         if(tmp == end)
                         {
                             find = true;
                             break;
                         }
                         q[cur^1].push(tmp);
                         dict.erase(tmp);
                     }
                 }
             }
             if(find)
                 return length+2;
         }
         cur ^= 1;
         length++;
     }
     return 0;
 }
开发者ID:Abocer,项目名称:leetcode-1,代码行数:54,代码来源:word-ladder.cpp

示例15: ladderLength

int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict)
{
	unordered_set<string> s1;
	unordered_set<string> s2;
	s1.insert(beginWord);
	s2.insert(endWord);
	wordDict.erase(beginWord);
	wordDict.erase(endWord);

	return ladderLengthHelper(s1,s2,wordDict,2);

}
开发者ID:Khrylx,项目名称:Leetcode-Medium-2,代码行数:12,代码来源:127+Word+Ladder.cpp


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