vector<string> wordBreak(string s, unordered_set<string> &dict) { int n = s.size(); vector<string> resultCollection; if(n == 0) return resultCollection; vector<string>* stringBreakStore = new vector<string>[n+1]; stringBreakStore[n].push_back(""); string substr, res; int size, len; for(int i = n - 1; i >= 0; i--) { size = n - i; for(int j = 0; j < size; j++) { substr = s.substr(i, j+1); if(dict.find(substr) == dict.end()) continue; if(stringBreakStore[i+j+1].size() == 0) continue; len = stringBreakStore[i+j+1].size(); for(int k = 0; k < len; k++) { res.clear(); res = substr; if(stringBreakStore[i+j+1].at(k).size() > 0) { res += " "; res += stringBreakStore[i+j+1].at(k); } stringBreakStore[i].push_back(res); } } } size = stringBreakStore[0].size(); for(int i = 0; i < size; i++) resultCollection.push_back(stringBreakStore[0].at(i)); delete [] stringBreakStore; return resultCollection; }
bool wordBreak2(string s, unordered_set<string> &dict) { vector<bool> f(s.size() + 1, false); f[0] = true; // 空字符串 for (unsigned 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()]; }
vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) { // Start typing your C/C++ solution below // DO NOT write int main() function unordered_map<string, list<string> > DirectedGraph; unordered_map<string, int> pathLen; //Use BFS to construct a graph queue<string> q; queue<int> len; q.push(start); len.push(1); pathLen[start] = 1; typedef unordered_map<string,int>::iterator umit; while(!q.empty()) { string curStr = q.front(); int curLen = len.front(); if(curStr == end) break; q.pop(), len.pop(); for(int i = 0; i < curStr.size(); ++i) { string temp = curStr; for(int j = 'a'; j <= 'z'; ++j) { if(j == curStr[i]) continue; temp[i] = j; if(dict.find(temp) != dict.end()) { umit it = pathLen.find(temp); if(it == pathLen.end()) { q.push(temp); len.push(curLen + 1); pathLen[temp] = curLen + 1; DirectedGraph[temp].push_back(curStr); } else if(it->second == curLen + 1) { DirectedGraph[temp].push_back(curStr); } } } } } sols.clear(); oneSol.clear(); umit it = pathLen.find(end); if(it == pathLen.end()) return sols; GetAllPath(end, start, DirectedGraph); return sols; }
vector< vector<string> > findLadders(string start, string end, unordered_set<string> &dict) { vector<vector<string> > result; queue<string> q; q.push(start); unordered_set<string> visited; visited.insert(start); unordered_map<string, vector<string> > m; unordered_map<string, int> level; level[start] = 1; int maxLevel = -1; while (!q.empty()) { string tmp = q.front(); q.pop(); if (maxLevel != -1 && level[tmp] > maxLevel) { break; } for (int i=0; i < tmp.size(); i++) { for (char c='a'; c <= 'z'; c++) { if (c == tmp[i]) continue; string temp = tmp; temp[i] = c; if (temp == end) { maxLevel = level[tmp]; if (m.find(end) == m.end()) { m[end] = vector<string>(); } m[end].push_back(tmp); } else if (dict.find(temp) != dict.end()) { if (visited.find(temp) == visited.end()) { q.push(temp); visited.insert(temp); m[temp] = vector<string>(); m[temp].push_back(tmp); level[temp] = level[tmp] + 1; } else if (level[tmp]+1 == level[temp]) { m[temp].push_back(tmp); } } } } } vector<string> path; buildPaths(start, end, m, path, result); return result; }
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 == end ) return 1; if( dict.empty() ) return 0; int len = 1; queue<string> u; u.push( start ); u.push( "" ); while( !u.empty() ) { string v = u.front(); u.pop(); if( v == "" ) //whole level has been traversaled. { len = len+1; if( !u.empty() ) u.push( "" ); } else { if( v == end ) return len; else { string temp = v; for( int i = 0; i < v.length(); ++i ) { for( int j = 'a' ; j <= 'z'; ++j ) { v[i] = j; if( dict.find(v) != dict.end() ) //find the word { u.push( v ); dict.erase( v ); } } v = temp; } } } } return 0; }
int ladderLength(string start, string end, unordered_set<string> &dict) { if (start==end) { return 1; } queue<string> words; words.push(start); dict.erase(start); int depth = 1; int remain = 1; int newRemain = 0; while (!words.empty()) { string word = words.front(); words.pop(); for (auto it = dict.begin();it!=dict.end();) { if (isDiffOne(word,*it)) { if (*it==end) { return depth+1; } else { words.push(*it); it = dict.erase(it); ++newRemain; } } else { ++it; } } --remain; if (remain==0) { remain = newRemain; newRemain = 0; ++depth; } } return 0; }
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 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; } } }
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); }
bool MakeOfWords(string word, int length){ //cout<<"curr: "<<word<<endl; int len = word.length(); //cout<<"len:"<<len<<endl; if(len == 0) return true; for(int i=1; i<=len; ++i){ if(i == length) return false;//取到原始串,即自身 string str = word.substr(0, i); //cout<<str<<endl; //if(hash.find((char*)&str[0])){ if (hashset.find(str) != hashset.end()) { if(MakeOfWords(word.substr(i), length)) return true; } } return false; }
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]; }
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); } } }
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 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(); } } } }
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); } } } } }
vector<string> findNeighbors(string s, unordered_set<string>& wordList){ vector<string> result; for(int i = 0; i < s.length(); ++ i){ char ch = s[i]; for(int j = 0; j < 26; ++ j){ char newch = 'a' + j; if(ch == newch) continue; s[i] = newch; if(wordList.find(s) != wordList.end()){ wordList.erase(s); result.push_back(s); } } s[i] = ch; } return result; }
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 CanWordBreak(string &s, int startPos, bool *canWordBreak, unordered_set<string>& wordDict) { string subString; if (startPos == s.size()) return true; if (canWordBreak[startPos] == false) return canWordBreak[startPos]; for (int i = startPos; i < s.size(); i++) { subString = s.substr(startPos, i - startPos + 1); if (wordDict.find(subString) != wordDict.end()) if (CanWordBreak(s, i + 1, canWordBreak, wordDict)) return true; } canWordBreak[startPos] = false; return false; }
bool wordBreak2(string s, unordered_set<string>& wordDict) { std::vector<string> v; bool *f = new bool[s.size() + 1]; f[0] = true; for(int i = 1; i <= s.size(); i++) { for(int j = 0; j < i; j++) { cout << "i=" << i << ",j=" << j << " " << s.substr(j, i) << endl; if(f[j] && (wordDict.find(s.substr(j,i)) != wordDict.end())) { f[i] = true; v.push_back(s.substr(j,i)); } } } for(auto &i : v) { cout << i << endl; } return f[s.size()]; }
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]; }
/** * @param s: A string s * @param dict: A dictionary of words dict */ bool wordBreak(string s, unordered_set<string> &dict) { if (s.empty()) { if (dict.empty() || dict.find("") != dict.end()) return true; else return false; } vector<bool> is_break(s.size()+1, false); is_break[0] = true; for (int i = 1; i <= s.size(); ++i) { for (string candidate: dict) { if (candidate.size() <= i && \ candidate == string(s.begin() + i - candidate.size(), s.begin() + i)) { if (is_break[i - candidate.size()]) is_break[i] = is_break[i - candidate.size()]; } } } return is_break[s.size()]; }
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 helper(string word, unordered_set<string> &wordList, queue<string> &q) { for (int i = 0; i < word.size(); i++) { char ch = word[i]; for (int j = 0; j < 26; j++) { //可能的下一个单词 word[i] = 'a' + j; //满足条件的下一个单词 if (wordList.find(word) != wordList.end()) { //加入这一层结果队列中 q.push(word); //从字典中删除这个单词,避免重复 wordList.erase(word); } } //恢复word word[i] = ch; } }
bool wordBreak(string s, unordered_set<string> &dict) { int len = s.length(); vector<bool> dp(len + 1, false); dp[len] = true; //start from end of string and proceed for (int i = len - 1; i >= 0; i--) { for (int j = i; j<len; j++) { string str = s.substr(i, j - i + 1); if (dict.find(str) != dict.end() && dp[j + 1]) { dp[i] = true; break; } } } return dp[0]; }
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; } }
int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) { if (beginWord == endWord) return 0; set<string> visited; vector<string> parent, child; set<string> dic; unordered_set<string>::iterator it; int wordLength = beginWord.size(); for (it = wordDict.begin(); it != wordDict.end(); it++) dic.insert(*it); parent.push_back(beginWord); visited.insert(beginWord); int length = 1; while (visited.size() != wordDict.size()) { for (int i = 0; i != parent.size(); i++) { for (int j = 0; j != wordLength; j++) { string key = parent[i]; for (char c = 'a'; c != 'z' + 1; c++) { key[j] = c; if (key == endWord) return length + 1; if (dic.find(key) != dic.end() && visited.find(key) == visited.end()) { child.push_back(key); visited.insert(key); } } } } parent.clear(); for (int i = 0; i != child.size(); i++) parent.push_back(child[i]); length++; child.clear(); if (parent.size() == 0) return 0; } return 0; }