[LintCode] Word Break
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.
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的更多相关文章
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- 【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 ...
- 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 ...
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- Leetcode#139 Word Break
原题地址 与Word Break II(参见这篇文章)相比,只需要判断是否可行,不需要构造解,简单一些. 依然是动态规划. 代码: bool wordBreak(string s, unordered ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 140. Word Break II
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
随机推荐
- Button控件常用api
加载按钮的纹理.loadTextures (const std::string &normal, const std::string &selected, const std::str ...
- string,const char*,char*之间的相互转换
1. string转const char* string s = "abc"; const char* c_s = s.c_str(); 2. const char*转string ...
- [Effective JavaScript 笔记] 第2条:理解JavaScript的浮点数
JavaScript数值型类型只有数字 js只有一种数值型数据类型,不管是整数还是浮点数,js都把归为数字. typeof 17; // “number” typeof 98.6; // “num ...
- [Effective JavaScript 笔记]第20条:使用call方法自定义接收者来调用方法
不好的实践 函数或方法的接收者(即绑定到特殊关键字this的值)是由调用者的语法决定的.方法调用语法将方法被查找的对象绑定到this变量,(可参阅之前文章<理解函数调用.方法调用及构造函数调用之 ...
- 第9章 使用ssh服务管理远程主机。
章节简述: 学习使用nmtui命令配置网卡参数.手工将多块网卡做绑定.使用nmcli命令查看网卡信息和使用ss命令查看网络及端口状态. 完整演示sshd服务配置方法并详细讲述每个参数的作用,实战基于密 ...
- hiho #1288 微软2016.4校招笔试题 Font Size
#1288 : Font Size 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Steven loves reading book on his phone. The ...
- CentOS6 下安装HP-LaserJet 1020打印机
因为实验室有个多余的老服务器,所以近段时间想把老服务器做成打印机服务器,同时因为最近在学习linux,所以就像在CentOS6.3 上安装打印机驱动.因为是新手,所以走了不少弯路,今天终于把打印机安装 ...
- overflow-x和overflow-y其中一个设置为visible时的奇怪现象
当overflow-x和overflow-y其中一个设置为visible时,如果另一个不是visible,那么它会被自动重置为auto 看看效果先: 第一次遇到这个问题时,我还以为是chrome的一个 ...
- 【云计算】ubuntu下docker安装配置指南
Docker Engine安装配置 以下描述仅Docker在Ubuntu Precise 12.04 (LTS).Ubuntu Trusty 14.04 (LTS).Ubuntu Wily 15.10 ...
- ZOJ 2315
---恢复内容开始--- http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1315 这个题目比较难以看懂,我也是看网上的题目意思才 ...