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 ...
随机推荐
- hive数据库的一些应用
1.创建表格create table usr_info(mob string,reason string,tag string) row format delimited fields termina ...
- 呆呆的io流输入输出的一些基础
关于io流的File类,下面来码一些基础属性: 文件的属性: /* public String getName() 获取文件的名字 public boolean canRead() 判断文件是否可以读 ...
- iOS开发几年了,你清楚OC中的这些东西么!!!?
iOS开发几年了,你清楚OC中的这些东西么!!!? 前言 几年前笔者是使用Objective-C进行iOS开发, 不过在两年前Apple发布swift的时候,就开始了swift的学习, 在swift1 ...
- UI-简答的BOL的取值塞值
不知道从什么时候开始,习惯用BOL MODEL来做一些东西的了.某个项目开始正式接触标准主数据的时候,开始了用MAINTAIN BAPI和BUPA的一些FM.然后在一段时间内是以此类的FM来开发的.B ...
- js基础练习---图片无缝左右滚动效果(主要以复制删除为主)
昨天闲来没事 看了下图片效果 发现这个方法j 就自己模仿下 上代码 当中有很多的纰漏 请大神们多多指教一二? <script type="text/javascript" ...
- thinkphp 调用系统的方法
在需要调用的脚本 加载 load('filename');//filename为文件名
- [Jquery]导航菜单效果-纵向
$( document ).ready( function(e){ var $catCont = $( ".cat-cont" ); //二级菜单div var $ca ...
- CCocos2Dx 一段遍历子节点的代码
CCLog("Lein will hide account!CS_FAST_REGISTER_REQ"); <p> CCNode* child1 = (CCNode*) ...
- Delphi 的 7zip 压缩算法
http://blog.csdn.net/warrially/article/details/8039915
- windows下安装openssh服务并实现远程登录
需要准备的工具: winscp 点击下载 openssh 点击下载 步骤: 在远程计算机安装 1.首先安装openssh,双击并安装 2.指定用户的home directory为C:\ ...