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

本题如果暴力的话,超时

本题用动规

设dp[i]表示s[0,i)之间存在划分使分隔后的字符串都在dict里面

dp[i]=

  true, 如果s[0,i)在dict里面

  true,如果dp[k]=true (即s[0,k)存在划分在dict里面)且s[k,i)在dict里面

  false,其他(默认)

  注意程序是前闭后开

bool wordBreak(string s, unordered_set<string> &dict){
int n = s.length();
vector<bool> dp(n+,false);
dp[]=true;
for(int i = ; i < n+; ++ i){
for(int j = ; j < i; ++ j){
if(dp[j]&&dict.find(s.substr(j,i-j))!=dict.end()){
dp[i] = true;
break;
}
}
}
return dp[n];
}

  

Leetcode Word Break的更多相关文章

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

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

  2. LeetCode:Word Break II(DP)

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

  3. LeetCode Word Break II

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

  4. [leetcode]Word Break II @ Python

    原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words  ...

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

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

  7. [LeetCode] Word Break II 解题思路

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

  8. [Leetcode] word break ii拆分词语

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

  9. LeetCode: Word Break I && II

    I title: https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, ...

  10. [LeetCode] Word Break 拆分词句

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

随机推荐

  1. Android Studio 生成Jar包时遇到的gradlew下载问题

    网上介绍说使用gradlew打包jar,可是输入gradlew makeJar后就开始download  XXX.zip,但是等了很久都没有完成.解决办法如下: 原文:http://blog.csdn ...

  2. MVC学习笔记--跟小静学MVC相关语法特性小补习

    http://www.cnblogs.com/janes/archive/2012/10/15/2721101.html http://www.cnblogs.com/h82258652/p/4795 ...

  3. SQL跨项目查询语法

    EXEC sp_addlinkedserver 'ITSV', '', 'SQLOLEDB', '192.168.1.248' EXEC sp_addlinkedsrvlogin 'ITSV', 'f ...

  4. 重温WCF之WCF传输安全(十三)(3)基于SSL的WCF对客户端验证(转)

    转载地址:http://www.cnblogs.com/lxblog/archive/2012/09/18/2690719.html 上文我们演示了,客户端对服务器端身份的验证,这一篇来简单演示一下对 ...

  5. Javascript事件冒泡机制

    1. 事件 在浏览器客户端应用平台,基本生都是以事件驱动的,即某个事件发生,然后做出相应的动作. 浏览器的事件表示的是某些事情发生的信号.事件的阐述不是本文的重点,尚未了解的朋友,可以访问W3scho ...

  6. 项目管理10000 hours – 瞎扯谈系列

    本系列会 zz 网上现有的文章,套句经典的话就是死磕自己,娱乐大众. 项目能否按时完成是项目管理的重要目标,将会面临的问题有团队的稳定性,冲突,会议以及压力. 团队中适度的人员流动是可以理解的,如何减 ...

  7. hdu 5294 最短路+最大流 ***

    处理处最短路径图,这个比较巧妙 链接:点我

  8. Spring之ResourceLoader加载资源

    Resource与ResourceLoader对比 1.Resource接口定义了应用访问底层资源的能力. 通过FileSystemResource以文件系统绝对路径的方式进行访问: 通过ClassP ...

  9. RDS MySQL 全文检索相关问题的处理

    RDS MySQL 全文检索相关问题 1. RDS MySQL 对全文检索的支持 2. RDS MySQL 全文检索相关参数 3. RDS MySQL 全文检索中文支持 3.1 MyISAM 引擎表 ...

  10. OpenGL大作业

    GLfloat light0_position[] = { 15.0,15.0,15.0,10.0 };//定义光源位置 103glLightfv(GL_LIGHT0, GL_POSITION, li ...