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

题意:S是否可以由dict中的字符串合成。

思路:动态规划。维护一个数组vector<bool> dp(s.size()+1,false),其中dp[i] 代表S中[0,i-1]是否用dict中的字符串表示,能,true,不能,false。对dp[i]而言,若dp[j] (j<i)能在dict中找到,则只需看s.substr(j,i-j)是否能在dict中找到,若能,则i++,重新分析,不能j++。这里值得注意的是:下标的问题。dp[j]表示S中s.substr(0,j)(前闭后开,所以代表S中[0, j-1] 子字符串 )能否在dict中找到。代码如下:

 class Solution {
public:
bool wordBreak(string s, unordered_set<string> &dict)
{
int len=s.size();
vector<bool> dp(len+,false);
dp[]=true; / for(int i=;i<len+;++i)
{
for(int j=;j<i;++j)
{
if(dp[j]&&dict.find(s.substr(j,i-j)) !=dict.end())
{
dp[i]=true;
break;
}
}
}
return res[len];
}
};

网友Code Gander总结了动态规划的一般套路。

个人总结:

动态规划:基于一个递推公式以及一个或者多个初始状态。较为重要是:状态和状态转移方程!

三步曲:

一、存储什么历史信息以及用什么结构;
二、递推方程(重要);

三、起始条件;

最重要的还是知道,什么情况下用动态规划。

[Leetcode] word break 拆分词语的更多相关文章

  1. [LeetCode] Word Break 拆分词句

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

  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拆分词语

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

  4. LeetCode:Word Break II(DP)

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

  5. LeetCode Word Break II

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

  6. [leetcode]Word Break II @ Python

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

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

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

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

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

随机推荐

  1. CacheManager源码分析

    计算rdd的某个分区是从RDD的iterator()方法开始的,我们从这个方法进入 然后我们进入getOrCompute()方法中看看是如何进行读取数据或计算的 getOrElseUpdate()方方 ...

  2. unity独立游戏开发日志2018/09/22

    f::很头痛之前rm做的游戏在新电脑工程打不开了...只能另起炉灶... 还不知道新游戏叫什么名...暂且叫方块世界.(素材已经授权) 首先是规划下场景和素材文件夹的建立. unity常用的文件夹有: ...

  3. EXKMP学习笔记QAQ

    因为一本通少了一些算法,所以我就自行补充了一些东西上去. EXKMP也就是扩展KMP,是一种特别毒瘤的东西 EXKMP确实很难,我理解他的时间与AC机的时间差不多,而且还很难记,因此一学会就马上写博客 ...

  4. FIFO队列(First In First Out)和优先队列

    queue<类型名> q; q.size() - 返回队列中元素个数 q.empty() - 若队列为空,返回true ,否则返回false q.pop() - 删除队首元素,但不返回其值 ...

  5. python, pycharm, virtualenv 的使用

    创建虚拟环境,一次安装多个库 pip freeze > requirements.txt (库的名字都在里面) 产生requirements.txt文件 在另一个环境下使用 pip instal ...

  6. js学习日记-new Object和Object.create到底干了啥

    function Car () { this.color = "red"; } Car.prototype.sayHi=function(){ console.log('你好') ...

  7. springmvc基础篇—通过注解的方式去配置项目

    学习了通过xml方式去配置项目后,当然要掌握更简单更灵活的注解方式哟,这是官方推荐使用的方式. 一.修改配置文件,建议大家直接使用我的配置文件 <?xml version="1.0&q ...

  8. PHP Warning: File upload error - unable to create a temporary file in Unknown on line 0

    代码在本地运行一切都OK,放到服务器上,网站访问正常,上传就出现该错误. 提示:PHP Warning: File upload error - unable to create a temporar ...

  9. HTTP 知新

    REST 先从 REST 的角度来看看 HTTP 协议规范, URL:需要操作的对象,也就是资源 HTTP method:我要对该对象做什么(POST 增.DELETE 删.GET 查.PUT 和 P ...

  10. 平面最近点对(HDU 1007)

    题解:点击 #include <stdio.h> #include <string.h> #include <algorithm> #include <ios ...