std::vector<std::string> wordBreak(std::string s, std::unordered_set<std::string>& dict) { if (m.count(s)) { DEBUG("match!", s); return m[s]; } std::vector<std::string> result; if (dict.count(s)) { result.push_back(s); } for (int i = 1; i < s.size(); ++i) { std::string word = s.substr(i); DEBUG("word", word); if (dict.count(word)) { std::string rem = s.substr(0, i); DEBUG("rem", rem); std::vector<std::string> prev = combine(word, wordBreak(rem, dict)); result.insert(result.end(), prev.begin(), prev.end()); } } DEBUG("s", s); PrintVec(s, result); m[s] = result; return result; }
void wordBreak(string s, unordered_set<string>& wordDict, unordered_map<int, vector<string>>& inter_res, int start){ if(start==s.size()){ inter_res[start]={""}; return; } // 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()){ // compare with that part of TLE solution below------------------------------------------------ if(inter_res.find(end+1)!=inter_res.end()) for(auto e:inter_res[end+1]) // if(find(inter_res[start].begin(), inter_res[start].end(), (word + " " + e))==inter_res[start].end()) // since it has already pruned the recursion tree, there is no need to remove duplicates if(e=="") inter_res[start].push_back(word); else inter_res[start].push_back(word+ " " + e); else{ wordBreak(s, wordDict, inter_res, end+1); // returns the complete combination vector of s[end+1, s.size()] for(auto e:inter_res[end+1]) if(e=="") inter_res[start].push_back(word); else inter_res[start].push_back(word+ " " + e); } // ------------------------------------------------------------------------------------------ } } return; }
vector<string> wordBreak(const string& s, vector<string>& wordDict) { if(mem.find(s) != mem.end()) return mem[s]; unordered_set<string> dict(wordDict.begin(), wordDict.end()); vector<string> res; if(dict.find(s) != dict.end()) res.push_back(s); for(int i = 1; i < s.size(); ++i) { string post = s.substr(i); string pre = s.substr(0, i); if(dict.find(post) != dict.end()) { auto sub_res = wordBreak(pre, wordDict); combine(post, sub_res); res.insert(res.end(), sub_res.begin(), sub_res.end()); } } mem[s] = res; return res; }
vector<string> Solution::wordBreak(string s, unordered_set<string> &wordDict) { vector<string> res; // check hash first if (dpHash.find(s) != dpHash.end()) { return dpHash[s]; } // check if need go further // it there's no checking here, the run time will be // wi hash : 20ms // wo hash : TLE bool notFound = true; for (int i = int(s.size()) - 1; i >=0; --i) { // key here if (wordDict.find(s.substr(i)) != wordDict.end()) { notFound = false; break; } } if (notFound) { return res; } // go further if (wordDict.find(s) != wordDict.end()) { res.push_back(s); } for (int i = int(s.size()) - 2; i >= 0; --i) { if (wordDict.find(s.substr(0, i + 1)) != wordDict.end()) { vector<string> subRes = wordBreak(s.substr(i + 1), wordDict); for (auto tmp : subRes) { res.push_back(s.substr(0, i + 1) + " " + tmp); } } } dpHash[s] = res; // update hash return res; }
void wordBreak(string &s, map<char, vector<string> > &map_dict, vector<string> &res, vector<string> &cur_res, int idx, vector<bool> &state) { if(idx == s.size()) { string str = cur_res[0]; for(int i = 1; i < cur_res.size(); ++i) str.append(" " + cur_res[i]); res.push_back(str); return; } if(!map_dict.count(s[idx])) return; vector<string> &dict(map_dict[s[idx]]); for(int i = 0; i < dict.size(); ++i) { if(idx + dict[i].size() <= s.size() && state[idx + dict[i].size()]) { string tmp = s.substr(idx, dict[i].size()); if(tmp == dict[i]) { int beforeChange = res.size(); cur_res.push_back(tmp); wordBreak(s, map_dict, res, cur_res, idx + tmp.size(), state); cur_res.pop_back(); if(res.size() == beforeChange) state[idx + tmp.size()] = false; } } } }
public List<String> wordBreak(String s, Set<String> dict) { List<String> result = new ArrayList<String>(); for(int j = s.length() - 1; j >= 0; j--){ if(dict.contains(s.substring(j))) break; else{ if(j == 0) return result; } } for(int i = 0; i < s.length()-1; i++) { if(dict.contains(s.substring(0,i+1))) { List<String> strs = wordBreak(s.substring(i+1,s.length()),dict); if(strs.size() != 0) for(Iterator<String> it = strs.iterator();it.hasNext();) { result.add(s.substring(0,i+1)+" "+it.next()); } } } if(dict.contains(s)) result.add(s); return result; }
bool wordBreak(string s, unordered_set<string>& wordDict) { int min_len=0, max_len=0; for(auto w:wordDict){ // find minimum and maximum length of word in wordDict w.size()>max_len ? max_len=w.size() : max_len; w.size()<min_len ? min_len=w.size() : min_len; } return wordBreak(s, wordDict, 0, min_len, max_len); }
void CtrlrMIDIBuffer::colorize() { if (!dataToDisplay.contains("\n")) wordBreak(documentEditor->getNumColumnsOnScreen()); currentTokenSet = getTokenSetFromMenu(); tokeniser.setTokenSet (currentTokenSet); status->repaint(); }
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); } } } }
vector<string> wordBreak(string s, unordered_set<string>& wordDict) { for(auto w:wordDict){ w.size()>max_len ? max_len=w.size() : max_len; w.size()<min_len ? min_len=w.size() : min_len; } unordered_map<int, vector<string>> inter_res; // inter if(!wordDict.empty()) wordBreak(s, wordDict, inter_res, 0); return inter_res[0]; }
int main() { unordered_set<string> d; string a[] = {"a", "abc", "b", "cd"}; vector<string> b = arrayToVector(a); for(int i = 0;i < b.size();i++) { d.insert(b[i]); } cout<<wordBreak("abcd", d)<<endl; return 0; }
vector<string> wordBreak(string s, unordered_set<string>& wordDict) { for(auto w:wordDict){ w.size()>max_len ? max_len=w.size() : max_len; w.size()<min_len ? min_len=w.size() : min_len; } vector<string> res; if(!wordDict.empty()) wordBreak(s, wordDict, res, 0); return inter_res[s.size()-1]; }
bool wordBreak(string s, unordered_set<string>& wordDict, int startPoint, int min_len, int max_len){ if(startPoint==s.size()) return true; bool res=false; for(int i=startPoint+min_len-1;i< min(startPoint+max_len, (int)s.size());i++){ // O(n) [start, i] forms new words if(wordDict.find(s.substr(startPoint, i-startPoint+1))!=wordDict.end()) res= record.find(i+1)!=record.end() ? record[i+1] : (wordBreak(s, wordDict, i+1, min_len, max_len) || res); } record[startPoint]=res; return res; }
bool wordBreak(string s){ if (cache.find(s) != cache.end()) return cache[s]; if (Dict->count(s)) return cache[s] = true; for (int i = 1; i < s.length(); i++){ string left = s.substr(0, i); if (Dict->count(left) && wordBreak(s.substr(i, s.length() - i))) return cache[s] = true; } return cache[s] = false; }
vector<string> wordBreak(string s, vector<string>& wordDict) { if (m.count(s)) return m[s]; if (s.empty()) return {""}; vector<string> res; for (string word : wordDict) { if (s.substr(0, word.size()) != word) continue; vector<string> rem = wordBreak(s.substr(word.size()), wordDict); for (string str : rem) { res.push_back(word + (str.empty() ? "" : " ") + str); } } return m[s] = res; }
void wordBreak(const string &s, int beg, unordered_set<string> &wordDict, vector<bool> &dp, string str, vector<string> &ret) { if(beg == s.size()) { ret.push_back(str.substr(0, str.size() - 1)); //eliminate the last space return; } for(int i = beg + 1; i <= s.size(); ++i) //We only go further for certain cases if(dp[i] && wordDict.find(s.substr(beg, i - beg)) != wordDict.end()) wordBreak(s, i, wordDict, dp, str + s.substr(beg, i - beg) + " ", ret); }
// Driver program to test above functions int main() { wordBreak("ilikesamsung")? cout <<"Yes\n": cout << "No\n"; wordBreak("iiiiiiii")? cout <<"Yes\n": cout << "No\n"; wordBreak("")? cout <<"Yes\n": cout << "No\n"; wordBreak("ilikelikeimangoiii")? cout <<"Yes\n": cout << "No\n"; wordBreak("samsungandmango")? cout <<"Yes\n": cout << "No\n"; wordBreak("samsungandmangok")? cout <<"Yes\n": cout << "No\n"; return 0; }
vector<string> wordBreak(string s, unordered_set<string> &dict) { // Note: The Solution object is instantiated only once and is reused by each test case. vector<string> res; if(s.size() == 0) return res; map<char, vector<string> > map_dict; for(unordered_set<string>::const_iterator iter = dict.begin(); iter != dict.end(); ++iter) { if(iter->size() == 0) continue; map_dict[(*iter)[0]].push_back(*iter); } vector<bool> state(s.size() + 1, true); vector<string> cur_res; wordBreak(s, map_dict, res, cur_res, 0, state); return res; }
bool wordBreak(string s, unordered_set<string> &dict) { if ((int)s.size() == 0) { return true; } for (int i = (int)s.size(); i > 0; --i) { if (dict.find(s.substr(0, i)) != dict.end()) { if (wordBreak(s.substr(i, s.size() - i), dict)) { return true; } } } return false; }
bool wordBreak(string s, unordered_set<string> &dict) { int len = s.length(); //看看s本身在不在dict里面 if(dict.find(s) != dict.end()) return true; //对每个位置尝试进行分割 for(int i=1; i<len; i++) { if(dict.find(s.substr(0,i)) != dict.end()) { //如果某位置之前的子串在dict中,则判断剩余的是否能划分 if(!m[i] && wordBreak(s.substr(i,len-i), dict)) { return true; } else { m[i] = true; //用备忘录记录下,这个位置就不必重复检验了 } } } return false; }
vector<string> wordBreak(string s, unordered_set<string>& dict) { if(m.count(s)) return m[s]; //take from memory vector<string> result; if(dict.count(s)){ //a whole string is a word result.push_back(s); } for(int i=1;i<s.size();++i){ string word=s.substr(i); if(dict.count(word)){ string rem=s.substr(0,i); vector<string> prev=combine(word,wordBreak(rem,dict)); result.insert(result.end(),prev.begin(), prev.end()); } } m[s]=result; //memorize return result; }
string wordBreak(string input){ unsigned long long inLen = input.length(); assert( 0 != inLen); if(dict.contains(input)){ return input; } for(int i=1;i < inLen; i++){ string prefix = input.substr(0,i); if(dict.contains(prefix)){ string brokenSuffix = wordBreak(input.substr(i+1,inLen-i-1)); if( 0 != brokenSuffix.length()){ return prefix + " " + brokenSuffix; } } } return ""; }
vector<string> wordBreak(string s){ if (cache.find(s) != cache.end()) return cache[s]; vector<string> result; if (Dict->count(s)) result.push_back(s); string left; for (int i = 1; i < s.length(); i++){ left += s[i - 1]; if (Dict->count(left)){ vector<string> right = wordBreak(s.substr(i, s.length() - i)); combine(left, right, result); } } return cache[s] = result; }
vector<string> wordBreak(string s, unordered_set<string>& wordDict) { vector<string> res; // check if the current string contains word showed in dictionary for (int i = (int)s.length(); i >= 0; i--) { if (wordDict.find(s.substr(i))!= wordDict.end()) { break; } else { if (i == 0) { return res; } } } // word break for the current string for (int i = 0; i < (int)s.length(); i++) { if (wordDict.find(s.substr(0, i + 1)) != wordDict.end()) { // check if the rest contains word showed in dictionary and // update list vector<string> list = wordBreak(s.substr(i + 1, s.length()), wordDict); if (list.size() != 0) { for (int j = 0; j < (int)list.size(); j++) { res.push_back(s.substr(0, i + 1) + " " + list[j]); } }else{ if(i==s.length()-1){ res.push_back(s.substr(0, i + 1)); } } } } /*if (wordDict.contains(s)) { res.add(s); }*/ return res; }
int main() { while(1) { string A; cin>>A; int n; cin>>n; vector<string> B; for(int i = 0 ; i<n ; i++) { cin>>B[i]; } cout<<wordBreak(A,B)<<endl; } }
vector<string> wordBreak(string s, unordered_set<string> &dict) { vector<string> res; if(s.size() == 0) return res; if(cache.find(s) != cache.end()) return cache.find(s)->second; if(dictContains(s, dict)) res.push_back(s); for(int i = 1; i < s.size(); i++){ string prefix = s.substr(0, i); string suffix = s.substr(i); if(dictContains(prefix, dict) && checkSuffix(suffix, dict)){ vector<string> words = wordBreak(suffix, dict); for(int j = 0; j < words.size(); j++){ res.push_back(prefix + " " + words[j]); } } } cache.insert({s, res}); return res; }
public List<String> wordBreak(String s, Set<String> dict) { List<String> list=new ArrayList<String>(); if(map.containsKey(s)) return map.get(s); for(int i=1; i<=s.length();i++){ String left=s.substring(0,i); String right=s.substring(i); if(dict.contains(left)){ List<String> a=wordBreak(right, dict); for(String b:a){ list.add(left+" "+b); } if(right.length()==0) list.add(left); } } map.put(s, list); return list; }
vector<string> wordBreak(string s, unordered_set<string>& wordDict) { if(mymap.count(s)) return mymap[s]; vector<string> ret; if(wordDict.count(s)){ ret.push_back(s); } for(int i = 1; i < s.size(); ++i){ //backtracking string word = s.substr(i); if(wordDict.count(word)){ string rem = s.substr(0, i); vector<string> pre = combine(word, wordBreak(rem, wordDict)); ret.insert(ret.end(), pre.begin(), pre.end()); } } mymap[s] = ret; return ret; }
vector<string> wordBreak(string s, unordered_set<string>& wordDict) { /******** Below is the result form word break **********/ vector<bool> dp(s.size() + 1, false); dp[0] = true; for(int i = 1; i <= s.size(); ++i) { for(int j = i - 1; j >= 0; --j) { if(dp[j] && wordDict.find(s.substr(j, i - j)) != wordDict.end()) { dp[i] = true; break; } } } if(!dp[s.size()]) return {}; /******************************************************/ vector<string> ret; wordBreak(s, 0, wordDict, dp, "", ret); return ret; }
void Main() { unordered_set<string> dict = { "a", "b" }; print(wordBreak("ab", dict)); }