140. Word Break II(dp,字典匹配,并输出所有匹配结果,即保存dp路径)(继续理解,重刷)

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



分析:

解分为三步:

(1)构造两级向量(vector<vector<int> > v)

链式存放前一个字符的位置。

(2)基于向量v逆向递归寻找词,借助栈

[dog  -->  [dog, sand  -->  [dog, sand, cat

                   [dog, and   -->  [dog, and, cats

(3)出栈时词以空格隔开,存入ret向量

"cat sand dog"

"cats and dog"


ac代码:

class Solution {
public:
    vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
        vector<string> ret;
        string news = "0" + s;
        int n = news.size();
        vector<vector<int> > v(n);
        vector<bool> bpos(n, false);
        bpos[0] = true;
        for(int i = 1; i < n; i ++)
        {
            for(int j = 0; j < i; j ++)
            {
                if(bpos[j] == true && wordDict.find(news.substr(j+1, i-j)) != wordDict.end())
                {
                    bpos[i] = true;
                    v[i].push_back(j);   
                }
            }
        }
        if(bpos[n-1] == false)
            return ret;
        else
        {
            stack<string> stk;
            genRet(ret, news, v, stk, n-1);
            return ret;
        }
    }
    void genRet(vector<string>& ret, string news, vector<vector<int> > v, stack<string> stk, int bpos)
    {
        if(bpos == 0)
        {// generate final string
            string str;
            while(!stk.empty())
            {
                string top = stk.top();
                stk.pop();
                str += (top + " ");
            }
            str.erase(str.end()-1);
            ret.push_back(str);
        }
        else
        {
            for(int i = 0; i < v[bpos].size(); i ++)
            {
                string cur = news.substr(v[bpos][i]+1, bpos-v[bpos][i]);
                stk.push(cur);
                genRet(ret, news, v, stk, v[bpos][i]);
                stk.pop();
            }
        }
    }
};


mycode:

class Solution {
public:
    vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
        int i,j,L=s.length();
        vector<string>ret;
        vector<vector<int>>v(L+1);
        stack<string>stk;
        bool word[L+1];
        for(i=1;i<=L;i++)
        word[i]=false;
        word[0]=true;
        for(i=1;i<=L;i++)
        {
            for(j=0;j<i;j++)
            {
                if(word[j]&&wordDict.find(s.substr(j,i-j))!=wordDict.end())
                {
                    word[i]=true;
                    v[i].push_back(j);
                }
            }
        }
        if(!word[L])
            return ret;
        else
        {
            genRet(ret,s,v,stk,L);
            return ret;
        }
    }
    void genRet(vector<string>&ret,string s,vector<vector<int>>&v,stack<string>&stk,int L)
    {
        if(L==0)
        {
            string str;
            while(!stk.empty())
            {
                string top=stk.top();
                stk.pop();
                str+=top+" ";
            }
            str.erase(str.end()-1);
            ret.push_back(str);
        }
        else
        {
            for(int i=0;i<v[L].size();i++)
            {
                string tem=s.substr(v[L][i],L-v[L][i]+1);
                stk.push(tem);
                genRet(ret,s,v,stk,v[L][i]);
                stk.pop();
            }
        }
    }
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值