http://oj.leetcode.com/problems/word-break/

动态规划题目,重点是建立出模型来:

fun(start,end) = fun(start,i)*fun(i+1,end);

二维动态数组的申请:

int len = s.length();
int **flag = new int *[len];
for(int i = 0;i<len;i++)
    flag[i] = new int [len];

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std; class Solution {
public:
bool fun(string s,unordered_set<string> &dict,int start,int end,int **flag)
{
if(flag[start][end]==)
return true;
if(flag[start][end]==-)
return false;
string subString = s.substr(start,end-start+);
if(dict.find(subString)!=dict.end())
return true;
for(int i = start;i<end;i++)
if(fun(s,dict,start,i,flag)*fun(s,dict,i+,end,flag))
{
flag[start][end] = ;
return ;
}
flag[start][end] = -;
return false;
}
bool wordBreak(string s, unordered_set<string> &dict) {
int len = s.length();
int **flag = new int *[len];
for(int i = ;i<len;i++)
flag[i] = new int [len];
return fun(s,dict,,len-,flag);
}
}; int main()
{
class Solution mys;
string s = "leetcode";
unordered_set<string> dict;
dict.insert("le");
//dict.insert("et");
dict.insert("code");
cout<<mys.wordBreak(s,dict);
return ;
}

LeetCode OJ——Word Break的更多相关文章

  1. [LeetCode] 139. Word Break 单词拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  2. [LeetCode] 140. Word Break II 单词拆分II

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

  3. 【leetcode】Word Break (middle)

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

  4. [Leetcode Week9]Word Break II

    Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...

  5. [Leetcode Week9]Word Break

    Word Break 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-break/description/ Description Given ...

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

  7. Leetcode#139 Word Break

    原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...

  8. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  9. LeetCode OJ——Word Ladder2

    http://oj.leetcode.com/problems/word-ladder-ii/ class Solution { public: vector<vector<string& ...

随机推荐

  1. pandas学习series和dataframe基础

    PANDAS 的使用 一.什么是pandas? 1.python Data Analysis Library 或pandas 是基于numpy的一种工具,该工具是为了解决数据分析人物而创建的. 2.p ...

  2. 素材网站——mokuge

  3. 图解一致性协议2PC和3PC

    原图地址:https://www.processon.com/diagraming/5b89f6ace4b0d4d65bf10786

  4. Python虚拟机函数机制之闭包和装饰器(七)

    函数中局部变量的访问 在完成了对函数参数的剖析后,我们再来看看,在Python中,函数的局部变量时如何实现的.前面提到过,函数参数也是一种局部变量.所以,其实局部变量的实现机制与函数参数的实现机制是完 ...

  5. wordpress 获取站点的所有链接

    <?php include "wp-load.php"; $posts = new WP_Query('post_type=any&posts_per_page=-1 ...

  6. [git 学习篇]工作区和暂存区

    1 工作区,就是目录/User/my./learngit 2 版本库 工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库. liuzhipeng@exdroid43:~/pad/pad- ...

  7. 九度oj 1006

    题目1006:ZOJ问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:20252 解决:3544 题目描述:                        对给定的字符串(只包含'z', ...

  8. 【bzoj4080】[Wf2014]Sensor Network 随机化

    题目描述 魔法炮来到了帝都,除了吃特色菜之外,还准备去尝一尝著名的北京烤鸭.帝都一共有n(1<=1<=100)个烤鸭店,可以看成是二维平面内的点.不过由于魔法炮在吃烤鸭之前没有带钱,所以吃 ...

  9. localStorage的用法

    1.在HTML5中,本地存储是一个window的属性,包括localStorage和sessionStorage,前者是一直存在本地的,后者是伴随着session,窗口一旦关闭就消失了.二者用法完全相 ...

  10. NOJ——1568走走走走走啊走(超级入门DP)

    [1568] 走走走走走啊走 时间限制: 1000 ms 内存限制: 65535 K 问题描述 菜菜赚了钱回来,想起要买很多桶回来,不同地方的桶质量是不同的,他在(1,1)点出发因为飞机票有点贵所以他 ...