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 ...
随机推荐
- Java 基础知识点(必知必会其二)
1.如何将数字输出为每三位逗号分隔的格式,例如“1,234,467”? package com.Gxjun.problem; import java.text.DecimalFormat; impor ...
- springMVC 验证器
采用Hibernate-validator来进行验证,Hibernate-validator实现了JSR-303验证框架支持注解风格的验证.首先我们要到http://hibernate.org/val ...
- MVC中view页面用jquery方法绑定select控件值
var sortid = '@Model.myWorkMatter.WorkMatterSortID'; $("#selectSort").val(sortid); $(" ...
- SQL Server数据库(SQL Sever语言 CRUD)
使用SQL Sever语言进行数据库的操作 常用关键字identity 自增长primary key 主键unique 唯一键not null 非空references 外键(引用) 在使用查询操作数 ...
- angularJs项目实战!03:angularjs与其他类库的协作(转)
angularjs,在我看来是个中等重量级的框架.即不像backbone那么简单,也不像dojo和Yui那么包罗万象.很多时候,妄图包罗万象,往往会出现很多子模块的质量高不成低不就,并且修改起来较为困 ...
- [转载]DataSet导入到Excel文件
/// <summary> /// 将数据导入到Excel /// </summary> /// <param name="ds"& ...
- CSS3卷角
众所周知,border-radius 属性可以用来设置圆角,但很少人知道它还可以做很多不规则的犄角.卷角(rounded corners) 工作原理: 一.独立属性:border-bottom-lef ...
- 用PDB库调试Python程序
Python自带的pdb库,发现用pdb来调试程序还是很方便的,当然了,什么远程调试,多线程之类,pdb是搞不定的. 用pdb调试有多种方式可选: 1. 命令行启动目标程序,加上-m参数,这样调用my ...
- 常州培训 day3 解题报告
第一题: 给出数轴正半轴上N个点的坐标和其权值,给出初始体力值M,人一开始在位置0,体力值会随着走过路程的增加而增加,走多少个单位的路消耗多少体力值.到每个点可以打掉,消耗的体力值就是其权值.求 最多 ...
- Power string(poj 2406)
题目大意,给出一个字符串s,求最大的k,使得s能表示成a^k的形式,如 abab 可以表示成(ab)^2: 方法:首先 先求kmp算法求出next数组:如果 len mod (len-next[len ...