题目地址:请戳我

这一题在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. sql 存储过程中top 后面跟参数的问题

    之前存储过程中有top的情况,都是拼接sql,然后通过exec执行,进行查询结果,很不方便. 今天研究了,原来top后面是可以直接写参数的. 只需要top 后面的参数加上小括号就好了 eg: TOP ...

  2. 在Asp.net MVC中使用Authorization Manager (AzMan)进行Windows用户身份认证

    背景 创建需要通过Windows用户进行身份认证的Asp.net MVC应用 要点 在Asp.net MVC应用基于Windows用户进行身份认证的方法有很多,如MVC自带的Windows认证就经常被 ...

  3. python sorted用法

    python列表排序 python字典排序 sorted List的元素可以是各种东西,字符串,字典,自己定义的类等. sorted函数用法如下: sorted(data, cmp=None, key ...

  4. Effective Java 48 Avoid float and double if exact answers are required

    Reason The float and double types are particularly ill-suited for monetary calculations because it i ...

  5. IIS管理

    1.缓存的处理 http://www.cnblogs.com/dudu/p/iis_user-mode_caching_cache-control_public.html 2.负载均衡的使用 ARR ...

  6. 烂泥:ubuntu中使用virt-manager图形化新建虚拟机

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 上一篇文章介绍了,如何在ubuntu下安装KVM的虚拟机管理器virt-manager,这篇文章我们来介绍,如何在图形界面下使用virt-manager ...

  7. Super A^B mod C

    Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B ...

  8. Ubuntu 安装JDK并配置成为默认的JDK

    Ubuntu安装JDK 系统版本:Ubuntu 15.04 x64 JDK版本:jdk-8u60-linux-x64 1.查看系统位数,输入以下命令即可 getconf LONG_BIT 2.下载对应 ...

  9. CentOS 6.5上MySQL安装部署与入门。

    centos 6.5 yum 安装mysql1. 安装软件:yum install -y mysql-server mysql mysql-devel2.启动服务:service mysqld sta ...

  10. selenium操作滚动条的几种方式

    1.操作滚动条到当前可见视图的元素位置 WebElement element = dr.findElement(By.id("4")); ((JavascriptExecutor) ...