void wordBreak(string s, unordered_set<string>& wordDict, vector<string>& res, int start){ // if this set to be s.size(), then each time it will go through the whole recursion tree for(int end=start+min_len-1;end<min(start+max_len, (int)s.size());end++){ string word=s.substr(start, end-start+1); if(wordDict.find(word)!=wordDict.end()){ if(inter_res.find(start-1)!=inter_res.end()){ for(auto e:inter_res[start-1]) if(find(inter_res[end].begin(), inter_res[end].end(), (e + " "+word))==inter_res[end].end()) inter_res[end].push_back(e + " "+word); wordBreak(s, wordDict, res, end+1); } else{ inter_res[end].push_back(word); wordBreak(s, wordDict, res, end+1); } } } }
bool wordBreak(string s, unordered_set<string> &dict) { vector<bool> dp(s.length(),false); for(int i = 0; i<s.length(); i++) { for(int j = i; j < s.length(); j++) { string word = s.substr(i, j-i+1); if(dict.find(word) != dict.end()) if (i==0) dp[j] = true; else if (dp[i-1] == true) dp[j] = true; } } return dp.back(); }
bool WordBreak::wordBreak(string s, unordered_set<string>& wordDict) { int sz = s.size(); vector<bool> dp(sz + 1, false); dp[0] = true; for (int i = 1; i <= sz; i++) { for (int j = 0; j < i; j++) { if (dp[j] && wordDict.find(s.substr(j, i - j)) != wordDict.end()) { dp[i] = true; break; } } } return dp[sz]; }
vector<string> wordBreak(string s, unordered_set<string> &dict) { // Note: The Solution object is instantiated only once and is reused by each test case. int n = s.size(); if (n == 0) return vector<string> (0); vector<vector<int> > dp_prev(n+1, vector<int> (0));//dp_prev[k] has all the prev nodes for substr(0,k-1) dp_prev[0] = vector<int> (1,-1);//assign any number to it make it valid for (int i=1; i<n+1; ++i){ for (int j=0; j<i; ++j){ if (dp_prev[j].size()>0 && dict.find(s.substr(j,i-j))!=dict.end()){ dp_prev[i].push_back(j); } } } //backtracking vector<string> output(0); backTracking (s, dp_prev, n, output); return output; }
void DFSUtil(Vertex *curr,unordered_set<int> &visited) { if(!curr) { return; } visited.insert(curr->key); visit(curr); for(auto nv:curr->_neighbours) { if(visited.find(nv) == visited.end()) { DFSUtil(_graph[nv],visited); } } }
static bool is_oneway_method( const IOBuf* buf, const unordered_set<string>& oneways) { string fname; MessageType mtype; int32_t protoSeqId = 0; ProtocolReader iprot; iprot.setInput(buf); try { iprot.readMessageBegin(fname, mtype, protoSeqId); auto it = oneways.find(fname); return it != oneways.end(); } catch(const TException& ex) { LOG(ERROR) << "received invalid message from client: " << ex.what(); return false; } }
bool wordBreak(string s, unordered_set<string> &dict) { int sz = s.size(); int dp[sz + 5]; // dp[i] = dp[j] && dict.find(s.substr(j, i - j)) (0 <= j <= i) memset(dp, 0, sizeof(dp)); dp[0] = 1; for (int i = 1; i <= sz; i ++) { for (int j = 0; j <= i; j ++) { string t = s.substr(j, i - j); if (dp[j] && dict.find(t) != dict.end()) { dp[i] = 1; break; } } } int ans = dp[sz]; return ans; }
pull hash_word ( char *ptr, int wl ){ struct stemmer *z = create_stemmer(); wl = stem(z, ptr , wl - 1 ) + 1; ull word_val = 0; ull pos_word = 0; for ( ui l = 0 ; l < wl; l++ ) word_val = word_val*27 + (*(ptr + l) - 96 ); if ( stop_words.find(word_val) == stop_words.end()){ pos_word = lower_bound(words.begin(), words.end(), make_pair(word_val,0), comparator_fn ) - words.begin(); if ( pos_word == 0 ) word_val = 0; } else word_val = 0; return make_pair(word_val, pos_word); }
void wordBreak(string s, unordered_set<string>& wordDict, vector<string> &result, int start, string part, int maxLength) { if(start == s.size()) { result.push_back(part); return; } for(int i = start;i < start + maxLength && i < s.size();i ++) { string sub = s.substr(start, i - start + 1); if(wordDict.find(sub) != wordDict.end()) { string temp; if(part.size() == 0) { temp = sub; } else { temp = part + " " + sub; } wordBreak(s, wordDict, result, i + 1, temp, maxLength); } } }
void runDynamicProg_(string s, unordered_set<string>& wordDict) { //allocate cache isWord_ = new bool*[s.length()]; for (unsigned int i=0; i<s.length(); i++) { isWord_[i] = new bool[s.length()]; } //main dyn programming algorithm for (unsigned int sz=1; sz <= s.length(); sz++) { unsigned int szInd = sz-1; for (unsigned int i=0; i<s.length() && (i+sz-1)<s.length(); i++) { //i denotes starting index string testMe = s.substr(i, sz); //Is word of size sz starting at index i in s, a legit word or not? isWord_[szInd][i] = (wordDict.find(testMe) == wordDict.end()) ? false : true; } } }
void backtrack(vector<int> &A, vector<bool> used, unordered_set<int> &sqr, int &ans, vector<int> &cur) { if (cur.size() == A.size()) { ans++; } else { for (size_t i = 0; i < A.size(); ++i) { if (used[i]) continue; if (i > 0 && !used[i] && A[i - 1] == A[i]) continue; if (cur.empty() || sqr.find(cur.back() + A[i]) != sqr.end()) { cur.push_back(A[i]); backtrack(A, used, sqr, ans, cur); cur.pop_back(); } } } }
bool wordBreak(unordered_set<string> &dict, string s) { vector<bool> f(s.size() + 1, false); f[0] = true; for (int i = 1; i <= s.size(); ++i) { for (int j = i - 1; j >= 0; --j) { if (f[j] && dict.find(s.substr(j, i - j)) != dict.end()) { f[i] = true; break; } } } return f[s.size()]; }
bool wordBreak(string s, unordered_set<std::string> &dict) { s.insert(0,"x"); int nChar = s.size(); vector<bool> possible(nChar, 0); possible[0] = true; for(int i =1; i< nChar; ++i) { for(int k=0; k<i; ++k) { possible[i] = possible[k] && dict.find(s.substr(k+1, i-k)) != dict.end(); if(possible[i]) break; } } return possible[nChar-1]; }
int solve(int mask, int pos){ int n = v.size(); if( bitcount(mask) == n ) return 1; if( dp[mask][pos] != -1 ) return dp[mask][pos]; int result = 0; int last = -1; for( int i = 0 ; i < n; ++i ){ if( !(mask & (1<<i) ) && hash.find(v[pos] + v[i]) != hash.end() && (last == -1 || v[i] != v[last]) ){ result += solve( mask | (1<<i), i); last = i; } } return dp[mask][pos] = result; }
void addNeighborWords(string word, unordered_set<string>& dict, queue<string>& q) { dict.erase(word); 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 (dict.find(temp) != dict.end()) { q.push(temp); dict.erase(temp); } } } } }
bool wordBreak(string s, unordered_set<string>& wordDict) { vector<int> dp(s.size() + 1); dp[0] = 1; for (int i = 1; i <= s.size(); i++) { bool flag = false; for (int j = 0; j < i; j++) { if ((dp[j] == 1) && (wordDict.find(s.substr(j, i - j)) != wordDict.end())) { flag = true; break; } } if (flag == true) dp[i] = 1; else dp[i] = 0; } return dp[s.size()] == 1; }
bool wordBreak(string s, unordered_set<string> &dict) { if (s.empty()) return false; int len = s.length(); vector<vector<bool> > mm(len, vector<bool>(len, false)); for (int i = 0; i < len; ++i) { for (int j = i; j < len; ++j) { if (dict.find(s.substr(i, j - i + 1)) != dict.end()) mm[i][j] = true; } } for (int i = 0; i < len; ++i) { for (int j = i; j < len; ++j) { for (int k = i; k < j; ++k) { if (mm[i][k] && mm[k + 1][j]) mm[i][j] = true; } } } return mm[0][len - 1]; }
vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) { this->start = start; dict.insert(end); cur_lev.insert(start); while(true) { if (cur_lev.empty()) break; cleardict(dict, cur_lev); next_lev.clear(); for (auto i = cur_lev.begin(); i != cur_lev.end(); i++) expand(dict, *i); if (next_lev.find(end) != next_lev.end()) { path.push_back(end); out(end); return res; } cur_lev = next_lev; } return res; }
int ladderLength1(string beginWord, string endWord, unordered_set<string>& wordList) { if(beginWord == endWord) return 0; unordered_set<string> head, tail, *pHead, *pEnd; head.insert(beginWord); tail.insert(endWord); int result = 2; while (!head.empty() && !tail.empty()) { if(head.size() < tail.size()) { pHead = &head; pEnd = &tail; } else { pHead = &tail; pEnd = &head; } unordered_set<string> candidate; for(auto i = pHead->cbegin(); i != pHead->end(); ++i) { string word = *i; //update every char for(int j = 0; j < word.size(); ++j) { char ch = word[j]; for(int k = 0; k < 26; ++k) { word[j] = (char)('a' + k); if(pEnd->find(word) != pEnd->end()) return result; if(wordList.find(word) != wordList.end()) { candidate.insert(word); wordList.erase(word); } } word[j] = ch; } } result++; swap(*pHead, candidate); } return 0; }
// if true is returned the void FindCloudFloodfill(int x, int y, int& xmin,int& xmax, int& ymin, int& ymax, unordered_set<Vector2f>& visitedPixels, bool& tooBig) { Vector2f p((float)x,(float)y); if(tooBig) { return; // too big, stop the entire recursion } if(abs(xmax -xmin) > CLOUD_MAX_SIZE || abs(ymax -ymin) > CLOUD_MAX_SIZE) { tooBig = true; return; // too big, shut down recursion } const float sample = SampleNoise(p); if( (sample < EPSILON) || // invisible pixel visitedPixels.find(p) != visitedPixels.end() // already visited pixel ) { // no longer a cloud. stop recursion. return; } visitedPixels.insert(p); // update min and max. if(x < xmin) { xmin = x; } if(y < ymin) { ymin = y; } if(x > xmax) { xmax = x; } if(y > ymax) { ymax = y; } FindCloudFloodfill(x+1,y,xmin,xmax, ymin, ymax,visitedPixels, tooBig); FindCloudFloodfill(x-1,y,xmin,xmax, ymin, ymax,visitedPixels, tooBig); FindCloudFloodfill(x,y+1,xmin,xmax, ymin, ymax,visitedPixels, tooBig); FindCloudFloodfill(x,y-1,xmin,xmax, ymin, ymax,visitedPixels, tooBig); }
int ladderLength(string start, string end, unordered_set<string> &dict) { unordered_set<string> added; queue<string> q; int ret = 0; int lev1 = 1, lev2 = 0; q.push(start); added.insert(start); while(!q.empty()) { string s = q.front(); q.pop(); --lev1; for(int i = 0; i < s.length(); ++i) { for(int j = 0; j < 26; ++j) { string t = s; t[i] = 'a' + j; if(t != s) { if(t == end) return ret+2; if(dict.find(t) != dict.end() && added.find(t) == added.end()) { q.push(t); added.insert(t); ++lev2; } } } } if(lev1 == 0) { ++ret; lev1 = lev2; lev2 = 0; } } return 0; }
int ladderLength(string start, string end, unordered_set<string> &dict) { queue<string> q; unordered_set<string> visited; q.push(start); visited.insert(start); int level = 1; int num1 = 1, num2 = 0; while(!q.empty()) { string cur = q.front(); q.pop(); num1--; for(int i=0; i<cur.size(); i++) { for(char k = 'a'; k<='z'; k++) { if(k == cur[i]) continue; swap(cur[i], k); if(cur == end) return level+1; if(dict.find(cur) != dict.end() && visited.find(cur) == visited.end()) { q.push(cur); visited.insert(cur); num2++; } swap(cur[i], k); } } if(num1 == 0) { level++; swap(num1, num2); } } return 0; }
void helper(string s, unordered_set<string> &dict, int start, string t, vector<string> &res) { if (start >= s.size()) { res.push_back(t); return ; } string temp = ""; for (int i = start; i < s.size(); i++) { temp += s[i]; if (dict.find(temp) != dict.end()) { string nextpass = t.size() > 0? t + ' ' + temp : t + temp; helper(s, dict, i + 1, nextpass, res); } } }
bool QuotedLiteralFSM::extend(int x) { CHECK(!ch_.empty()); bool terminated = false; // whether the current sequence can be extended if (octal_ > 0 && octal_ < 3 && isOctal(x)) { ++octal_; } else if (hex_ && isxdigit(x)) { /* no-op */ } else if (lastIsBackSlash(ch_)) { if (isOctal(x)) { octal_ = 1; } else if (x == 'x') { hex_ = true; } else if (SimpleEscape.find(x) == SimpleEscape.end()) { Throw("Bad escape sequence for {}: {}", static_cast<char>(x), Utf8Encoder::encode(ch_)); } /* else { std::cout << format("Escape sequence for {}: {}", static_cast<char>(x), Utf8Encoder::encode(ch_)) << std::endl; } */ } else { // cannot extend previous escape sequence octal_ = 0; hex_ = false; if (x == '\n') { Throw("Unexpected new line after `{}`", Utf8Encoder::encode(ch_)); } else if (x == quote()) { terminated = true; } } ch_.push_back(x); return terminated; }
void dfs(string& s,unordered_set<string>& wordDict,vector<bool> &res,vector<string> &solve,string str,int pos) { if(pos==s.size()) { solve.push_back(str); return; } for(int j=pos;j<s.size();j++) { string t(s,pos,j-pos+1); string temp = str; if(res[j]&&wordDict.find(t) != wordDict.end()) { if(pos!=0)temp+=" "; temp+=t; dfs(s,wordDict,res,solve,temp,j); } } }
bool wordBreak(string strtomatch, unordered_set<string> &myset) { unsigned strlength = strtomatch.length(); BuildMatrix(&pmatrix,strlength,strlength); for(int i=0;i<strlength;++i){ for(int j=i;j<strlength;++j){ pmatrix[i][j] = false; } } for(unsigned i=0;i<strlength;++i){ for(unsigned j=i;j<strlength;++j){ string strdata; strdata.assign(strtomatch,i,j-i+1); if(myset.find(strdata)!=myset.end()){ pmatrix[i][j] = true; } } } for(int i=strlength-1;i>=0;--i){ bool isallfalse = true; for(int j = i;j<strlength;++j){ if(pmatrix[i][j]){ isallfalse = false; break; } } if(isallfalse&&(i-1>=0)){ for(int j = 0;j<strlength;++j){ pmatrix[j][i-1] = false; } } } bool ret = FindWord(strlength); ReleaseMatrix(&pmatrix,strtomatch.length()); return ret; }
bool wordBreak(string s, unordered_set<string>& wordDict) { int len = s.length(); vector<int> dp(len + 1); dp[0] = 1; for (int i = 1; i <= len; ++i) { for (int j = 1; j <= i; ++j) { // check if (wordDict.find(s.substr(i-j, j)) != wordDict.end()) { dp[i] = dp[i - j]; } // if is matched if (dp[i]) break; } } return dp[len]; }
// Copies the QNAME field from an answer section and returns its size, taking // into account pointers. static uint16_t getName(string &name, const char *payload, const char *_name) { uint16_t nameLength = 0; // Copies non-pointer parts. while (*_name && !isPointer(_name)) { if (name.length() + *_name > 255) { return 0; } nameLength += *_name + 1; if (!getLabel(_name, name)) { return 0; } _name += *_name + 1; } // Follows any pointers that may exist and copies their contents. if (isPointer(_name)) { visitedPositions.clear(); nameLength += 2; while (isPointer(_name)) { visitedPositions.insert(_name); _name = payload + (ntohs(*(uint16_t*)_name) & 0x3fff); // Detects more than one visit to the same location in the packet, // which would result in an infinite loop. if (visitedPositions.find(_name) != visitedPositions.end()) { return 0; } while (*_name && !isPointer(_name)) { if (name.length() + *_name > 255) { return 0; } if (!getLabel(_name, name)) { return 0; } _name += *_name + 1; } } } else { if (nameLength > 2) { ++nameLength; } } return nameLength; }
/** * @param s: A string s * @param dict: A dictionary of words dict */ bool wordBreak(string s, unordered_set<string> &dict) { const int n = s.size(); int max_len = 0; for (auto str: dict) max_len = max(max_len, int(str.size())); vector<bool> dp(n+1, 0); dp[0] = true; for (int i = 0; i < n; ++i) { for (int j = i+1; j <= min(n, i+max_len); ++j) { if (dp[i] && dict.find(s.substr(i, j-i)) != dict.end()) { dp[j] = true; continue; } } } return dp[n]; }
inline void tagOOVs ( fst::VectorFst<Arc> *myfst, unordered_set<std::string>& vcb ) { fst::MakeWeight<Arc> mw; typedef typename Arc::StateId StateId; for ( fst::StateIterator< fst::VectorFst<Arc> > si ( *myfst ); !si.Done(); si.Next() ) { StateId state_id = si.Value(); for ( fst::MutableArcIterator< fst::MutableFst<Arc> > ai ( myfst, si.Value() ); !ai.Done(); ai.Next() ) { Arc arc = ai.Value(); if ( vcb.find ( ucam::util::toString<unsigned> ( arc.olabel ) ) == vcb.end() ) { arc.ilabel = arc.olabel; arc.olabel = OOV; arc.weight = mw ( 0 ); ai.SetValue ( arc ); } } } };