码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode@ [139/140] Word Break & Word Break II

时间:2015-11-26 23:00:50      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

https://leetcode.com/problems/word-break/

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

For example, given
s = "leetcode",
dict = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

技术分享
class Solution {
public:
    bool wordBreak(string s, unordered_set<string>& wordDict) {
        if(s.length() == 0) return false;
        
        vector<bool> canBreak(s.length(), false);
        for(int i=0;i<s.length();++i) {
            if(wordDict.find(s.substr(0, i+1)) != wordDict.end()) {
                canBreak[i] = true;
                continue;
            }
            for(int pre=0;pre<i;++pre) {
                if(canBreak[pre] && wordDict.find(s.substr(pre+1, i-pre)) != wordDict.end()) {
                    canBreak[i] = true;
                    break;
                }
            }
        }
        
        return canBreak[s.length()-1];
    }
};
View Code

 

https://leetcode.com/problems/word-break-ii/

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.

Return all such possible sentences.

For example, given
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"].

A solution is ["cats and dog", "cat sand dog"].

技术分享
class Solution {
public:
    void findBreakPoint(vector<bool>& canBreak, string& s, unordered_set<string>& wordDict) {
        for(int i=0;i<s.length();++i) {
            if(wordDict.find(s.substr(0, i+1)) != wordDict.end()) {
                canBreak[i] = true;
                continue;
            }
            for(int pre=0;pre<i;++pre) {
                if(canBreak[pre] && wordDict.find(s.substr(pre+1, i-pre)) != wordDict.end()) {
                    canBreak[i] = true;
                    break;
                }
            }
        }
    }
    void dfs(vector<string>& res, vector<string>& load, vector<bool>& canBreak, string& s, unordered_set<string>& wordDict, int idx) {
        if(idx == s.length()-1) {
             string tmp = "";
             for(int i=0;i<load.size()-1;++i) tmp += load[i] + " ";
             if(load.size()>0) tmp+=load[load.size()-1];
             res.push_back(tmp);
             return;
        }
        
        for(int nx=idx+1;nx<s.length();++nx) {
            if(canBreak[nx] && wordDict.find(s.substr(idx+1, nx-idx)) != wordDict.end()) {
                load.push_back(s.substr(idx+1, nx-idx));
                dfs(res, load, canBreak, s, wordDict, nx);
                load.pop_back();
            }
        }
    }
    vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
        vector<bool> canBreak(s.length(), false);
        vector<string> res; res.clear();
        vector<string> load; load.clear();
        
        findBreakPoint(canBreak, s, wordDict);
        if(canBreak[s.length()-1]) dfs(res, load, canBreak, s, wordDict, -1);
        return res;
    }
};
View Code

 

leetcode@ [139/140] Word Break & Word Break II

标签:

原文地址:http://www.cnblogs.com/fu11211129/p/4999001.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!