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. SQL命令(二)

    (1)数据库查询 格式: SELECT <列名1,2,3...> FROM <表名> [WHERE子句] [GROUP BY 子句] [HAVING 子句] [ORDER BY ...

  2. ecshop 漏洞如何修复 补丁升级与安全修复详情

    目前ecshop漏洞大面积爆发,包括最新版的ecshop 3.0,ecshop 4.0,ecshop2.7.3全系列版本都存在着高危网站漏洞,导致网站被黑,被篡改,被挂马,许多商城系统深受其漏洞的攻击 ...

  3. 自己动手编写 Dockerfile 构建自定义的Jenkins

    1.构建jenkins 镜像 vim Dockerfile FROM jenkins  USER root ARG dockerGid=999  RUN echo "docker:x:${d ...

  4. 自定义vim配置文件vimrc,用于c/c++编程

    vim作为Linux下广受赞誉的代码编辑器,其独特的纯命令行操作模式可以很大程度上方便编程工作,通过自定义vim配置文件可以实现对vim功能的个性化设置. vim配置文件一般有两份,属于root的/e ...

  5. DHT11温湿度传感器编程思路以及代码的实现(转载)

    源自:https://blog.csdn.net/qq_34952376/article/details/81193938 在我们刚开始进入单片机的学习中,练习写传感器的时序是必不可少的,其实我比较推 ...

  6. 001---C/S架构

    C/S 架构介绍 什么是C/S架构 C:client,客户端 S:server,服务端 实现客户端和服务端之间的网络通信 什么是网络 人与人之间交流是通过语言,才能彼此理解对方的意思.但是地球上有多个 ...

  7. 算法-----数组------ 数组中的第K个最大元素

    在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 ...

  8. centos linux 因别名问题引起的麻烦及解决技巧

    老男孩儿-19期 L005-13节中分享.自己整理后发到自己微博中留档. 原文:http://oldboy.blog.51cto.com/2561410/699046 实例:老男孩linux实战培训第 ...

  9. 玩转Vim-札记(二)

    玩转Vim-札记(二) 距上篇博文已有一周有余,上次主要介绍了编辑器之神Vim的起源.安装并介绍了两种模式以及一些简单的操作.本次将继续对Vim的使用进行介绍. 登堂入室 首先接着说移动吧: 0 → ...

  10. 《百词斩·象形9000》第一册(上) 符号Symbol 1

    001-upon prep. 在......上面 Wish upon a star.#对着星星许愿. 002-think V. 想,思索,认为:以为,预料 What do you think?#你认为 ...