【Word Break II】cpp
题目:
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"].
代码:
class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string>& wordDict)
{
vector<string> ret;
vector<string> tmp;
vector<bool> possible(s.size(), true);
Solution::dfs( possible, wordDict, ret, tmp, s, , s.size()-);
return ret;
}
static void dfs(
vector<bool>& possible, // possible[i] : if s[i~end] can be possible word broken
unordered_set<string>& wordDict,
vector<string>& ret,
vector<string>& tmp,
string& s, int begin, int end )
{
if ( begin>end )
{
string str = "";
for ( int i=; i<tmp.size(); ++i ) { str = str + tmp[i] + " "; }
ret.push_back(str.substr(,str.size()-));
}
for ( int i=begin; i<=end; ++i )
{
if ( wordDict.find(s.substr(begin,i-begin+))!=wordDict.end() && possible[i] )
{
tmp.push_back(s.substr(begin,i-begin+));
int oriSolution = ret.size();
Solution::dfs( possible, wordDict, ret, tmp, s, i+, end);
if ( oriSolution==ret.size()) possible[i]=false;
tmp.pop_back();
}
}
}
};
tips:
其实在word break i这道题的时候就想用dfs做,但是会超时。
word break ii这道题是求所有可行解,就尤其想用dfs来做。
一开始写了一版裸dfs的代码,发现会超时:原因是没有剪枝。
这里学习了一下大神的剪枝技巧(http://fisherlei.blogspot.sg/2013/11/leetcode-wordbreak-ii-solution.html)
这里的possible[i] 代表的是 s[i+1:s.size()-1] 可否被给定的wordDict来word break。翻译过来就是,从i往后(不包括i)是否行可以被wordDict表示。
这个思路很精妙:
1. 从给定当前点往后看,看能否满足条件。这样dfs下次再走到这个点的时候,就知道是否可以往下走了。
2. 为什么不把possible[i]当成s[0~i]是否满足条件呢?因为能来到位置i的方式有很多种,一种方式行不通不代表其他方式行不通
3. 由i往后,一直到end,已经把所有可能走到最后的方式都包括了,如果所有可能走到最后的方式中都行不通,那就是肯定行不通了
4. 如何记录是否行得通了呢?我就是卡在这里了,没想到太好的办法。这时学习了大神的办法,比较下解集的个数:如果个数没变,那肯定是行不通了。
===============================================
第二次过这道题,复习遍原来的方法。
class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string>& wordDict)
{
vector<string> ret;
vector<string> tmp;
vector<bool> possible(s.size(),true);
Solution::dfs(ret, tmp, , s.size()-, s, wordDict, possible);
return ret;
}
static void dfs(
vector<string>& ret,
vector<string>& tmp,
int begin,
int end,
string& s,
unordered_set<string>& wordDict,
vector<bool>& possible
)
{
if ( begin>end )
{
string str = "";
for ( int i=; i<tmp.size(); ++i ) str += tmp[i] + " ";
ret.push_back(str.substr(,str.size()-));
return;
}
for ( int i=begin; i<=end; ++i )
{
if ( wordDict.find(s.substr(begin, i-begin+))!=wordDict.end() && possible[i] )
{
tmp.push_back(s.substr(begin, i-begin+));
int pre = ret.size();
Solution::dfs(ret, tmp, i+, end, s, wordDict, possible);
if ( ret.size()==pre ) possible[i] = false;
tmp.pop_back();
}
}
}
};
【Word Break II】cpp的更多相关文章
- 【Word Ladder II】cpp
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- 【Unique Paths II】cpp
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 【palindrome partitioning II】cpp
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- 【Jump Game II 】cpp
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- 【Path Sum II】cpp
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- 【Spiral Matrix II】cpp
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- 【Combination Sum II 】cpp
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 【Single Num II】cpp
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
- 【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 ...
随机推荐
- C# 常见的字符串操作
例1: 遍历字符串中的每一个字符: string src = "aa-b - c-a - d-e- d-e- a- a-b-cc"; foreach(char c in src) ...
- VS中生成网站和发布网站的区别
VS中生成网站和发布网站的区别 生成网站:是网站项目的编译. 我们知道像一样的C#编译性语言,在运行程序的时候,首先都要经过编译成计算机识别的二进制代码,才能运行.还有网站编译后,浏览 ...
- 更新浏览器,导致编写脚本报错Message: Unable to find a matching set of capabilities
卸载更新浏览器后,所编写的脚本无法运行,报如下的错误:selenium.common.exceptions.WebDriverException: Message: Unable to find a ...
- JAVA时间加工类
/** * 当天凌晨 */ public static Calendar startOfDay(Calendar c) { if (c == null) { return c; } c.set(Cal ...
- Samuraiwtf-解决sshd安装启动
东西没仔细看就给别人了,具体是系统默认的sshd怎么都起不来.于是重新安装一下,过程如下 root@samuraiwtf:~# netstat -nlt Active Internet connect ...
- LeetCode 4Sum 4个数之和
题意:这是继2sum和3sum之后的4sum,同理,也是找到所有4个元素序列,满足他们之和为target.以vector<vector<int>>来返回,也就是二维的,列长为4 ...
- 如何修改集群的公网信息(包括 VIP) (文档 ID 1674442.1)
适用于: Oracle Database - Enterprise Edition - 版本 11.2.0.3 到 12.1.0.2 [发行版 11.2 到 12.1]本文档所含信息适用于所有平台 用 ...
- Go - 环境安装
目录 你好,Go语言 环境安装 目录结构 命令 开发工具 学习网址 小结 你好,Go语言 Go 是一个开源的编程语言,它能让构造简单.可靠且高效的软件变得容易. 因工作需要,准备入坑,先从环境安装开始 ...
- 欠采样(undersampling)和过采样(oversampling)会对模型带来怎样的影响
项目中出现了二分类数据不平横问题,研究总结下对于类别不平横问题的处理经验: 为什么类别不平横会影响模型的输出? 许多模型的输出类别是基于阈值的,例如逻辑回归中小于0.5的为反例,大于则为正例.在数据不 ...
- python_56_递归
在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数.(最大调用自己999次) def calc(n): print(n) if int(n/2)>0: retur ...