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. POJ 3041 Asteroids

     最小点覆盖数==最大匹配数 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12678 Accepted:  ...

  2. Json数据

    <title>无标题文档</title>//使用 jquery 必须的先加载 <script src="jquery-2.1.1.min.js"> ...

  3. iATKOS v7硬盘安装教程(硬盘助手+变色龙安装版)

    这是作者:Tong 写的一篇安装教程 首先感谢:wowpc制作的变色龙安装版.iATKOS作者以及硬盘安装助手作者 前言:现在时代在进步,系统同样也在进步,在以前要在PC上整个Mac是很痛苦的事情,就 ...

  4. Windows2003操作系统SQL Server 2008安装图解(详细)

    最近不少用户在windows2003 server 32位操作系统上安装SQL Server2008总是失败,出现大量错误.今天经过通过我反复测试安装,找出了一个便捷的安装方法,节省大家宝贵时间,具体 ...

  5. poj3904

    题意:给出n(n<10000)个数,这些数<=10000,要求选出四个数字且他们的最大公约数为1的(注意:不需要两两互质),有多少种选法. 分析: 容斥原理 假设平面上有一些圆,互相之间有 ...

  6. 关于php中数据访问的几点补充

    前几篇文章说了,parent.self.static关键字的使用,parent可以访问父类的静态方法和静态变量,self和static可以访问本类的静态成员等等,但实际上他们还有其他作用,来看一下: ...

  7. MFC 文件按行读写 CStdioFile

    //写文件 CStdioFile file; file.Open("test.txt",CFile::modeCreate|CFile::modeReadWrite); file. ...

  8. windows 常用快捷键

    快捷键,学会就可以扔掉鼠标.      F1帮助              F2改名              F3搜索              F4地址              F5刷新     ...

  9. 一塔湖图(codevs 1024)

    题目描述 Description 小松所在的PK大学校园又称作燕园,是一个十分美丽的校园.有博雅塔,未名湖,亚洲最大的高校图书馆,人称“一塔湖图”.但是由于燕园的历史比较悠久,所以很多的老房子都要不断 ...

  10. 好玩儿的expect

    前言 1> 借鉴里面的应用思想,使用断言提高代码的健壮性及维护性 2> 实现方式——不采用直接嵌入expect的方式,统一进行重写(提取常用断言方法,重新构造API) 官网介绍 https ...