Word Break II
Given a string s and a dictionary of words dict, 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"].
DFS+剪枝
是前面那拆分词语的扩展,除了要判断能否由字典里的词语组成,还要求出所有的组合。
单纯的DFS会TLE,需要剪枝。
class Solution{
private:
void helper(string& s,unordered_set<string>& wordDict,int start,vector<bool>& possible,string path,vector<string>& res){
if(start == int(s.length())){
res.push_back(path);
return;
}
for(int i = start;i<int(s.length());i++){
string word = s.substr(start,i-start+);
if(wordDict.find(word) != wordDict.end() && possible[i+]){
if(start == )
path.append(word);
else{
path.append(" ");
path.append(word);
}
int oldsize = res.size();
helper(s,wordDict,i+,possible,path,res);
if(int(res.size()) == oldsize) possible[i+] = false;
if(start == )
path.resize(path.size() - word.size());
else
path.resize(path.size() - word.size()-);
}
}
}
public:
vector<string> wordBreak(string s,unordered_set<string>& wordDict){
vector<string> res;
vector<bool> possible(s.size()+,true);
helper(s,wordDict,,possible,"",res);
return res;
}
};
Word Break II的更多相关文章
- 【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 ...
- 17. Word Break && Word Break II
Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...
- LeetCode之“动态规划”:Word Break && Word Break II
1. Word Break 题目链接 题目要求: Given a string s and a dictionary of words dict, determine if s can be seg ...
- 【LeetCode】140. 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 解题报告
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- [Leetcode Week9]Word Break II
Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...
- 【Word Break II】cpp
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- 140. Word Break II(hard)
欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
随机推荐
- 搭建apache http服务器
异步: http://blog.csdn.net/lzhlzz/article/details/39496285
- iOS检测版本更新
有时候为了需求,我们需要检测app版本更新今天在这里整合下 //获取当前版本号 NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDi ...
- 白皮 Chapter 2
7.2 做题一遍就过的感觉简直太美好啦~然而我并没有测试数据QAQ //program name digit #include<cstdio> #include<iostream&g ...
- 动态加载jQuery
success: function(data){ for(var i in data){ $('.x-details>ul:eq(0)').append("<li>&quo ...
- 用Advanced Installer制作DotNetBar for Windows Forms 12.0.0.1_冰河之刃重打包版详解
关于 DotNetBar for Windows Forms 12.0.0.1_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版-------------- ...
- Leetcode 给一个数a和一个向量b,找出该向量b中的2个数相加等于a,并输出这两个数在向量中的位置
看C++primer Plus看的无聊,第一次做Leetcode的练习,本来想做二维向量的,结果始终通不过,查了原因,必须用一维的... 一维的答案: class Solution { public ...
- Sprint 2(第一天)
Sprint 2计划会议: 目标: 1.实现用户模块的权限控制,能够进行用户登录的功能 2.对菜单模块实现增加菜单列表详情,修改菜单列表详情,删除菜单列表详情,查询菜单列表详情的功能 3.实现菜品分类 ...
- 移动测试主要使用的测试框架,基于python
1.uiautomator google自己的测试框架,可以跨应用测试, 语言支持java,python也可以在GitHub上找到封装的包,去调用uiautomator. 2.Robotium jav ...
- mybatis学习2
解决字段名与实体类属性名不相同的冲突 1. 准备表和数据:CREATE TABLE orders(order_id INT PRIMARY KEY AUTO_INCREMENT,order_no VA ...
- JS中setInterval与setTimeout的区别
JS里设定延时: 使用SetInterval和设定延时函数setTimeout 很类似.setTimeout 运用在延迟一段时间,再进行某项操作. setTimeout("function& ...