[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 ...
随机推荐
- jquery ajax 提交表单(file && input)
用到的插件 jquery.js jquery.form.js[http://malsup.github.io/jquery.form.js] 提交页面 <form enctype="m ...
- redis 多实例配置
(redis的安装, 配置, 登陆等基础不再多说, 网上很多资料的, 这里只说个人对redis多实例的理解与配置) 我自己使用的redis版本是 2.8.13, 环境是 ubuntu 个人对多实例的理 ...
- Linux开发工具之gcc
一.gcc入门(上) 1.gcc相关概念 gcc(GNU C Compiler)编译器,最初支持C语言,现已支持C.C++.Java.Pascal.Ada.COBOL语言等:支持多种硬件平台: ...
- jQuery选择器实现隔行变色
<script type="text/javascript"> $(function(){ $("#tableName tr:nth-child(even)& ...
- LinqJoin方法
Linq知识点总结: (一).构建两个List泛型集合 List<Person> list=new List<Person>() { ...
- xls和xlsx
xls XLS 就是 Microsoft Excel 工作表,是一种非常常用的电子表格格式.xls文件可以使用Microsoft Excel打开,另外微软为那些没有安装Excel的用户开发了专门的 ...
- MySQL拷贝表的几种方式
假如我们有以下这样一个表: id username password ----------------------------------- 1 admin * ...
- makecert 制作数字证书
在MS的SDK6.0中有个证书生成工具makecert.exe, 你可以使用这个工具来生成测试用的证书. 第一步,生成一个自签名的根证书(issuer,签发者). >makecert -n &q ...
- Java学习----设计正真的应用程序
import java.util.Scanner; // 输入10位学生的成绩,并且判断他们的成绩是哪个等级,其中90-100是A级,80-89是B级,70-79是C级,60-69是D级,60分以下E ...
- 织梦dedecms网站六大SEO优化技巧(转帖)
一个排名好的网站离不开好的cms,当然不同cms各有各的好处,因此我们在上线新网站的时候,要针对不同的情况因地制宜,选择不同的网站管理系统来做seo优化,现在使用比较流行的cms是织梦dedecms, ...