题目地址:请戳我

这一题在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. UIWebView的基本用法

    一.UIWebView的基础使用 1.创建UIWebView: CGRect bouds = [[UIScreen manScreen]applicationFrame]; UIWebView* we ...

  2. javascript 定时器

    setTimeout()--用于指定在一段特定的时间后执行某段程序.           格式: [定时器对象名=]setTimeout("<表达式>",毫秒数); 功 ...

  3. JavaScript Patterns 2.8 Number Conversions with parseInt()

    Strings that start with 0 are treated as octal numbers (base 8) in ECMAScript 3; however, this has c ...

  4. PS网页设计教程XXVII——设计一个大胆和充满活力的作品集

    作为编码者,美工基础是偏弱的.我们可以参考一些成熟的网页PS教程,提高自身的设计能力.套用一句话,“熟读唐诗三百首,不会作诗也会吟”. 本系列的教程来源于网上的PS教程,都是国外的,全英文的.本人尝试 ...

  5. 读书笔记——Windows环境下32位汇编语言程序设计(13)关于EXCEPTION_DEBUG_INFO结构体

    在动手自己尝试编写书上第13章的例子Patch3时,遇到了一个结构体EXCEPTION_DEBUG_INFO. 这个结构体在MASM的windows.inc中的定义和MSDN中的定义不一样. (我使用 ...

  6. Ubuntu,QT5连接MySQL

    用QT连接MySQL需要共享库 libqsqlmysql.so的驱动,路径在plugin/sqldrivers目录下,乍看已经可用了,其实不然. 用ldd命令分析一下,libmysqlclient_r ...

  7. Linux Strip

    一.简介 strip经常用来去除目标文件中的一些符号表.调试符号表信息,以减小程序的大小. 二.语法 https://sourceware.org/binutils/docs/binutils/str ...

  8. Count and Say

    Count and Say https://leetcode.com/problems/count-and-say/ The count-and-say sequence is the sequenc ...

  9. dba诊断之lock

    --产生锁的详细信息 select a.session_id, c.SERIAL#,d.spid, os_user_name, b.object_name,locked_mode,    c.sql_ ...

  10. Unity3d 内置图形界面系统(Editor GUI)

    一.说在前面的 1.unity内置的ui系统,无论是在性能的表现上.功能的强大性上.制作复杂ui的便捷性上,还是其它的一些方面都不如一些第三方的插件来的好,如:NGUI和DF-GUI(PS: 后者比前 ...