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. 对O(logN)复杂度的推导

    之前一直对O(logN)这个复杂度如何推导出的存在疑问,这段时间看了一些算法相关的内容,正好看到这个问题,大略研究了一下算是基本解答了我的疑惑:现记录如下 假设有一棵高为H的满二叉树,则它的节点共有N ...

  2. matlplotlib根据函数画出图形

    根据函数画出函数的轨迹 import matht = np.linspace(0, math.pi, 1000)x = np.sin(t)y = np.cos(t) + np.power(x, 2.0 ...

  3. nginx日志相关优化安全

    一.编写脚本实现nginx access日志轮询 配置日志切割脚本,如下: [root@nginx shell]# cat cut_nginx_log.sh #!/bin/bash #Author:M ...

  4. CentOS 系统下Gitlab搭建与基本配置 以及代码备份迁移过程

    GitLab 是一个开源的版本管理系统,提供了类似于 GitHub 的源代码浏览,管理缺陷和注释等功能,你可以将代码免费托管到 GitLab.com,而且不限项目数量和成员数.最吸引人的一点是,可以在 ...

  5. R-codes-tips

    1. 在shell执行R文件 chmod 0755 file.R Rscript file.R 2.  载入数据 data(dune) 3. attach() 将data.frame添加到R的搜索路径 ...

  6. LeetCode(123) Best Time to Buy and Sell Stock III

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  7. eclipse使用技巧的网站收集——转载(二)

    写代码离不开文本编辑器,看代码也离不开,iar和keil编辑和阅读简直一般般了,因此使用eclipse可以看看代码,提高效率.网上有几个博客的文章,这里收集一下,以备忘. 以下文章转载自:http:/ ...

  8. LINQ to SQL和Entity Framework对比与关联

    LINQ to SQL和Entity Framework都是一种包含LINQ功能的对象关系映射技术.他们之间的本质区别在于EF对数据库架构和我们查询的类型实行了更好的解耦.使用EF,我们查询的对象不再 ...

  9. 关于windows服务的编写/安装/与调试

    前注: 首先,这篇文章是从网上转过来的,因为最近有个项目,需要编写一个Windows Service来定时执行程序,网上很容易找到了这篇文章,大概看了一下,文章讲的还是很详细的.不过这篇文章应该是.n ...

  10. php isset()与empty()详解

    bool isset(mixed var);[;mixed var[,...]] 这个函数需要一个变量名称作为参数,如果这个变量存在,则返回true,否则返回false. 也可以传递一个由逗号间隔的变 ...