//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); }
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); }
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); }
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 }
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; }
// 双向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); }
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; }
// 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); }
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; }
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; }
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(); } }
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); } }
//这题很有意思,不知道从哪里下手 //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; }
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()]; }
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; } }
vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) { allstr.clear(); res.clear(); if (dict.size() == 0) return res; if (start == end) { vector<string> justone(1, start); res.push_back(justone); return res; } allstr.push_back(make_pair(start, 1)); allstr.push_back(make_pair(end, 0)); for (unordered_set<string>::iterator pos = dict.begin(); pos != dict.end(); ++ pos) { if (*pos != start && *pos != end) { allstr.push_back(make_pair(*pos, 0)); } } _makeLinks(); int step = _ladderLen(); stk.clear(); stk.push_back(1); _DFS(step); return res; }
vector<string> wordBreak(string s, unordered_set<string> &dict) { vector<string> ans; int bitmap = 0; min = INT_MAX; max = -1; if (s.size() == 0 || dict.size() == 0) return ans; for (unordered_set<string>::iterator iter = dict.begin(); iter != dict.end(); iter++) { string d = *iter; int size = d.size(); if (min > size) min = size; if (max < size) max = size; for (int i = 0; i < d.size(); i++) bitmap |= 1<<(d[i]-'a'); } bitmap = ~bitmap; for (int i = 0; i < s.size(); i++) { if ((1<<(s[i]-'a')) & bitmap) return ans; } string tmp; find_ans(ans, s, dict, tmp, 0); return ans; }
CharacterVector file(CharacterVector& x) { const char *const test_lines = x[0]; vector<pair<string, string> > res; taggerseg.tag(test_lines, res); //unsigned int it; vector<string> m; m.reserve(res.size()*2); if(stopWords.size()>0){ for (vector<pair<string, string> >::iterator it = res.begin(); it != res.end(); it++) { if (stopWords.end() == stopWords.find((*it).first)) { m.push_back((*it).first); m.push_back((*it).second); } } }else{ for (vector<pair<string, string> >::iterator it = res.begin(); it != res.end(); it++) { m.push_back((*it).first); m.push_back((*it).second); } } return wrap(m); }
vector<QueenNode*> getPossibleChild(const unordered_set<int>& fullSet, const vector<int>& curSeq) { int n = (int)fullSet.size(); unordered_set<int> neighbor(fullSet); for(size_t i = 0; i < curSeq.size(); i++){ removeItem(neighbor, curSeq[i]); } for(int iter = 1; iter <= (int)curSeq.size(); iter++ ){ int x = curSeq.size() - iter; int y = curSeq[curSeq.size() - iter]; if(x + iter < n && y + iter < n){ removeItem(neighbor, y + iter); } if(x + iter < n && y - iter >= 0){ removeItem(neighbor, y - iter); } } vector<QueenNode*> neighborList; if(!neighbor.empty()){ for(unordered_set<int>::iterator it = neighbor.begin(); it != neighbor.end(); it++){ neighborList.push_back(new QueenNode(*it)); } } return neighborList; }
bool wordBreak(string s, unordered_set<string> &dict) { if (s.size() == 0){ return true; } if (dict.size() == 0){ return false; } bool* f = (bool*)malloc(sizeof(bool)*s.size()); for(int i = 0; i < s.size(); i++){ f[i] = false; } for(int i = 0; i < s.size(); i++){ f[i] = dict.count(s.substr(0,i+1)); if (f[i]){ continue; } for (int j = 0; j < i; ++j){ if (f[j] && dict.count(s.substr(j+1,i-j))){ f[i] = true; break; } } } return f[s.size() - 1]; }
int minLength(string &start, string &end, unordered_set<string> &dict){ int n = dict.size(); if(n == 0) return 0; int len = dict.begin()->size(); queue<string> q; q.push(start); map<string, int> vis; map<string, string> pre; vis[start] = 0; while(!q.empty()){ string u = q.front(); q.pop(); for(int i = 0; i < len; i++){ string v = u; char oldc = u[i]; for(char c = 'a'; c <= 'z'; c++){ if(c == oldc) continue; v[i] = c; if(v == end){ return vis[u] + 1; } if(dict.find(v) == dict.end()) continue; if(vis.find(v) == vis.end()){ vis[v] = vis[u] + 1; q.push(v); } } } } return 0; }
void findNeighborPoints(vtkDataSet* data, vtkIdType ptId, unordered_set<vtkIdType>& nbrs, vtkIdList* cellIds, vtkIdTypeArray* neighbors = NULL) { nbrs.clear(); cellIds->Reset(); data->GetPointCells(ptId, cellIds); for (size_t k = 0; k < cellIds->GetNumberOfIds(); k++) { vtkCell* cell = data->GetCell(cellIds->GetId(k)); for (size_t l = 0; l < cell->GetNumberOfEdges(); l++) { vtkCell* edge = cell->GetEdge(l); vtkIdType s = edge->GetPointId(0); vtkIdType e = edge->GetPointId(1); vtkIdType n = -1; if (s == ptId) { n = e; } else if (e == ptId) { n = s; } if (n > -1) { if (nbrs.find(n) == nbrs.end()) { nbrs.insert(n); if (neighbors != NULL) { neighbors->SetComponent(ptId, nbrs.size() - 1, n); } } } } } }
vector<string> wordBreak(string s, unordered_set<string> &dict) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. vector<string> sentences; if (s != "" && dict.size() && this->is_valid(s, dict)) this->find_sentences("", s, dict, sentences); return sentences; }
void print_clusters(){ int i = 0; unordered_set<unordered_set<point*>*>::iterator iter; for(iter = clusters.begin(); iter != clusters.end(); iter++){ printf("i: %d\n", i); i++; unordered_set<point*>* c = *iter; printf("cluster: %d\n", c->size()); unordered_set<point*>::iterator it; for(it = c->begin(); it != c->end(); it++){ print_point(*it); } } printf("cluster size: %d\n", clusters.size()); printf("noise: %d\n", noiseCluster->size()); printf("total: %d\n", clusters.size()); }
// constructor 1: // takes in kmer sets - kmers and potential edges (read edges) KBF2(const int k, unordered_set<kmer_t> & kmer_set, unordered_set<kmer_t> & edge_set, const unsigned extend_len = 1, const size_t size_factor = 10) : BaseBloomFilter(k, kmer_set.size(), size_factor), extend_len(extend_len){ populate(kmer_set); getEdges(edge_set); cerr << "Edges: " << edge_kmers.size() << endl; extended_check = 0; edge_check = 0; edge_pass = 0; }
vector<string> wordBreak(string s, unordered_set<string> &dict) { vector<string> res; if (dict.size() == 0 || s.size() == 0) return res; helper(s, dict, 0, "", res); return res; }
vector<string> wordBreak(string s, unordered_set<string>& wordDict) { vector<string> res; if (s.length() == 0 || wordDict.size() == 0) //非合法输入 return res; if (!IswordBreak(s, wordDict)) //先判断是否可分解,其实这步可 return res; return __wordBreak(s, wordDict); //分解 }
void _loadSpecialSymbols() { size_t size = sizeof(SPECIAL_SYMBOL)/sizeof(*SPECIAL_SYMBOL); for(size_t i = 0; i < size; i ++) { _specialSymbols.insert(SPECIAL_SYMBOL[i]); } assert(_specialSymbols.size()); }
// constructor actions void constructKBF(unordered_set<kmer_t> & kmer_set, unordered_set<kmer_t> & edge_set){ populate(kmer_set); getEdges(edge_set); cerr << "Edges: " << edge_kmers.size() << endl; sparse_check = 0; extended_check = 0; edge_check = 0; edge_pass = 0; }
void print(unordered_set<string> set) { cout << "\nSet: \n"; int len = set.size(); for (string s : set) { cout << s << "\n"; } }