LeetCode Word Break II

class Solution {
private:
    vector<string> result;
public:
    vector<string> wordBreak(string s, unordered_set<string> &dict) {
        vector<vector<int> > dp;
        result.clear();
        
        int len = s.length();
        dp.resize(len + 1);
        dp[0].push_back(-1);
        
        for (int i=1; i<=len; i++) {
            for (int j=i-1; j>=0; j--) {
                string str = s.substr(j, i - j);
                if (dict.find(str) != dict.end()) {
                    if (dp[j].empty()) continue;
                    dp[i].push_back(j);
                }
            }
        }
        dfs_build(dp, s, len, "");
        return result;
    }
    
    void dfs_build(vector<vector<int> >& dp, string& s, int len, string sent) {
        vector<int> indices = dp[len];
        for (int i=0; i<indices.size(); i++) {
            int prefix_len = indices[i];
            if (prefix_len <= 0) {
                result.push_back(s.substr(0, len) + sent);
            } else {
                string suffix = s.substr(prefix_len, len - prefix_len);
                dfs_build(dp, s, prefix_len, " " + suffix + sent);
            }
        }
    }
};

首先直接用dp构建结果,就是dp数组的类型是vector<vector<string> >,每个外层元素表示某一长度内的所有可分词情况,然后读入一个字母遍历原来的状态,如果新的组合可以则保存,但是这样会超内存,于是把dp数组改为只保存上一个可分词的长度(等同于原先字符串中的索引位置),然后用一个dfs构建出最终的结果,时间在80+ms应该不算暴力了吧。下面给出memory limit exceed的代码,好理解一些

class Solution {
public:
    vector<string> wordBreak(string s, unordered_set<string> &dict) {
        vector<vector<string> > dp;
        int len = s.length();
        dp.resize(len + 1);
        dp[0].push_back("");
        
        for (int i=1; i<=len; i++) {
            for (int j=i-1; j>=0; j--) {
                string str = s.substr(j, i - j);
                if (dict.find(str) != dict.end()) {
                    vector<string>& prefix = dp[j];
                    if (prefix.empty()) continue;
                    for (int k=0; k<prefix.size(); k++) {
                        if (prefix[k].length() == 0) {
                            dp[i].push_back(str);
                        } else {
                            dp[i].push_back(prefix[k] + " " + str);
                        }
                    }
                }
            }
        }
        vector<string> ret = dp[len];
        dp.clear();
        return ret;
    }
};

 第二轮:

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:
    vector<string> wordBreak(string s, unordered_set<string> &dict) {
        int n = s.size();
        vector<vector<int> > dp(n + 1);
        
        dp[0].push_back(-1);
        
        for (int i=1; i<=n; i++) {
            for (int j=i-1; j>=0; j--) {
                if (dict.count(s.substr(j, i-j)) > 0 && !dp[j].empty()) {
                    dp[i].push_back(i-j);
                }
            }
        }
        
        vector<string> res;
        dfs(s, dict, dp, n, "", res);
        return res;
    }
    
    void dfs(string& s, unordered_set<string>& dict, vector<vector<int> >& dp, int last, string suffix, vector<string>& res) {
        if (last == 0) {
            res.push_back(suffix);
            return;
        }
        vector<int>& lens = dp[last];
    
        for(int i=0; i<lens.size(); i++) {
            int seg_len = lens[i];
            if (suffix.size() == 0) {
                dfs(s, dict, dp, last - seg_len, s.substr(last-seg_len, seg_len), res);
            } else {
                dfs(s, dict, dp, last - seg_len, s.substr(last-seg_len, seg_len) + " " + suffix, res);
            }
        }
    }
};

 

posted @ 2014-05-07 21:26  卖程序的小歪  阅读(274)  评论(0编辑  收藏  举报