LeetCode: Word Break I && II
I
title:
https://leetcode.com/problems/word-break/
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given
s = "leetcode"
,
dict = ["leet", "code"]
.
Return true because "leetcode"
can be segmented as "leet code"
.
思路:一开始就考虑直接使用dfs超时了。。。
class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
if (s.size() == )
return true;
string tmp;
for (int i = ; i < s.size(); i++){
tmp.push_back(s[i]);
if (wordDict.find(tmp) != wordDict.end()){
string tmp1(s.begin()+i+,s.end());
if (wordBreak(tmp1,wordDict))
return true;
}
}
return false;
}
};
其实对于这样的问题,是可以想到是可以使用动态规划的。
一个DP问题。定义possible[i] 为S字符串上[0,i]的子串是否可以被segmented by dictionary.
那么
possible[i] = true if S[0,i]在dictionary里面
= true if possible[k] == true 并且 S[k+1,j]在dictionary里面, 0<k<i
= false if no such k exist.
class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
int len = s.size();
vector<bool> dp(len+,false);
dp[] = true;
for (int i = ; i < len+; i++){
for (int k = ; k < i; k++){
if (dp[k] && wordDict.find(s.substr(k,i-k)) != wordDict.end()){
dp[i] = true;
break;//break是因为题目只是需要判断是否存在
}
}
}
return dp[len];
}
};
II
https://leetcode.com/problems/word-break-ii/
title:
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"].
思路:使用dp,并且保存中间结果。注意,在I中找到一个解就直接break,这里需要记录下这个信息。另外prev[i][j]为true表示字串[j,i]在字典中
class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string>& wordDict) { int len = s.size();
vector<bool> dp(len+,false);
vector<vector<bool> > prev(len+,vector<bool>(len,false));
dp[] = true;
for (int i = ; i < len+; i++){
for (int k = ; k < i; k++){
if (dp[k] && wordDict.find(s.substr(k,i-k)) != wordDict.end()){
dp[i] = true;
prev[i][k] = true;
}
}
}
vector<string> result;
vector<string> results;
genPath(len,s,dp,prev,result,results);
return results;
} void genPath(int cur,string s, vector<bool> dp,vector<vector<bool> > prev, vector<string> &result,vector<string> &results){
if (cur == ){
string tmp;
for (int i = result.size()-; i > ; i--){
tmp += result[i] + " ";
}
tmp += result[];
results.push_back(tmp);
return ;
}
for (int i = ; i < s.size(); i++){
if (prev[cur][i]){
result.push_back(s.substr(i,cur-i));
genPath(i,s,dp,prev,result,results);
result.pop_back();
}
}
}
};
LeetCode: Word Break I && II的更多相关文章
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- 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 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
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- [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 @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words ...
- 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 Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
随机推荐
- Window 2008 R2 + IIS7.5 + VS2013 错误代码 0x80070002
HTTP 错误 404.0 - Not Found 您要找的资源已被删除.已更名或暂时不可用.详细错误信息模块 IIS Web Core通知 MapRequest Handler处理程序 Static ...
- phonegap 环境搭建
经过了一番讨论,最后还是决定用phonegap来开发产品.因为用phonegap的人力成本相比原生开发还是节省了不少,并且可以跨平台.至于软件的运行效率,在ios上还是相当流畅的,在android上就 ...
- nginx模块开发(18)—日志分析
1.日志简介 nginx日志主要有两种:访问日志和错误日志.访问日志主要记录客户端访问nginx的每一个请求,格式可以自定义:错误日志主要记录客户端访问nginx出错时的日志,格式不支持自定义.两种日 ...
- Dijsktra算法C++实现
Dijsktra算法解决了有向图G=(V,E)上带权的单源最短路径问题.但要求所有边的权值非负. 思想:Dijkstra算法中设置了一顶点集合S,从源点s到集合中的顶点的最终最短路径的权值均已确定.算 ...
- poj 3735 Training little cats(矩阵快速幂,模版更权威,这题数据很坑)
题目 矩阵快速幂,这里的模版就是计算A^n的,A为矩阵. 之前的矩阵快速幂貌似还是个更通用一些. 下面的题目解释来自 我只想做一个努力的人 @@@请注意 ,单位矩阵最初构造 行和列都要是(猫咪数+1) ...
- GET和POST的区别,就是明信片和信封的区别
- Project Euler 92:Square digit chains 平方数字链
题目 Square digit chains A number chain is created by continuously adding the square of the digits in ...
- Project Euler 95:Amicable chains 亲和数链
Amicable chains The proper divisors of a number are all the divisors excluding the number itself. Fo ...
- lintcode :Binary Tree Preorder Traversal 二叉树的前序遍历
题目: 二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. 挑战 你能使用非递归实现么? 解题: 通过递 ...
- 如何配置JAVA的环境变量、Tomcat环境变量
配置JAVA环境变量 1.右击[我的电脑]---[属性]-----[高级]---[环境变量],如图: 2.选择[新建系统变量]--弹出“新建系统变量”对话框,在“变量名”文本框输入“JAVA_HOME ...