Given a string s and a dictionary of words dict, determine if s can be break into a space-separated sequence of one or more dictionary words.

Example

Given s = "lintcode", dict = ["lint", "code"].

Return true because "lintcode" can be break as "lint code".

Solution:

DP.

bool wordBreak(string s, unordered_set<string> &dict) {
int len = s.size();
if(len == ) return true; int min_len = INT_MAX, max_len = INT_MIN;
for (auto &ss : dict) {
min_len = min(min_len, (int)ss.length());
max_len = max(max_len, (int)ss.length());
} vector<int> breakIdx;
breakIdx.push_back(-); for(int i = ;i < len;++i){
for(int j = breakIdx.size() - ;j >= ;--j){
int idx = breakIdx[j];
if(i - idx < min_len || i - idx > max_len)
continue;
string str = s.substr(idx + , i - idx);
if(dict.find(str) != dict.end()){
breakIdx.push_back(i);
break;
}
}
}
return (*breakIdx.rbegin()) == (len - );
}

[LintCode] Word Break的更多相关文章

  1. [LeetCode] Word Break II 拆分词句之二

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

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

  3. 17. Word Break && Word Break II

    Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...

  4. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  5. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  6. 【LeetCode OJ】Word Break II

    Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...

  7. Leetcode#139 Word Break

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

  8. 【leetcode】Word Break (middle)

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

  9. 140. Word Break II

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

随机推荐

  1. MSSQL复习

    1.用户角色: 登录名就相当于一个用户 角色相当于把你的操作权限分组了 2.数据系统结构(略) 网络连接接口 关系引擎 存储引擎 内存 3.数据库的结构 数据库 架构 对象(在Sql server中将 ...

  2. Codeforces Round #FF (Div. 2) C. DZY Loves Sequences

    解题报告:输入一个数列,选取一个子数列,要求最多只能改动这个子数列中的一个数,使得这个子数列是严格的升序的(严格升序没有相等的) 我的做法是,第一步把这个 数列的每个升序的子数列都找出来,然后看这些子 ...

  3. MySQL数据库服务器的架设

    导读 MySQL数据库是Linux操作系统上用得最多的数据库系统,它可以非常方便的与其它服务器集成在一起,如Apache.Vsftpd.Postfix等.下面介绍RHEL 6平台MySQL数据库服务器 ...

  4. 腾讯新浪通过IP地址获取当前地理位置(省份)的接口

    腾讯新浪通过IP地址获取当前地理位置(省份)的接口  腾讯的接口是 ,返回数组 http://fw.qq.com/ipaddress 返回值 var IPData = new Array(" ...

  5. chrome控制台支持多行js模式

    shift + 回车 是换行 转自: http://zhidao.baidu.com/link?url=MYjGRwvVQYJwnr38VTHPJdzRNtF1COyqpeuAtBYbxFYJcu6p ...

  6. 【云计算】Docker集中化web界面管理平台shipyard

    Docker集中化web界面管理平台shipyard docker shipyard seanlook                        2015年01月05日发布             ...

  7. 【云计算】Netflix 开源持续交付平台 Spinnaker

    oschina        发布于: 2015年11月19日 (0评)          分享到:    收藏 +1 CDS首都在线全球云主机.全球私有网络,开工送礼,免费试用! »   日前,Ne ...

  8. KDD-CUP Proposal

    From 鞠源 已有 1303 次阅读 2012-11-25 21:09 |系统分类:科研笔记|关键词:会议 领域 justify 知识 KDDCUP - Competition is a stron ...

  9. 56. 2种方法判断二叉树是不是平衡二叉树[is balanced tree]

    [本文链接] http://www.cnblogs.com/hellogiser/p/is-balanced-tree.html [题目] 输入一棵二叉树的根结点,判断该树是不是平衡二叉树.如果某二叉 ...

  10. Android Studio "diamond operator is not supported" 处理方法

    低版本的android编译环境是不支持使用java7语法的,如果使用了,就会产生上述问题,如果你的android环境较新,那么可以使用以下方法: 在build.gradle的android标签下加入以 ...