题目:

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. OAuth2.0网页授权 提示未关注该测试号

    用无高级接口权限的公众号使用别人的appid和appsecret在网页中获取用户信息时,提示未关注该测试号. 搜集各种资料才发现是因为 测试帐号只能对关注者网页授权,正式帐号可以对未关注者授权

  2. delphi:临界区对象TCriticalSection(Delphi) 与 TRtlCriticalSection 的区别

    临界区对象TCriticalSection(Delphi) 与 TRtlCriticalSection 的区别 TRtlCriticalSection 是一个结构体,在windows单元中定义: 是I ...

  3. .Net Discovery系列之十一-深入理解平台机制与性能影响 (中)

    上一篇文章中Aicken为大家介绍了.Net平台的垃圾回收机制与其对性能的影响,这一篇中将继续为大家介绍.Net平台的另一批黑马—JIT.   有关JIT的机制分析   ● 机制分析   以C#为例, ...

  4. SQL:获取中文周几

    ) = case DatePart(DW, GetDate()) then '周日' then '周一' then '周二' then '周三' then '周四' then '周五' then '周 ...

  5. 黑吃黑第一季/全集Banshee迅雷下载

    第一季 Banshee Season 1 (2013)看点:“Banshee”在爱尔兰盖尔语中被称为“bean sidhe”,意思是“拥有超能力的女人”.不过在鲍尔的新剧中,Banshee并不是指超自 ...

  6. 豪斯医生第一季/全集House M.D 1迅雷下载

    豪斯医生 第一季 House M.D. Season 1 (2004)本季看点:态度无礼,表情凶恶,跛足拄着一根藤棍,永远是牛仔裤运动鞋的便装打扮而不是整洁的白大褂,普林斯顿大学附属医院的格雷戈·豪斯 ...

  7. 设置Images.xcassets后启动仍旧黑屏的问题

    换了XCode6之后,发现以前写Icon-76.png这样很麻烦,就使用了Image.xcassets,公司所有测试机跑了一下,发现没什么问题.唯独有一台设置有问题,iphone5 ios6.0系统, ...

  8. easyUi 的DataGrid的绑定

    html代码: @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Index_Layout.cshtml&quo ...

  9. 深入理解多线程(四)—— Moniter的实现原理

    在深入理解多线程(一)——Synchronized的实现原理中介绍过关于Synchronize的实现原理,无论是同步方法还是同步代码块,无论是ACC_SYNCHRONIZED还是monitorente ...

  10. 第一章 AOP

    关于AOP,通常我们会使用AspectJ注解来做,共有6中切面 前置:@Before 后置:@After 返回值:@AfterReturing 异常:@AfterThrowing 环绕:@Around ...