• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ unordered_set类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: findLadders

 vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
     dict.insert(end);
     pre.clear();
     len.clear();
     queue<string> q;
     q.push(start);
     len[start] = 0;
     while(!q.empty()) {
         string s = q.front(), t = s;
         int d = len[s];
         q.pop();
         if (t == end)
             break;
         for(int i = 0; i < s.length(); i++) {
             int k = s[i]-'a';
             for(int j = 0; j < 26; j++) {
                 if( j != k) {
                     s[i] = 'a'+j;
                     if (dict.count(s)>0) {
                         if (len.count(s) == 0) {
                             len[s] = d+1;
                             pre[s].push_back(t);
                             q.push(s);
                         } else if(len[s] == d+1) {
                             pre[s].push_back(t);
                         }
                     }
                 }
             }
             s[i] = 'a'+k;
         }
     }
     ans.clear();
     tmp.assign(1, end);
     build(start, end);
     return ans;
 }
开发者ID:sdanyalk,项目名称:interview,代码行数:37,代码来源:126_WordLadderII.cpp


示例2: explore

    /*
     * Recursivly explores the game board and add any solutions to validWords collection.
     */
    void Boggle::explore(string word, int x, int y, unordered_set<int>& visited){

        //Add letter contained in this cell to word.
        word.push_back(board.get(y, x));

        //If our word is a valid prefix it is worth to continue, otherwise we return.
        if(lexicon.containsPrefix(word)){

            //If word is acceptable we add it to validWords collection.
            if(word.length() >= MIN_WORD_LENGTH && !alreadyFound(word) && lexicon.contains(word)){

                cpuScore += scoreFor(word);
                cpuWords.insert(word);
            }

            //Convert coordinates to index in order to more easily store them as visited.
            int currentIndex = x + y * BOARD_WIDTH;
            visited.insert(currentIndex);

            //Check all possible neighbours, exploring those who are withing bounds.
            for(int ny = y - 1; ny <= y + 1; ny++){
                for(int nx = x - 1; nx <= x + 1; nx++){
                    if(board.inBounds(ny, nx)){
                        if(visited.count(nx + ny * BOARD_WIDTH) == 0){

                                explore(word, nx, ny, visited);
                        }
                    }
                }
            }

            //Erase cell from visited collection as we are backtracking.
            visited.erase(currentIndex);
        }

        return;
    }
开发者ID:Trites,项目名称:TDDD86,代码行数:40,代码来源:Boggle.cpp


示例3: ladderLength

    int ladderLength(string start, string end, unordered_set<string> &dict) {
        deque<string> pop_from;
        deque<string> push_to;
        int total_dist = 1;
        // init
        pop_from.push_back(start);

        while(!pop_from.empty()) {
            while (!pop_from.empty()) {
                string& orig = pop_from.front();
                string node = pop_from.front();
                int dist = 0;
                for (int i = 0; i < node.length() && dist < 2; i++) {
                    if (node[i] != end[i]) {
                        ++dist;
                    }
                }
                if (dist <= 1) {
                    return total_dist + dist;
                }
                for (int i = 0; i < orig.length(); ++i) {
                    node = orig;
                    for (char j = 'a'; j <= 'z'; ++j) {
                        node[i] = j;
                        if (dict.count(node) > 0) {
                            push_to.push_back(node);
                            dict.erase(node);
                        }
                    }
                }
                pop_from.pop_front();
            }
            pop_from.swap(push_to);
            ++total_dist;
        }
        return 0;
    }
开发者ID:kmiku7,项目名称:leetcode,代码行数:37,代码来源:solution_optimize_3.cpp


示例4: wordBreak

		bool wordBreak(string s, unordered_set<string> &dict) {
			int len = (int)s.size();
			int *st = new int[len+1];

			/*
			printf("%s\n", s.c_str());
			for (unordered_set<string>::iterator usit = dict.begin(); usit != dict.end(); ++usit)
			{
				printf("%s\n", usit->c_str());
			}
			*/	

			memset(st, 0, sizeof(int) * (len+1));
			st[0] = 1;
			unordered_set<string>::iterator usit = dict.begin();

			for (int i = 1; i <= len; ++i)
			{
				for (usit = dict.begin(); usit != dict.end(); ++usit)
				{
					int cur_len = (int)usit->size();
					int dis = i - cur_len;
					if (dis < 0) continue; //too short, impossible
					const char *start = s.c_str() + dis;

					if (st[dis] == 1 && strncmp(start, usit->c_str(), cur_len) == 0)
					{
						st[i] = 1;
						break;
					}
				}
			}

			bool res = st[len] == 1;
			delete[] st;
			return res;
		}
开发者ID:ak638,项目名称:leetcode,代码行数:37,代码来源:WordBreak.cpp


示例5: WordBreak

	void WordBreak(string s, string curStr, unordered_set<string> & dict, unordered_set<string>& cache, vector<string> & res)
	{
		if (s.length() == 0)
		{
			res.push_back(curStr);
			return;
		}
		else
		{
			for (int i = 1; i <= s.length(); i++)
			{
				string str = s.substr(0, i);
				if (dict.count(str) != 0 && cache.count(s.substr(i)) == 0)
				{
					int size = res.size();
					WordBreak(s.substr(i), curStr.length() > 0 ? curStr + " " + str : str, dict, cache, res);
					if (res.size() == size)
					{
						cache.insert(s.substr(i));
					}
				}
			}
		}
	}
开发者ID:qingchen021,项目名称:Leetcode,代码行数:24,代码来源:Word+Break+II.cpp


示例6: wordBreak

        vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
            int len = s.size();
            vector<bool> canBreak(len+1);
            vector<int> idxes;
            idxes.push_back(0);
            canBreak[0] = true;
            for (int i=1; i<=len; ++i)
                for (int j=i-1; j>=0; --j)
                    if (canBreak[j] && wordDict.count(s.substr(j, i-j))) {
                        canBreak[i] = true;
                        idxes.push_back(i);
                        break;
                    }
            if (!canBreak[len]) return {};

            int idxLen = idxes.size();
            vector<vector<string>> breaks(idxLen+1);
            breaks[0].push_back({""});
            string word;
            int idx1, idx2;
            for (int i=1; i<=idxLen; ++i) {
                for (int j=0; j<i; ++j) {
                    idx1 = idxes[j]; idx2 = idxes[i];
                    word = s.substr(idx1, idx2-idx1);
                    if (wordDict.count(word))
                        for (auto &w : breaks[j])
                            breaks[i].push_back(w + word + ' ');
                }
            }

            vector<string> res;
            for (auto &w : breaks[idxLen]) {
                res.push_back(w.substr(0, w.size()-1));
            }
            return res;
        }
开发者ID:Sean-Lan,项目名称:leetcode,代码行数:36,代码来源:wordBreakII.cpp


示例7: wordBreak

    bool wordBreak(string s, unordered_set<string> &dict) {

        if(  s.length() == 0 ){

            return true;
        }
        bool flag[1000];
        memset(flag, false,sizeof(flag));
        flag[0] = true;

        for( int i = 0 ; i < s.length(); i++){

            for( int j = 0; j <= i; j++){

                if( flag[j] && ( dict.find( s.substr(j,i +1 - j)) != dict.end())){
                    flag[i + 1] = true;

                }
            }
        }

        return flag[s.length()];

    }
开发者ID:nhzc123,项目名称:leetcode,代码行数:24,代码来源:WordBreak.cpp


示例8: wordBreak

 bool wordBreak(string s, unordered_set<string>& wordDict) {
     int n = s.size();
     if (n == 0) return false;
     vector<bool> dp(n + 1, false); // dp[i+1] : 0~i is true
     dp[0] = true;
     for (int i = 0; i < n; i++) {
         for (int j = i; j >= 0; j--) {
             if (dp[j] && wordDict.count(s.substr(j, i-j+1)) > 0) {
                 dp[i+1] = true;
                 break; // no need for further steps
             }
         }
     }
     return dp[n];
 }
开发者ID:WayneDW,项目名称:leetCode,代码行数:15,代码来源:139_Word_Break.cpp


示例9: bfs

int bfs(string start, string end, unordered_set<string> &dict, unordered_map<string,int> &level) 
{
	int n = dict.size();
	queue<string> q;
	q.push(start);
	int dist = 0;
	queue<string> q1;
	level[start] = 0;
	while(q.size() || q1.size())
	{
		if (q.size() == 0)
		{
			dist++;
			q = q1;
			q1 = queue<string>();
		}
		string str = q.front();
		q.pop();
		if (str == end)
			return dist;
		for ( int i = 0; i < str.size(); i++)
			for ( char c = 'a'; c <= 'z'; c++)
			{
				if (str[i] == c) continue;
				string str1 = str;
				str1[i] = c;
				if ( (dict.count(str1) || str1 == end ) && !level.count(str1))
				{
					q1.push(str1);
					level[str1] = dist + 1;
				}

			}	
	}
	return 0;
}
开发者ID:wny1990,项目名称:LeetCode-in-Cpp,代码行数:36,代码来源:word_ladder_2.cpp


示例10: findLadders

    vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) 
    {
        vector<vector<string> > result;

        map<string,Node*> nodes;
        Node nStart;
        nStart.name = start;

        Node nEnd;
        nEnd.name = end;
        nodes[start] = &nStart;
        for (std::unordered_set<string>::iterator i = dict.begin(); i != dict.end(); ++i)
        {
            Node* tmp = new Node();
            tmp->name = *i;
            nodes[*i] = tmp;
        }
        nodes[end] = &nEnd;
        dict.insert(end);
        dict.insert(start);


        nodes[start]->val = 1;
        innerSearch(nodes[start],dict,nodes);
        int minValue = nodes[start]->val == INT_MAX ? 0 : nodes[end]->val; 
        if ( minValue > 0 )
        {
            //printf("size:%ld\n", nodes[start]->nexts.size());
            vector<string> parent;
            parent.push_back(start);
            innerCreate(nodes[start],1,minValue,parent,result);
        }
        return result;


    }
开发者ID:AlvinTech,项目名称:LeetCode,代码行数:36,代码来源:word-ladder-ii.cpp


示例11: wrap

void _gameData::addEnemyMaskPlus1(unordered_set<pos>& poses, pos rc){
  float sqrtfar2=sqrt(ar2);
  int maskcnt=0;
  for(int row=-4; row<=4; row++){
    for(int col=-4; col<=4; col++){
      float oedist=sqrt(euclideanDist2(0,0,row,col,rows,cols));
      //if(sqrtfar2+1<oedist and oedist<=sqrtfar2+2){
      if(oedist<=sqrtfar2+2){
        poses.insert(pos(wrap(rc.row+row, rows), wrap(rc.col+col,cols)));
        maskcnt++;
      }
    }
  }
  //LOG("added " << maskcnt << " mask tiles");
};
开发者ID:liquid-phynix,项目名称:aicbot,代码行数:15,代码来源:base.cpp


示例12: wordBreak

 bool wordBreak(string s, unordered_set<string> &dict) {
     if (s.empty()) return false;
     int N = s.size();
     vector<bool> dp(N + 1, false);
     dp[0] = true;
     for (int j = 1; j <= N; j++) {
         for (int i = 0; i < j; i++) {
             if (dict.count(s.substr(i, j - i)) && dp[i]) {
                 dp[j] = true;
                 break;
             }
         }
     }
     return dp[N];
 }
开发者ID:astridliu,项目名称:leetcode-6,代码行数:15,代码来源:WordBreak.cpp


示例13: buildMap

 void buildMap(const unordered_set<string>& dict) {
     vdict.clear();
     unordered_map<string, int> unmap;
     for (auto iter = dict.begin(); iter != dict.end(); iter++) {
         unmap[*iter] = vdict.size();
         vdict.push_back(*iter);
     }
     
     map.clear();
     map.resize(vdict.size());
     for (size_t i = 0; i < vdict.size(); i++) {
         string word = vdict[i];
         for (size_t j = 0; j < word.size(); j++) {
             for (char c = 'a'; c <= 'z'; c++) {
                 if (c == vdict[i][j]) continue;
                 word[j] = c;
                 if (unmap.count(word)) {
                     map[i].push_back(unmap[word]);
                 }
                 word[j] = vdict[i][j];
             }
         }
     }
 }
开发者ID:Amywyc,项目名称:leetcode-1,代码行数:24,代码来源:WordLadderII.cpp


示例14: dfs

 void dfs(string s, int index, vector<string> &result, string &eachResult, vector<bool> &possible, unordered_set<string> &dict) {
     if (index == s.size()) {
         eachResult.resize(eachResult.size() - 1);
         result.push_back(eachResult);
         return;
     }
     
     for (int i = 1; i <= (int)s.size() - index; i++) {
         string tmp = s.substr(index, i);
         if (dict.find(tmp) != dict.end() && possible[index + i]) {
             string origin = eachResult;
             eachResult.append(tmp).append(" ");
             int beforeDFS = (int)result.size();
             dfs(s, index + i, result, eachResult, possible, dict);
             if (result.size() == beforeDFS) {
                 possible[index + i] = false;
             }
             
             eachResult = origin;
         }
     }
     
     return;
 }
开发者ID:uniquews,项目名称:LC,代码行数:24,代码来源:main.cpp


示例15: removeNotSinglesInLeft

/* remove overlaps that are duplicated in vector left, and spot sequences that should be remove in next pass because they reached their best overlap with a duplicated sequence */
vector<edge> removeNotSinglesInLeft(const vector<edge>& vect, unordered_set<string>& seqsToRemoveInPref, unordered_set<string>& seqsToRemoveInSuff){
	vector<edge> vectResult;
	uint i(0);
	bool remove(false);
	while (i < vect.size()){
		if (i == 0){
			if (vect[i].sequence != vect[i+1].sequence){
				vectResult.push_back(vect[i]);
			} else {
				remove = true;
			}
		} else if (i == vect.size()-1){
			if (vect[i].sequence != vect[i-1].sequence){
				vectResult.push_back(vect[i]);
			} else {
				remove = true;
			}
		} else {
			if (vect[i].sequence != vect[i+1].sequence and vect[i].sequence != vect[i-1].sequence){
				vectResult.push_back(vect[i]);
			} else {
				remove = true;
			}
		}
		if (remove == true){
			if (vect[i].canonical == true){
				seqsToRemoveInSuff.insert(vect[i].sequence);
			} else {
				seqsToRemoveInPref.insert(vect[i].sequence);
			}
		}
		++i;
		remove = false;
	}
	return vectResult;
}
开发者ID:kamimrcht,项目名称:kMILL,代码行数:37,代码来源:compaction.cpp


示例16: helper

 void helper(string beginWord, string endWord, unordered_set<string> wordList, int& minLength, vector<string>& ladder, vector<vector<string>>& res) {
     if (beginWord == endWord || isIntermediate(beginWord, endWord)) {
         if (isIntermediate(beginWord, endWord)) {
             ladder.push_back(endWord);
         }
         if (ladder.size() == minLength) {
             res.push_back(ladder);
         } else if (ladder.size() < minLength) {
             res.clear();
             res.push_back(ladder);
             minLength = ladder.size();
         }
         return;
     }
     for (auto it = wordList.begin(); it != wordList.end(); ++it) {
         if (isIntermediate(beginWord, *it)) {
             ladder.push_back(*it);
             unordered_set<string> newWordList = wordList;
             newWordList.erase(newWordList.find(*it));
             helper(*it, endWord, newWordList, minLength, ladder, res);
         }
     }
     return;
 }
开发者ID:kean0212,项目名称:learning,代码行数:24,代码来源:wordLadderII.cpp


示例17: StringProduction

bool StringProduction(string src, string dst, unordered_set<string> dico, list<string>* prod_seq)
{
	if (dico.find(src) == dico.end() || dico.find(dst) == dico.end())
		return false;

	map<string, string> parents;
	unordered_set<string> processed;
	queue<string> discovered;
	
	//init the bfs
	discovered.push(src); 
	parents[src] = "";
	while (!discovered.empty())
	{
		string current = discovered.front();discovered.pop();
		processed.insert(current);
		list<string> neighbours = GetNeighbours(current, dico);
		for (string s : neighbours)
		{
			if (processed.find(s) == processed.end())
			{
				parents[s] = current;
				discovered.push(s);
			}
		}		
	}
	if (processed.find(dst) == processed.end())
		return false;
	
	while (dst!="")
	{
		prod_seq->push_front(dst);
		dst = parents[dst];
	}
	return true;
}
开发者ID:zebullax,项目名称:EPI,代码行数:36,代码来源:Chap19.cpp


示例18: ladderLength

    int ladderLength(string start, string end, unordered_set<string> &dict) {
	int length = 2, count = 1, neighb = 0;
	unordered_set<string> visited;
	queue<string> level;
	level.push(start);
	visited.insert(start);
	while(!level.empty())
	{
	    string curr = level.front();
	    level.pop();
	    --count;
	    for(int i = 0; i < curr.size(); ++i)
	    {
		for(int j = 0; j < 26; ++j)
		{
		    string next = curr;
		    next[i] = 'a' + j;
		    if(next == end)
			return length;
		    if(dict.find(next) != dict.end() && visited.find(next) == visited.end())
		    {
			++neighb;
			level.push(next);
			visited.insert(next);
		    }
		}
	    }
	    if(count == 0)  // new level starts
	    {
		++length;
		count = neighb;
		neighb = 0;
	    }
	}
	return 0;
    }
开发者ID:suifengls,项目名称:LeetCode,代码行数:36,代码来源:WordLadder.cpp


示例19: ladderLength

    int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) {
        queue<string> q;
        int s = beginWord.size();
        int r = 1;
        q.push(beginWord);
        while  (!q.empty()) {
            cout << " -+-+-+-+-+-+-+ " << endl;
            int num = q.size();
            bool found = false;
            for (int i = 0; i < num; i++) {
                    string tmp = q.front();
                    cout << "tmp:" << tmp << endl;
                    q.pop();
                    for (int k = 0; k != s; k++) {
                        char c = tmp[k];
                        for (int l = 0; l < 26; l++) {
                            tmp[k] = 'a'+l;
		            unordered_set<string>::iterator it;
                            it = wordList.find(tmp);
                            if (it != wordList.end()) {
                                if (*it == endWord) {
			            cout << *it << ",";
                	            return r+1;
                                } else {
                                    q.push(tmp);
                                    wordList.erase(it);
                                }
                            }
                        }
                        tmp[k] = c;
                    }
            }
            r++;
        }
        return 0;
    }
开发者ID:Peilin-Yang,项目名称:leetcode,代码行数:36,代码来源:word_ladder.cpp


示例20: ladderLength

 int ladderLength(string start, string end, unordered_set<string> &dict) {
     dict.insert(end);
     queue<pair<string,int>> q;
     q.push(make_pair(start,1));
     while(!q.empty()) {
         string s = q.front().first;
         int len = q.front().second;
         if(s==end) return len;
         q.pop();
         vector<string> neighbors = findNeighbors(s, dict);
         for(int i=0; i<neighbors.size(); i++) 
             q.push(make_pair(neighbors[i],len+1));
     }
     return 0;
 }
开发者ID:wxping715,项目名称:LeetCode,代码行数:15,代码来源:127.cpp



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ unsigned类代码示例发布时间:2022-05-31
下一篇:
C++ unordered_multimap类代码示例发布时间:2022-05-31
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap