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 ...
随机推荐
- java中判空
一.概述 java中判等似乎很简单,==用来判断对象引用(内存地址)是否相同,equals用来判断值是否相同.你可以试用String对象轻松区分这一点. 那么在null判等(也就是判空操作)时呢? 可 ...
- uva 11076
计算出每一位上数字i会出现的次数 累加 #include <cstdio> #include <cstdlib> #include <cmath> #includ ...
- 一个IT人士的个人经历,给迷失方向的朋友
这些日子我一直在写一个实时操作系统内核,已有小成了,等写完我会全部公开,希望能够为国内IT的发展尽自己一份微薄的力量.最近看到很多学生朋友和我当年一样没有方向 ,所以把我的经历写出来与大家共勉,希望能 ...
- java基础知识回顾之---java String final类普通方法的应用之“子串在整串中出现的次数”
/* * 2 一个子串在整串中出现的次数. * "loveerlovetyloveuiloveoplove" * 思路: * 1,要找的子串是否存在,如果存在获取其出现的位置.这个 ...
- git init 与 git init --bare 的区别
git init 和 git init –bare 的区别 使用命令"git init --bare"(bare汉语意思是:裸,裸的)初始化的版本库(暂且称为bare repos ...
- hdu 2481 Toy
好题!!!没话说…… 用到的知识面很多,这题的难点在于公式的推导. 原始推导过程见:http://hi.baidu.com/spellbreaker/item/d8bb3bda5af30be6795d ...
- mysql 中 isnull 和 ifnull 判断字段是否为null
对于统计count(type)和avg(type) 都不起作用 SQL中有ISNULL方法,介绍如下: ISNULL使用指定的替换值替换 NULL. 语法ISNULL ( check_expressi ...
- hdu1162Eddy's picture
http://acm.hdu.edu.cn/showproblem.php?pid=1162 最小生成树 #include<iostream> #include<stdio.h> ...
- lintcode 中等题:majority number III主元素III
题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O( ...
- lintcode:Compare Strings 比较字符串
题目: 比较字符串 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母 样例 给出 A = "ABCD" B = "ACD" ...