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可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
随机推荐
- [Shell] 文件名截取的问题:bash .vs. csh
参考: http://bbs.chinaunix.net/thread-1825455-1-1.html 但是, 经常处理更复杂的文件名, 这里给出一个加长版的例子, 换汤不换药. 查询当前shell ...
- OpenGl在VS中的配置
刚开始接触OpenGl的时候难免会遇到一些问题,这些问题可能和程序无关,只是一些编译环境的设置和头文件的安装,特别整理了一下,如下: (1)将gult32.dll,glut.dll复制到windows ...
- 模仿JavaAppArguments.java示例,编写一个程序,此程序从命令行接收多个数 字,求和之后输出结果,写出其的设计思想、程序流程图、源程序代码。
一 设计思想 首先现在file中建立一个类,并把任务名和类名写上(注意类名的大写):第二步则是参数的输入,并且定义求和变量:第三步则是对参数数据类型的要求,要把字符类型转化为整数类型并输出(这也是本道 ...
- iOS开发_数据存储方式
对于数据持久化的问题,博主并不准备在博文内放很多的代码进行更深一步解释,只是简单的介绍一下四种数据持久化,如有时间,会另外针对各个数据持久化的方法进行更进一步的阐述. 直接进入主题: 〈1.NSUse ...
- LeetCode 3 Longest Substring Without Repeating Characters 区间,想法 难度:1
https://leetcode.com/problems/longest-substring-without-repeating-characters/ 思路:从某点结束所能取到的最早开头是到目前出 ...
- Haskell List相关操作
一.List中只能存放相同类型的元素. ++:将两个List合并成一个List.例,[1,2,3]++[4,5,6],结果是[1,2,3,4,5,6]. : :将“:”前面的元素添加到“:”后面的L ...
- bzoj 3172 单词 ac自动机|后缀数组
题目大意: 给定n个字符串连成了一篇文章,问每个字符串在这篇文章中出现的次数,可重复覆盖 这里ac自动机和后缀数组都可以做 当然后缀数组很容易就解决,但是相对时间消耗高 这里就只讲ac自动机了 将每个 ...
- 使用Quartz.NET进行任务调度管理
1.Quartz.NET 介绍 Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用 ...
- 010 winform
2016-01-23 1.winform应用程序是一种智能客户端技术,我们可以使用winform应用程序帮助我们获得信息或者传输信息等. 2.属性Name:在后台要获得前台的控件对象,需要使用Name ...
- JS 笔记(二) - 函数
1. 函数的 声明 1) 声明式写法 function j1(id){ alert(id); } 2) 声明匿名函数变量 var j2 = function (a, b) { alert(a + &q ...