[LeetCode] Word Break II 解题思路
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
Return all such possible sentences.
For example, given
s = "catsanddog"
,
dict = ["cat", "cats", "and", "sand", "dog"]
.
A solution is ["cats and dog", "cat sand dog"]
.
这道题是 Word Break的一个拓展
问题:根据给的单词字典,求一个字符串所有可能的有效分割。
有了 Word Break 的经验,这道题做起来也顺畅了许多。
假设将字符串 s 分割为两段,[0,i-1], [i, n-1],如果[0, i-1] 为有效单词,[i, n-1]为有效单词集合,那么 s 就是一个有效字符串。将 [0, i-1] 依次和 [i, n-1] 所有可能分别匹配,则得到 s 以 i 为分割点得全部有效分割。
将 i 从 1 到 n-1 遍历一次,则求得 s 的全部分割的可能。
和 Word Break 一样,需要记录已计算的结果,提高效率。利用 map<int, vector<string>> idx_words[k] 来记录 [k, n-1] 子串的全部有效分割。
map<int, vector<string>> idx_words; vector<string> match(string s, unordered_set<string>& wordDict, int startIdx){
vector<string> res; for (int i = ; i < s.size(); i++) {
string leftS = s.substr(, i); unordered_set<string>::iterator us_iter = wordDict.find(leftS); if (us_iter != wordDict.end()) { int rightRLIdx = i + startIdx; vector<string> rightV;
if (idx_words.find(rightRLIdx) != idx_words.end()){
rightV = idx_words[rightRLIdx];
}else{ string rightS = s.substr(i, s.size() - i); rightV = match(rightS, wordDict, rightRLIdx);
idx_words[rightRLIdx] = rightV;
} for (int ii = ; ii < rightV.size(); ii++) {
string tmpS = leftS + " " + rightV[ii]; res.push_back(tmpS);
}
}
} if (wordDict.find(s) != wordDict.end()) {
res.push_back(s);
} return res;
} vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
vector<string> res; res = match(s, wordDict, ); return res;
}
[LeetCode] Word Break II 解题思路的更多相关文章
- 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 ...
- 【LeetCode】140. Word Break II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...
- [leetcode]Word Break II @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words ...
- [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(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] 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 Ladder II 解题报告
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...
- LeetCode: Word Break II [140]
[题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where ...
随机推荐
- gridview添加header
gridview是不能添加header的,这里的解决方法是将listview改造成gridview使用,功能很好用,唯一的缺点是列数不能自适应 示例代码下载地址http://pan.baidu.com ...
- jquer “$” 扩展(笔记)
/** * Created by shanruo on 16-03-10. */ (function ( $ ){ $.extend ({ /* * 根据参数名获取参数 @name String 参数 ...
- jsp页面可以巧用模态框
jsp页面使用模态框配合ajax出来的效果真的没话说,当然你也可以使用模态框配合action,但是在删除和更新的时候传值有点麻烦,用ajax 就没有这些问题 ,比如删除代码的时候在js文件中传值可以这 ...
- C# - 二叉树表达式计算
很早以前就写过双栈的表达式计算. 这次因为想深入学一下二叉树,网上都是些老掉牙的关于二叉树的基本操作. 感觉如果就学那些概念,没意思也不好记忆.于是动手写了一个表达式计算的应用例子. 这样学习印象才深 ...
- 安卓 报错 Check the Eclipse log for stack trace.
移除Android Dependencies就行了. 在properties的Libraries里
- 基于GBT28181:SIP协议组件开发-----------第四篇SIP注册流程eXosip2实现(一)
原创文章,引用请保证原文完整性,尊重作者劳动,原文地址http://www.cnblogs.com/qq1269122125/p/3945294.html. 上章节讲解了利用自主开发的组件SIP组件l ...
- css3之box-sizing
css盒子模型中包括几种重要的属性,包括margin.border.padding以及content.但浏览器对其盒子模型的解释则有所不痛,启用标准模式还是IE(怪)模式是与当前页面的文档声明相关的. ...
- 文件操作1-php
is_file (PHP 3, PHP 4, PHP 5) is_file -- 判断给定文件名是否为一个正常的文件 说明 bool is_file ( string filename ) 如果文件存 ...
- jQuery 使用 jQuery UI 部件工厂编写带状态的插件(翻译)
首先,我们要创建一个progress bar,它只允许我们简单的设置进度值.正如我们接下来将要看到的,我们需要通过调用 jQuery.widget 及其两个参数来实现这一操作,这两个参数分别是:将要创 ...
- js 求两个日期之间相差天数
//求两个日期之间的相差天数 function daysBetween(DateOne, DateTwo) { var OneMonth = DateOne.substring(5, DateOne. ...