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 ...
随机推荐
- tomcat 运行异常Cannot create PoolableConnectionFactory (到主机 的 TCP/IP 联接失败)(用户sa登录失败)
这是在java web中启动tomcat遇到的问题,因为这个问题,整整折腾了两天的时间,找了很都解决方案,但终究还是不能正常.现在整理下这个问题的解决方案: 首先,出这个问题之前,请检查一下的问题,这 ...
- C#textbox右击弹出菜单
给窗口体拖一个contextMenuTrip 控件,也就是右键菜单控件,这时你就不要给这个控件写内容了, 选中textBox 然后点属性窗口,把它的contextMenuTrip 属性选中你刚才托的那 ...
- Unity3D之Vector3.Dot和Vector3.Cross的使用
在unity3d中,Vector3.Dot表示求两个向量的点积;Vector3.Cross表示求两个向量的叉积. 点积计算的结果为数值,而叉积计算的结果为向量.两者要注意区别开来. 在几何数学中: 1 ...
- ***CI分页:为CodeIgniter写的分页类
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...
- 彷徨中的成长-记一个文科生的IT成长过程
纠结了许久,要不要写这篇文章,然而最终还是写了.就权当总结与呻吟吧..当然,呻吟最开始还是发在自己的站点的,忍不住手贱,还是想发博客园. 1 剧透 人算不如天算:时隔多年,我竟然搞起了前端. 2 发端 ...
- [转]python -m SimpleHTTPServer
本文转自:http://www.cnblogs.com/congbo/archive/2012/11/15/2769704.html 如果你急需一个简单的Web Server,但你又不想去下载并安装那 ...
- Project Euler 77:Prime summations
原题: Prime summations It is possible to write ten as the sum of primes in exactly five different ways ...
- spring @qualifier注解
1.spring @qualifier注解用来在spring按类型装配可能存在多个bean的情况下,@qualifier注解可以用来缩小范围或者指定唯一. 也可以用来指定方法参数 2.@qualifi ...
- Java学习笔记之:Java String类
一.引言 字符串广泛应用在Java编程中,在Java中字符串属于对象,Java提供了String类来创建和操作字符串. 创建字符串最简单的方式如下: String str= "Hello w ...
- PowerDesigner中转换物理模型时的命名转换
原文:PowerDesigner中转换物理模型时的命名转换 最近在使用PowerDesigner建模数据库,在使用中积累了一些遇到的问题和解决办法,记录下来,希望对遇到同样问题的朋友有所帮助. 早 期 ...