[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 ... 
随机推荐
- 错误	X	“X1”不包含“XX2”的定义,并且找不到可接受类型为“X1”的第一个参数的扩展方法“XX2”(是否缺少 using 指令或程序集引用?)
			由于我是复制其他.cs文件的代码··· 出错了·搜了一下解决方法··· 但是不适用···· 个人出错原因: 忘了在.cs文件的刚开始(即:using xx:后) namespace aaa.bb { ... 
- PHP常用正则表达式
			正则表达式用于字符串处理.表单验证等场合,实用高效.现将一些常用的表达式收集于此,以备不时之需. 匹配中文字符的正则表达式: [\u4e00-\u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表 ... 
- java笔记--使用SwingWoker类完成耗时操作
			使用SwingWoker类完成耗时操作: 对于Swing中的耗时操作,通常要在一个新的线程中运行,以免程序"假死". 在java6.0中,可以用SwingWoker类来完成 Swi ... 
- 教你安装漂亮的Arc GTK主题
			导读 近日,我们又发现了一款深受 Linux 用户喜爱的桌面主题 — Arc GTK,Arc GTK 主题已被很多 GNU/Linux 操作系统支持和采用,其中就包括即将到来的 Linux Mint ... 
- 91SDK接入及游戏发布、更新指南
			原地址:http://bbs.18183.com/thread-99382-1-1.html本帖最后由 啊,将进酒 于 2014-4-17 10:23 编辑 1.联系91的商务人员建讨论组或者厂商建Q ... 
- Unity3D研究院之自制批量关联材质与贴图插件
			原地址:http://www.xuanyusong.com/archives/2314 美术做过的模型导出fbx,美术把Fbx和贴图文件给了程序,程序把Fbx导入工程可能会出现贴图和材质没有关联上的问 ... 
- UIDatePicker的简单用法
			// 初始化UIDatePicker UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(, , , ) ... 
- linux安装setup工具
			如果你的Linux系统是最小化安装的,可能会没有setup命令工具,环境是centos 5.8 安装setup命令工具的步骤. 安装setuptool #yum install setuptool 系 ... 
- JavaScript 在页面上显示数字时钟
			显示一个钟表 拓展JavaScript计时:http://www.w3school.com.cn/js/js_timing.asp setTimeout() 方法会返回某个值.在下面的语句中,值被储存 ... 
- codeforces A. Flipping Game 解题报告
			题目链接:http://codeforces.com/problemset/problem/327/A 题意是输入一个只有0和1的序列,要求找出一个合理的区间,在这个区间里面把0变成1,1变成0,使得 ... 
