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. poj3335 Rotating Scoreboard

    题目描述: vjudge POJ 题解: 半平面交判核的存在性. 重点在于一个点的核也算核. 这样的话普通的求多边形的版本就要加一个特判. 就是把剩下的一个节点暴力带回所有直线重判,这时判叉积是否$\ ...

  2. Spring Security 与 OAuth2(介绍)

    https://www.jianshu.com/p/68f22f9a00ee Spring Security 与 OAuth2(介绍) 林塬 2018.01.23 11:14* 字数 3097 阅读 ...

  3. LeetCode之Weekly Contest 90

    LeetCode第90场周赛记录 第一题:亲密字符串 问题: 给定两个由小写字母构成的字符串 A 和 B ,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true :否则返回  ...

  4. angular4使用代理

    1. 在angular-cli项目根目录下创建proxy.config.json { "/api/v1": { "target": "http://1 ...

  5. Artwork Gym - 101550A 离线并查集

    题目:题目链接 思路:每个空白区域当作一个并查集,因为正着使用并查集分割的话dfs会爆栈,判断过于复杂也会导致超时,我们采用离线反向操作,先全部涂好,然后把黑格子逐步涂白,我们把每个空白区域当作一个并 ...

  6. Http协议——基本概念

    一.浏览网页的过程 用户输入一个url,浏览器根据url给web服务器发送一个Request,web服务器接收到Request后进行处理,并返回浏览器一个Response,可以响应一个静态页面或者图片 ...

  7. Linux和 Mac下git pull/push 免输入密码和账号

    linux下面可以直接创建.git-credential文件,命令如下: 创建文件,进入文件,输入内容: cd ~ touch .git-credentials vim .git-credential ...

  8. BZOJ 5390: [Lydsy1806月赛]糖果商店

    F[i][j]表示总重量为i,最上面那个盒子中糖果种类为j的方案数 每次新加一个盒子,或者在原来盒子中加入一个糖 F[i][0]为中间状态,优化转移(表示最上面那个盒子不能加糖果) #include& ...

  9. python面试题解析(数据库和缓存)

    1.     答: 关系型数据库:Mysql,Oracel,Microsoft SQL Server 非关系型数据库:MongoDB,memcache,Redis. 2.     答:     MyI ...

  10. python-高级编程-06-长连接&连接池

    我们都知道tcp是基于连接的协议,其实这个连接只是一个逻辑上面的概念,在ip层来看,tcp和udp仅仅是内容上稍有差别而已. tcp 的连接仅仅是连接两端对于四元组和sequence号的一种约定而已 ...