LeetCode:Word Break II(DP)
题目地址:请戳我
这一题在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)的更多相关文章
- 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 ...
- 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 ...
- [leetcode]Word Break II @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words ...
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [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 ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- [LeetCode] Word Break II 解题思路
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- 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 ...
- [Leetcode] word break ii拆分词语
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
随机推荐
- JavaScript Patterns 2.9 Coding Conventions
It’s important to establish and follow coding conventions—they make your code consistent, predictabl ...
- 【mysql】使用tpcc-mysql进行压力测试
Tpcc-mysql是percona基于tpcc衍生出来专用于mysql基准测试的产品 ,可以参见 <高性能MySQL第三版> 一.安装 rpm -Uvh http://dl.fedora ...
- QT的QWebView显示网页不全
最近使用QWebView控件遇到一个问题,就是无论窗口多大,网页都显示那么大,而且,显示不完全,有滚动条 试过使用showMaximized()方法, 还是一样,网上一直说是布局问题,也没说清楚是虾米 ...
- ffmpeg2.2在ubuntu下使用NDK编译——并在android工程下测试使用
作者:wainiwann 出处:http://www.cnblogs.com/wainiwann/ 本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则 ...
- 虚拟机centos6.5 --VirtualBox设置全屏
一.安装以下模块 yum install kernel-devel kernel-headers gcc,然后重启. 二.安装增强功能 安装失败,查看日志文件,cat /var/log/vboxadd ...
- Rhythmbox中文乱码问题的解决
Rhythmbox中文乱码问题的解决 Rhythmbox是Ubuntu自带的一款很优秀的音乐播放器,但是在处理中文时却不太友好,导入歌曲时中文会变成乱码 这个问题也是很好解决的. ** 1.Ctrl+ ...
- xcode 自定义include路径
- java 字节流和字符流的区别 转载
转载自:http://blog.csdn.net/cynhafa/article/details/6882061 java 字节流和字符流的区别 字节流与和字符流的使用非常相似,两者除了操作代码上的不 ...
- mvc area区域和异步表单,bootstrap简单实例
码农最怕眼高手低 今天来练习mvc Area技术和bootstrap以及异步表单的C#代码实现. 1.area区域架构对于建立复杂业务逻辑很有帮助,由 AreaRegistration.Regist ...
- 【OpenGL】交互式三次 Bezier 曲线
1. 来源 三次贝塞尔曲线就是依据四个位置任意的点坐标绘制出的一条光滑曲线 2. 公式 3. 实现 #include <iostream> #include <math.h> ...