[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 ...
随机推荐
- Linux编程之定制带级别的log
我的开发组长曾经说过这么一段话"一个优秀的程序员不在于他写代码有多快,也不在于他能不能实现这个模块的功能,要实现业务实现功能谁不会啊,重要的是他的解决能力,也就说当程序出现错误时你能不能够快 ...
- poj 1780 code(欧拉路)
/* 对于n为密码想要序列最短 那么 1234 2345 这两个一定挨着 就是说 前一个的后n-1位是后一个的前n-1位 假设n==3 我们用0-99作为点的编号建图 然后每个点连出去10条边 两个相 ...
- Word 查找和替换的通配符
查找和替换的通配符 若要查找 类型 示例 任意单个字符 ? s?t 可查找“sat”和“set”. 任意字符串 * s*d 可查找“sad”和“started”. 单词的开头 < <(in ...
- cancel-ng-swipe-right-on-child
<!DOCTYPE html> <html lang="en" ng-app="myapp"> <head> <met ...
- (转)SQL Server中使用convert进行日期转换
原文链接:http://www.cnblogs.com/weiqt/articles/1826847.html SQL Server中使用convert进行日期转换 一般存入数据库中的时间格式为yyy ...
- ASP.NET生成压缩文件(rar打包)
首先引用ICSharpCode.SharpZipLib.dll,没有在这里下载:http://files.cnblogs.com/files/cang12138/ICSharpCode.SharpZi ...
- ViewHolder的作用和用法
一直都看别人用ViewHolder,自己也用过,却不知道它的作用是什么?但知道肯定很有用,而且现在android studio应该有直接生产Viewholder的插件, 不过博主我是个新手,就没尝试去 ...
- Sqlserver2005手动备份远程数据库到本地数据库方法
1,在本地数据库中新建一个数据库名,如local 选中local,鼠标右键,任务,导入数据 2下一步: 注意:服务器名称写远程连接的主机的IP, 数据库选中你要备份的远程数据库. 3下一步: 注意:服 ...
- SystemConfig.getPropertyValue("test");配置文件已经加了test=abc,但是取得时候空字符串
1.定位tomcat中System.properties是否配置了,发现配置了 2.定位myeclipse中修改的tomcat是不是自己配置的tomcat.发现是 3.定位如下位置配置是否读取我先在用 ...
- Hibernate 环境搭建
Hibernate 工作流程 1.创建工程并导包 2.在src根目录下创建配置文件:hibernate.cfg.xml(也可以创建在src其他文件夹下,但是在后面的配置中,需要指明路径) <?x ...