题目:

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的更多相关文章

  1. LeetCode140:Word Break II

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  2. [LeetCode] Word Break II 拆分词句之二

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  3. 【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 ...

  4. 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 ...

  5. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  6. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  7. 【LeetCode OJ】Word Break II

    Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...

  8. Leetcode#139 Word Break

    原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...

  9. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  10. 140. Word Break II

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

随机推荐

  1. 《廖雪峰 . Git 教程》学习总结

    基本上,Git就是以下面的命令顺序学习的.文中笔记是从廖雪峰老师的 Git教程 中总结出来的,方面查阅命令. 1.基础 git config --global user.name "Your ...

  2. Android 数据存储02之文件读写

    Android文件读写 版本 修改内容 日期 修改人 V1.0 原始版本 2013/2/25 skywang Android文件读写的有两种方式.一种,是通过标准的JavaIO库去读写.另一种,是通过 ...

  3. 理解 HTTPS 协议

    英文原文:Understanding HTTPS Protocol 最近我们看到很多站点使用 HTTPS 协议提供网页服务.通常情况下我们都是在一些包含机密信息的站点像银行看到 HTTPS 协议. 如 ...

  4. dotnetty 心跳

    IdleStateHandler 需要提供三个参数: readerIdleTimeSeconds, 读超时. 即当在指定的事件间隔内没有从 Channel 读取到数据时, 会触发一个 READER_I ...

  5. IP地址和子网划分学习笔记之《预备知识:进制计数》

    一.序:IP地址和子网划分学习笔记开篇 只要记住你的名字,不管你在世界的哪个地方,我一定会去见你.——新海诚 电影<你的名字> 在我们的日常生活中,每个人的名字对应一个唯一的身(敏)份(感 ...

  6. hbase经常使用的shell命令样例

    1.hbase shell    进入hbase [hadoop@mdw ~]$ hbase shell HBase Shell; enter 'help<RETURN>' for lis ...

  7. java项目实现流水号自动增长

    项目中有一个规则编号字段,从1开始,编号长度为5位,那么第一条数据编号就是00001. 实现的基本思路就是项目启动时,从数据库获取当前最大值,作为静态变量存储: 业务获取新的编码,考虑并发问题,获取编 ...

  8. 一幅图秒懂LoadAverage(负载)

    转自:http://www.habadog.com/2015/02/27/what-is-load-average/ 一幅图秒懂LoadAverage(负载)   一.什么是Load Average? ...

  9. 真实的人类第三季/全集Humans迅雷下载

    Channel 4及AMC宣布续订<真实的人类 Humans>第三季,下季为8集:新季拍摄将在秋季开始,主要角色会回归.该剧设定在机器人Synth被繁忙都市人广泛使用的世界,呈现人类与机器 ...

  10. CCLabelAtlas的宽度为奇数时的显示bug

    遇到一个很郁闷的bug,CCLabelAtlas设置文字内容在ipad上和android上正常,就只有iphone怎么显示都不正常.后来把它宽度 + 1,然后就正常了.发现以前宽度设置为21px.23 ...