Given a string s and a dictionary of wordsdict, 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) {
vector<bool> f(s.size()+1, false);
vector<vector<bool> > prev(s.size()+1, vector<bool>(s.size()));
f[0] = true; // empty string for(int i=1; i<=s.size(); ++i)
{
for(int j=i-1; j>=0; --j){
if(f[j] && dict.find(s.substr(j, i-j)) != dict.end()){
f[i] = true;
prev[i][j] = true;
}
}
}
vector<string> path;
vector<string> result;
genPath(s, prev, s.size(), path, result); return result;
}
private:
void genPath(const string& s, const vector<vector<bool> >&prev, int cur, vector<string>&path, vector<string>&result){
if(cur == 0){
string tmp;
for(auto iter = path.rbegin(); iter != path.rend(); ++iter)
tmp += *iter + ' ';
tmp.erase(tmp.end()-1);
result.push_back(tmp);
}
for(int j=0; j<s.size(); ++j){
if(prev[cur][j]){
path.push_back(s.substr(j, cur-j));
genPath(s, prev, j, path, result);
path.pop_back();
}
}
}
};

LeetCode---Word Break 2的更多相关文章

  1. [LeetCode] Word Break II 拆分词句之二

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

  2. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  3. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  4. [leetcode]Word Break II @ Python

    原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words  ...

  5. LeetCode: Word Break II 解题报告

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  6. LeetCode ||& Word Break && Word Break II(转)——动态规划

    一. Given a string s and a dictionary of words dict, determine if s can be segmented into a space-sep ...

  7. [LeetCode] Word Break II 解题思路

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

  8. [Leetcode] word break ii拆分词语

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

  9. LeetCode: Word Break I && II

    I title: https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, ...

  10. [LeetCode] Word Break 拆分词句

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

随机推荐

  1. 杭JS

    杭JS的视频终于出来了.看了Garry Yao的视频依旧看不懂...总体上感觉是在职场上,我不在孤单了,勇敢的前进吧! 玉伯: 视频看了很多,什么情绪管理实践管理,最值得学习的就是知识管理定律了,找到 ...

  2. HTML5自学笔记[ 2 ]新增表单控件和表单属性

    新增<input>属性type="email",自动验证,若输入不为邮箱,则不能提交. 新增<input>属性type="tel",在移 ...

  3. HTTP协议中PUT和POST使用区别 【转载】

    有的观点认为,应该用 POST来创建一个资源,用PUT来更新一个资源:有的观点认为,应该用PUT来创建一个资源,用POST来更新一个资源:还有的观点认为可以用PUT和 POST中任何一个来做创建或者更 ...

  4. html 标签总结

    <q>标签,短文本引用 例如“聪明秀出为之英,胆略过人为之雄.” <blockquote>标签,长文本引用 <blockquote>标签的解析是缩进样式. 没有HT ...

  5. 报错总结_java.lang.RuntimeException: Invalid action class configuration that references an unknown class name

    在使用SSH进行项目开发时,一不小心就可能出现以上的错误提示. 这样的问题可以简单理解为未找到名字为XXX的action 1)xxxAction没有在Struts.xml中配置相应的action: 大 ...

  6. HADOOP :: java.lang.ClassNotFoundException: WordCount

    I am using eclipse to export the jar file of a map-reduce program. When i am run the jar using comma ...

  7. DB2存储过程语法规则

    如何声明一个存储过程CREATE PROCEDURE 存储过程名(IN 输入变量名 输入变量类型,OUT 输出变量名 输出变量类型)紧跟其后的是存储过程属性列表            常用的有:LAN ...

  8. 本地电脑localhost指向127.0.0.1的配置

    windows系统电脑,我们如果想访问本机部署的项目,通常使用的是localhost来指向本机,但是有时候发现不行,我们不妨打开资源管理器,C:\Windows\System32\drivers\et ...

  9. 用Navicat for MYsql创建表,插入中文显乱码

    段都有编码设置.出现乱码肯定是你现在用的编码混乱造成的 解决办法: 第一步 先改数据库编码 先修改你的数据库,如果你页面用的是UTF-8编码那么你数据库内的编码也需要设置为UTF-8,每个字段都需要设 ...

  10. 前端必须掌握30个CSS3选择器

    也许你已经学会了CSS的三个简单常用的选择器:#ID,.class,标签选择器,可是这些就足够了吗?随着CSS3的到来,作为前端开发者需要掌握下面三十个基本的选择器,这样才可以在平时开发中得心用手. ...