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 ...
随机推荐
- HDU 2602(01背包)
Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- NABCD模型(猫咪记单词)
项目需求分析与建议-NABCD模型(猫咪记单词) N (Need 需求) 对于现在的学生,尤其是大学生来说,学习英语是一件非常重要的事.我们有四级六级托福雅思等各种各样的英语方面的考试.而学习英语 ...
- 集群(cluster)原理(转)
1.什么是集群 集群(cluster)就是一组计算机,他们作为整体向用户提供一组网络资源.这些单个的计算机系统就是集群的节点(node).一个理想的集群是,用户从不会意识到集群系统底层的节点,在他 ...
- PHP+mysql常用类库
<?php /** * @title: Ekcms mysql类库 * @version: 1.0 * @author: perry <perry@1kyou.com> * @pub ...
- mysql有回滚,php没有回滚的说法
mysql 事务表是有回滚的说法.当发生mysql层面的错误才会执行回滚
- L1 - 闭包和原型链
先来一炮尝尝: var i = 10; function myFunc(){ var i = 20; function innerFunc(){ alert(i); } return innerFun ...
- Apache—DBUtils
简介 commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影 ...
- C# 数据流操作 Stream 相关
FileStream:對文件執行讀取與寫入 MemoryStream:對內存進行讀取與寫入 BufferedStream:對緩沖器進行讀取與寫入 StreamReader/StreamWriter 命 ...
- CodeForces 468A Program F
Description Little X used to play a card game called "24 Game", but recently he has found ...
- 这个setDefaultCloseOperation写不写的区别是什么?
2009-03-23 13:40提问者采纳 设置用户在此窗体上发起 "close" 时默认执行的操作.必须指定以下选项之一: DO_NOTHING_ON_CLOSE(在 W ...