【Word Break】cpp
题目:
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的更多相关文章
- 【Word Search】cpp
题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...
- 【word ladder】cpp
题目: Given two words (beginWord and endWord), and a dictionary, find the length of shortest transform ...
- 【Word Break II】cpp
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- 【word xml】将word转化为xml格式后,如何在xml中卫word添加分页符
1.首先在xml中找到我们需要添加分页符的位置 例如:我需要在这个第一部分上面添加一个分页符 2.找到这个[第一部分]这个位置之后,开始往上找,找到对应的位置 3.在</w:pPr>下方添 ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- 【Implement strStr() 】cpp
题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...
- 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~ ...
- POJ 1018 【枚举+剪枝】.cpp
题意: 给出n个工厂的产品参数带宽b和价格p,在这n个工厂里分别选1件产品共n件,使B/P最小,其中B表示n件产品中最小的b值,P表示n件产品p值的和. 输入 iCase n 表示iCase个样例n个 ...
- 【Text Justification】cpp
题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...
随机推荐
- 如何查看win10已激活密钥?查看win10已激活完整密钥的方法!
如何查看win10已激活密钥?查看win10已激活完整密钥的方法! HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVersion/So ...
- C++ 强制类型转换(转载)
转载自:http://www.weixueyuan.net/view/6329.html 在C++语言中新增了四个关键字static_cast.const_cast.reinterpret_cast和 ...
- RxJava 1升级到RxJava 2过程中踩过的一些“坑”
RxJava2介绍 RxJava2 发布已经有一段时间了,是对 RxJava 的一次重大的升级,由于我的一个库cv4j使用了 RxJava2 来尝鲜,但是 RxJava2 跟 RxJava1 是不能同 ...
- ArcGIS中Features与JSON的互相转化
实际操作过程非常简单,这里就简单记录下转换工具的位置:
- iOS 力学动画生成器UIKit Dynamics 之碰撞效果讲解
UIKit Dynamic是iOS7 新增的一组类和方法,可赋予UIView逼真的行为和特征,不需要写动画效果那些繁琐的代码,让开发人员能够轻松地改善应用的用户体验.一共有6个可用于定制UIDynam ...
- 优化Linux的内核参数来提高服务器并发处理能力
提高Linux系统下的负载能力,可以使用nginx等原生并发处理能力就很强的web服务器 使用Apache的可以启用其Worker模式,来提高其并发处理能力 修改Linux的内核相关TCP参数,来最大 ...
- 【BZOJ1059】[ZJOI2007] 矩阵游戏(匈牙利算法)
点此看题面 大致题意: 有一个\(N*N\)的\(01\)矩阵,可以任意交换若干行和若干列,问是否有方案使得左上角到右下角的连线上全是\(1\). 题意转换 首先,让我们来对题意进行一波转化. 如果我 ...
- Videos
Videos 时间限制: 1 Sec 内存限制: 128 MB提交: 17 解决: 7[提交] [状态] [讨论版] [命题人:admin] 题目描述 C-bacteria takes charg ...
- Dropout & Maxout
[ML] My Journal from Neural Network to Deep Learning: A Brief Introduction to Deep Learning. Part. E ...
- python 线程even
import threading,time import random def door(nums): num=1#电梯在一楼 while True: print("this door is ...