(算法)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"
.
思路:
1、DFS
2、动态规划
代码:
#include<iostream>
#include<unordered_set>
#include<vector> using namespace std; // dp
bool WordBreak_dp(string s,unordered_set<string> &wordDict){
int n=s.size(); vector<bool> dp(n+,false);
dp[]=true; for(int i=;i<n;i++){
for(int j=i;j>=;j--){
if(!dp[j]) continue;
if(wordDict.find(s.substr(j,i-j+))!=wordDict.end()){
dp[i+]=true;
break;
}
}
}
return dp[n]; } // dfs
bool WordBreak_dfs(string s,unordered_set<string> &wordDict){
if(s=="")
return true;
for(int i=;i<s.size();i++){
if(wordDict.find(s.substr(,i+))!=wordDict.end()){
if(WordBreak_dfs(s.substr(i+),wordDict))
return true;
}
}
return false;
} int main(){
unordered_set<string> wordDict;
wordDict.insert("leet");
wordDict.insert("code");
string s;
while(cin>>s){
cout<< WordBreak_dp(s,wordDict) <<endl;
cout<< WordBreak_dfs(s,wordDict) <<endl;
}
return ;
}
(算法)Word Break的更多相关文章
- LeetCode140:Word Break II
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- [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
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- 17. Word Break && Word Break II
Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- Leetcode#139 Word Break
原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 140. Word Break II
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
随机推荐
- 使用winrar自解压功能制作安装包
参考文献: bat脚本设置文件的只读属性:http://wenda.tianya.cn/question/0f484c28ffd8d4e9 bat脚本创建internet快捷方式:http://www ...
- [Ubuntu] 编译安装 PHP 依赖库
编译环境 sudo apt-get -y install build-essential xml sudo apt-get -y install libxml2-dev pcre sudo apt-g ...
- 突破 BTrace 安全限制
http://blog.csdn.net/alivetime/article/details/6548615
- HDU 4568 SPFA + TSP
这道题是长沙邀请赛的题,当时是道签到题. 这种题还是很常见的,讲一下思路. 首先是预处理出每个宝藏之间的距离,还有到边的距离,直接对每个宝藏进行一次SPFA就可以了. 然后就是经典的求TSP的过程. ...
- Error opening wax scripts: loading wax stdlib: bad header in precompiled chunk
在64位ios操作系统中使用lua报错. Error opening wax scripts: loading wax stdlib: bad header in precompiled chunk ...
- 快速将wax配置到项目中进行lua开发
通过Finder浏览到你保存该项目的文件夹.创建三个新的文件夹:wax.scripts和Classes. 第一:首先,下载源代码的压缩包.Wax放在GitHub上(https://github.com ...
- iOS-Xcode必备插件XAlign:瞬间优化你的代码
今天向大家介绍一个非常好用的Xcode代码编辑插件,这个插件可以很快速地使代码对齐,有3种模式:“=”对齐.宏定义对齐和属性对齐 XAlign效果图 1.“=”对齐 2.宏定义对齐 3.属 ...
- 【ELK】【docker】【elasticsearch】2.使用elasticSearch+kibana+logstash+ik分词器+pinyin分词器+繁简体转化分词器 6.5.4 启动 ELK+logstash概念描述
官网地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#docker-cli-run-prod ...
- winform 给textbox 增加 或 减小字体大小 z
private void btnAddFont_Click(object sender, EventArgs e) { float fSize = this.txtResult.Font.Size; ...
- python测试开发django-36.一对一(OneToOneField)关系查询
前言 前面一篇在xadmin后台一个页面显示2个关联表(OneToOneField)的字段,使用inlines内联显示.本篇继续学习一对一(OneToOneField)关系的查询. 上一篇list_d ...