140. Word Break II (String; DP,DFS)
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"].
思路:利用I的dp判断是否有解,如果有解,使用DFS存储结果(类似 Palindrome Partitioning)
class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
//dp[i]: s[0...i] can be egemented in dict
//dp[i] = dp[0][k] && d[k][i]
int len = s.length();
vector<bool> dp(len,false);
for(int i = ; i < len; i++){
if(wordDict.find(s.substr(,i+))!=wordDict.end()) dp[i]=true;
for(int j = ; j <= i; j++){
if((wordDict.find(s.substr(j,i-j+))!=wordDict.end()) && dp[j-]) dp[i]=true;
}
}
if(dp[len-]) dfs(s,,"",wordDict);
return ret;
}
void dfs(string s, int depth, string strCur, unordered_set<string>& wordDict){
for(int i = depth; i < s.length(); i++){
if(wordDict.find(s.substr(depth,i-depth+))==wordDict.end()) continue;
if(i==s.length()-) ret.push_back(strCur+s.substr(depth,i-depth+));
else dfs(s,i+,strCur+s.substr(depth,i-depth+)+" ", wordDict);
}
}
private:
vector<string> ret;
};
140. Word Break II (String; DP,DFS)的更多相关文章
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- 140. Word Break II(hard)
欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...
- 140. Word Break II
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- 【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(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- [LeetCode] 140. Word Break II 单词拆分II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...
- 139. Word Break 以及 140.Word Break II
139. Word Break Given a non-empty string s and a dictionary wordDict containing a list of non-empty ...
- LeetCode笔记:140. Word Break II
题目描述 给定一个非空的字符串s,一个非空的字符串list作为字典.通过在s中添加空格可以将s变为由list中的word表示的句子,要求返回所有可能组成的句子.设定list中的word不重复,且每一个 ...
- 【LeetCode】140. Word Break II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...
随机推荐
- 安全测试chicklist
- POJ 3666 Making the Grade (线性dp,离散化)
Making the Grade Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) T ...
- sublime text 3 3143
下载链接:https://download.sublimetext.com/Sublime%20Text%20Build%203143%20x64%20Setup.exe 注册信息:sublime t ...
- Nginx的启动、停止、重启
启动 启动代码格式:nginx安装目录地址 -c nginx配置文件地址 例如: [root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /us ...
- oracle 11g RAC 的一些基本概念(三)
Grid Infrastructure共享组件 Grid Infrastructure使用两种类型的共享设备来管理集群资源和节点:OCR(Oracle Cluster Registry)和表决磁盘 ...
- spark SQL概述
Spark SQL是什么? 何为结构化数据 sparkSQL与spark Core的关系 Spark SQL的前世今生:由Shark发展而来 Spark SQL的前世今生:可以追溯到Hive Spar ...
- jenkins API
1.curl http://199.168.299.99:8080/job/send_message/lastBuild/api/json --user administrator:1234 获取j ...
- 转载 深入理解java类加载器
1 基本信息 每个开发人员对java.lang.ClassNotFoundExcetpion这个异常肯定都不陌生,这背后就涉及到了java技术体系中的类加载.Java的类加载机制是技术体系中比较核心的 ...
- distinct group by
select num from test_test group by num; 比 select distinct(num) from test_test; 效率高 select count(dis ...
- zabbix微信报警
[root@LinuxS04 jiaoben]# ./weixin 联系人 baojing baojingok[root@LinuxS04 jiaoben]# pwd/usr/local/zabbix ...