LeetCode---Word Break 2
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的更多相关文章
- [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(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- [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 解题报告
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- 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 解题思路
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [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 I && II
I title: https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, ...
- [LeetCode] Word Break 拆分词句
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
随机推荐
- js一些题目
假期在家,看到的,昨天看了下: 原文链接:http://wwwcqamin.duapp.com/?p=102#comment-7 请说出下面程序的输出结果 第一题: 1 2 3 4 5 6 7 8 9 ...
- python list对象
list对象 1.list定义l=['first','second'] 2.list追加对象list.append('aa');append的方法总是把元素追加到末尾 insert(索引号,'项目') ...
- ajax接触
1. function doSave() { ajax_get("${contextPath}/auth/functionsave", $("#editForm" ...
- js从千分位格式
从千分位格式化谈JS性能优化 http://heeroluo.net/article/detail/115 方法六 // 方法六 function toThousands(num) { ).toStr ...
- easyUI dialog 弹窗 居中显示
默认情况下应该是在屏幕居中显示的.但是有的时候没有居中只要重新纠正下就可以了 $('#add_dialog').dialog('open'); //打开添加对话框 $('#add_dialog').w ...
- Java 集合系列 08 Map架构
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...
- oracle优化原则(二)
SQL优化原则 二.SQL语句编写注意问题 www.2cto.com 下面就某些SQL语句的where子句编写中需要注意的问题作详细介绍.在这些where子句中,即使某些列存在索引,但是由于编写了劣质 ...
- 关于时间序列数据库的思考——(1)运用hash文件(例如:RRD,Whisper) (2)运用LSM树来备份(例如:LevelDB,RocksDB,Cassandra) (3)运用B-树排序和k/v存储(例如:BoltDB,LMDB)
转自:http://0351slc.com/portal.php?mod=view&aid=12 近期网络上呈现了有关catena.benchmarking boltdb等时刻序列存储办法的介 ...
- Windows Store App 应用程序存储空间
与上面介绍的三种不同应用程序数据存储类型对应,应用程序有三种数据存储空间,分别为本地应用程序数据存储空间.漫游应用程序数据存储空间和临时应用程序数据存储空间.通过使用ApplicationData类的 ...
- BZOJ3695 滑行
转化模型就变成几层折射率不同的玻璃光要怎么走才能从(0, 0)到(x, y) 我们发现第一次光线射出去的角度确定,之后光的行程是确定的 而且角度和最后到达y时的x成正相关,于是可以二分! 然后物理学学 ...