题目地址:请戳我

这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解。但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体可以对比两段代码),如果不去掉则会漏掉一些解(前一道题加约束条件是为了更快的判断是字符串是够能被分词,这里是为了找出所有分词的情况)

代码如下:

 class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string> &dict)
{
// Note: The Solution object is instantiated only once and is reused by each test case.
vector<string> result;
if(dict.empty())
return result;
const int len = s.size();
bool canBreak[len]; //canBreak[i] = true 表示s[0~i]是否能break
memset(canBreak, , sizeof(bool)*len);
bool **pre = new bool *[len];//如果s[k..i]是字典中的单词,则pre[i][k]=true
for(int i = ; i < len; i++)
{
pre[i] = new bool[len];
memset(pre[i], , sizeof(bool)*len);
} for(int i = ; i <= len; i++)
{
if(dictContain(dict, s.substr(, i)))
{
canBreak[i-] = true;
pre[i-][] = true;
}
if(canBreak[i-] == true)
{
for(int j = ; j <= len - i; j++)
{
if(dictContain(dict,s.substr(i, j)))
{
canBreak[j+i-] = true;
pre[j+i-][i] = true;
}
}
}
}
//return false;
vector<int> insertPos;
getResult(s, pre, len, len-, insertPos, result);
return result;
} bool dictContain(unordered_set<string> &dict, string s)
{
unordered_set<string>::iterator ite = dict.find(s);
if(ite != dict.end())
return true;
else return false;
} //在字符串的某些位置插入空格,返回新字符串
string insertBlank(string s,vector<int>pos)
{
string result = "";
int base = ;
for(int i = pos.size()-; i>=; i--)
{
if(pos[i] == )continue;//开始位置不用插入空格
result += (s.substr(base, pos[i]-base) + " ");
base = pos[i];
}
result += s.substr(base, s.length()-base);
return result;
} //从前驱路径中构造结果
void getResult(string s, bool **pre, int len, int currentPos,
vector<int>insertPos,
vector<string> &result)
{
if(currentPos == -)
{
result.push_back(insertBlank(s,insertPos));
//cout<<insertBlank(s,insertPos)<<endl;
return;
}
for(int i = ; i < len; i++)
{
if(pre[currentPos][i] == true)
{
insertPos.push_back(i);
getResult(s, pre, len, i-, insertPos, result);
insertPos.pop_back();
}
}
}
};

 【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3385644.html

LeetCode:Word Break II(DP)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [leetcode]Word Break II @ Python

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

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

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

  5. [LeetCode] Word Break II (TLE)

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

  6. LeetCode Word Break II

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

  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. HDU 2639 Bone Collector II (dp)

    题目链接 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took part in ...

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

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

随机推荐

  1. 简单的Windows登陆界面设计

    要求: 1.用户名必须为字母. //限定用户名必须为字母 private void txtName_KeyPress(object sender, KeyPressEventArgs e) { if ...

  2. linux 同步IO: sync msync、fsync、fdatasync与 fflush

    最近阅读leveldb源码,作为一个保证可靠性的kv数据库其数据与磁盘的交互可谓是极其关键,其中涉及到了不少内存和磁盘同步的操作和策略.为了加深理解,从网上整理了linux池畔同步IO相关的函数,这里 ...

  3. 处理 InterruptedException——Brian Goetz

    本文转自Brian Goetz大师在IBM的developerWorks中发布的文章: 中文地址:http://www.ibm.com/developerworks/cn/java/j-jtp0523 ...

  4. 【原创】Java实现手机号码归属地查询

    网络上已经有很多的手机号码归属地查询的API接口,但是这些接口总是有一些大大小小的缺陷. 总结一下这些缺陷: 1.要直接将它的搜索框链接形式粘到自己的页面,点击查询的时候还要跳转到他们的网站来展示归属 ...

  5. ORA-29857: domain indexes and/or secondary objects

    dmp导入的时候出了问题,想把表空间和用户删除重建,然后再重新导入,却在删除表空间时报错:   > ORA-29857: domain indexes and/or secondary obje ...

  6. jQuery Validate 表单验证插件----通过name属性来关联字段来验证,改变默认的提示信息,将校验规则写到 js 代码中

    一.下载依赖包 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW  访问密码 f224 二. 添加一个另外一个插件jquery.validate.messages_cn.js. ...

  7. Redis系列(三)—— 订阅/发布

    Redis 订阅/发布 参考:http://www.cnblogs.com/mushroom/p/4470006.html,http://www.tuicool.com/articles/ABry2a ...

  8. JavaScript生成GUID的算法

    全局唯一标识符(GUID,Globally Unique Identifier)也称作 UUID(Universally Unique IDentifier) . GUID是一种由算法生成的二进制长度 ...

  9. Linux ethtool命令

    一.简介 ethtool 是用于查询及设置网卡参数的命令. 二.常用选项 ethtool ethx //查询ethx网口基本设置,其中 x 是对应网卡的编号,如eth0.eth1等等 ethtool ...

  10. mac下 ssh免密码登陆设置

    由于mac os 是基于unix的操作系统终端和linux非常类似,所以不用借助类似于windows下的putty 和CRT工具即可远程登陆linux服务器,只需简单地3步即可免密码ssh远程. 1 ...