leetcode-Word Break II

22 篇文章 0 订阅
22 篇文章 0 订阅

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"].

题意:有字符串s和字典dict,将s分割成由字典中出现的若干单词,用空格分开,求所有分割方法。
分析:首先,建立从s从头到尾的可行路径,即dp[i]为从i出发下一个可行位置列表。
为了建立dp[i],要从尾向头找单词,即从n-1到0枚举单词结尾位置j,每个位置找所有可行单词的起始位置i,dp[i].push_back(j)
这样反向建立路径,正向遍历时就一定是可行路径了

注意:若正向建立,那么会有很多路径不能到达终点,导致搜索空间变大,我就是因为这个问题TLE很多次
然后,用dfs或bfs从头搜索,记录结果即可。


代码:

class Solution {
public:
    vector<string> wordBreak(string s, unordered_set<string> &dict) {
        vector<string> ans;
        if(dict.empty()) return ans;
        if(s.empty())
        {
            ans.push_back(s);
            return ans;
        }
        int len = s.length();
        vector<int> tmp;
        vector<vector<int> > dp(len,tmp);
        
        for(int i=len-1; i>=0; i--)
        {
            if(i<len-1 && dp[i+1].size()==0)
                continue;
            for(int j=i; j>=0; j--)
            {
                string cur = s.substr(j,i-j+1);
                if(dict.find(cur)!=dict.end())
                    dp[j].push_back(i);
            }
        }
        
        stack<int> st;
        stack<string> sts;
        st.push(0);
        sts.push("");
        
        while(!st.empty())
        {
            int ind = st.top();
            string pre = sts.top();
            st.pop(),sts.pop();
            
            for(int i=0; i<dp[ind].size(); i++)
            {
                int j = dp[ind][i];
                string now = s.substr(ind,j-ind+1);
                
                string add = pre;
                if(add.length()>0) add += ' ';
                add += now;
                
                if(j+1>=len)
                {
                    ans.push_back(add);
                }
                else
                {
                    st.push(j+1);
                    sts.push(add);
                }
            }
        }
        return ans;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值