题目:

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

代码:

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

tips:

用了一个二维dp的思路。

dp[i][j]表示s[i]到s[j]是否能被dict表示。

dp的顺序是从后往前走,相当于是从最下往上dp上三角阵

求递推项dp[i][j]用一个循环遍历:dp[i][k] dp[k+1][j] ( i <= k <= j )如果能找到一个使得dp[i][j]为true,则跳出。

这里有一个corner case就是k==j的时候,dp[j+1][j]没有意义;因此特殊处理一下这种case,直接查找dict中是否有s[i:j]。

完成之后,在思考这道题是否可以用滚动数组来做?这样空间复杂度就降低为了O(n)

================================================

这个并不是滚动数组,就是一个一维DP,如下。

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

tips:

1. 空间复杂度由O(n²)降低到了O(n)

2. 代码更简洁了

并不是滚动数组,就是一维的DP,只不过dp[i]的取值要由dp[0:i-1]所有历史过程来决定。

===============================================

第二次过这道题,一开始没思路,后来强迫自己想出来了一维DP的解法,最后写了出来。

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

【Word Break】cpp的更多相关文章

  1. 【Word Search】cpp

    题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...

  2. 【word ladder】cpp

    题目: Given two words (beginWord and endWord), and a dictionary, find the length of shortest transform ...

  3. 【Word Break II】cpp

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

  4. 【word xml】将word转化为xml格式后,如何在xml中卫word添加分页符

    1.首先在xml中找到我们需要添加分页符的位置 例如:我需要在这个第一部分上面添加一个分页符 2.找到这个[第一部分]这个位置之后,开始往上找,找到对应的位置 3.在</w:pPr>下方添 ...

  5. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  6. 【Implement strStr() 】cpp

    题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...

  7. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  8. POJ 1018 【枚举+剪枝】.cpp

    题意: 给出n个工厂的产品参数带宽b和价格p,在这n个工厂里分别选1件产品共n件,使B/P最小,其中B表示n件产品中最小的b值,P表示n件产品p值的和. 输入 iCase n 表示iCase个样例n个 ...

  9. 【Text Justification】cpp

    题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...

随机推荐

  1. Centos6.5安装部署nodejs

    使用编译好的包安装 一.在官网下载包 https://nodejs.org/en/download/ 二.把包传送到服务器,进入到包目录并解压 tar axvf node-v6.9.5-linux-x ...

  2. 将数据库数据显示到TreeView控件中

    实现效果: 知识运用: TreeView控件中的Nodes集合的Add方法 实现代码: private void init() { treeView1.ShowLines = true; treeVi ...

  3. oo作业第四单元总结暨结课总结

    目录 一.第四单元作业架构设计 1.第一次UML作业架构设计 2.第二次UML作业架构设计 二.架构设计和OO方法理解演进 三.测试理解与实践的演进 四.课程收获总结 五.三个具体改进建议 一.第四单 ...

  4. CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第八节

    原文链接 第八节:利用CUDA函数库 Rob Farber 是西北太平洋国家实验室(Pacific Northwest National Laboratory)的高级科研人员.他在多个国家级的实验室进 ...

  5. for循环和数组练习

    //公鸡2文,母鸡1文,小鸡半文,每种至少一只,100文买100只鸡有多少种可能 var ci =0; for(var g=1;g<50;g++){ for(var m=1;m<100;m ...

  6. vue-awesome-swiper实现轮播图

    1.首先通过npm安装vue-awesome-swiper,我在项目中用的是2.6.7版本 npm install vue-awesome-swiper@2.6.7 –save 2. 在main.js ...

  7. 在mac下使用python抓取数据

    2015已经过去,这是2016的第一篇博文! 祝大家新年快乐! 但是我还有好多期末考试! 还没开始复习,唉,一把辛酸泪! 最近看了一遍彦祖的文章叫做 iOS程序员如何使用Python写网路爬虫 所以自 ...

  8. 微信小程序的开发——01小程序的执行流程是怎样的?

    作者:叶小钗 转载至:https://www.cnblogs.com/yexiaochai/p/9346043.html 我们这边最近一直在做基础服务,这一切都是为了完善技术体系,这里对于前端来说便是 ...

  9. C/C++程序基础 (八)数据结构

    非递归先序遍历 // 输出, 遍历左子树,遍历右子树 void firstOrder(Node* root) { stack<Node*> leftNodes; Node* curr = ...

  10. nginx下配置Yii2 rewrite、pathinfo等

    环境说明: 我试用的lnmp安装包安装的nginx,nginx版本是1.14.1 server { listen ; server_name www.baidu.com; #access_log /d ...