[LeetCode] Word Break II 拆分词句之二
[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 拆分词句的拓展,那道题只让我们判断给定的字符串能否被拆分成字典中的词,而这道题加大了难度,让我们求出所有可以拆分成的情况,就像题目中给的例子所示。可以用DFS的套路来解题,但是不是一般的brute force,我们必须进行剪枝优化,因为按照惯例OJ的最后一个test case都是巨长无比的,很容易就Time Limit Exceeded。那么如何进行剪枝优化呢,可以参见网友水中的鱼的博客,定义一个一位数组possible,其中possible[i] = true表示在[i, n]区间上有解,n为s的长度,如果某个区间之前被判定了无解,下次循环时就会跳过这个区间,从而大大减少了运行时间,参见代码如下:
class Solution {
public:vector<string> wordBreak(string s, unordered_set<string>& wordDict) {vector<string> res;string out;vector<bool> possible(s.size() + 1, true);wordBreakDFS(s, wordDict, 0, possible, out, res);return res;}void wordBreakDFS(string &s, unordered_set<string> &wordDict, int start, vector<bool> &possible, string &out, vector<string> &res) {if (start == s.size()) {res.push_back(out.substr(0, out.size() - 1));return;}for (int i = start; i < s.size(); ++i) {string word = s.substr(start, i - start + 1);if (wordDict.find(word) != wordDict.end() && possible[i + 1]) {out.append(word).append(" ");int oldSize = res.size();wordBreakDFS(s, wordDict, i + 1, possible, out, res);if (res.size() == oldSize) possible[i + 1] = false;out.resize(out.size() - word.size() - 1);}}}
};
[LeetCode] Word Break II 拆分词句之二相关推荐
- 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 [140]
[题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where ...
- Leetcode word break I II 词句拆分I和II的java实现及解析
Leetcode word break I & II 词句拆分I和II的java实现及解析 word break i是leetcode 里面中等难度的题目而word break II 更是ha ...
- 【DFS + 记忆化递归】LeetCode 140. Word Break II
LeetCode 140. Word Break II Solution1:我的答案 纯DFS,在第31个case时超时,还是记录一下.. class Solution { // DFS public ...
- Word Break Word Break II
leetcode上面的两道动态规划题. 题目1 Word Break Given a string s and a dictionary of words dict, determine if s c ...
- LeetCode——Word Break
LeetCode--Word Break Question Given a non-empty string s and a dictionary wordDict containing a list ...
- leetcode 140. Word Break II | 140. 单词拆分 II(动态规划)
题目 https://leetcode.com/problems/word-break-ii/ 题解 由 leetcode 139. Word Break | 139. 单词拆分(动态规划) 改造而来 ...
- leetcode word break java,Word Break leetcode java
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
- 139. Word Break 单词拆分
Title 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可以假设字 ...
最新文章
- springboot输出hello world_Spring Boot 入门教程 | 图文讲解
- hdu2561 第二小整数(排序)
- 事务的四个特性-ACID
- linux中多进程调试,linux下用gdb调试多进程
- 利用memcached实现CAS单点登录集群部署
- 细分将成为2011手机市场的主旋律
- 霸榜 | 微软CV模型收获近 2k star
- android int 首位值_Android中数值的精确计算
- JAVA redis 常用函数
- F5-yumnfsftp
- TCP/IP之封装,分用,server模型
- SQL的bit列名转换成access是/否数据类型,True无效的问题
- 批量给excel表中的每行添加表头
- 在 Java 应用程序中访问USB设备
- 解决Chrome、360自动填充用户名和密码行为带来的困扰
- Java 中文转拼音/汉字转拼音, 中文转五笔/汉字转五笔, 下载字典!
- 华东师范大学计算机坏老师,华东师范大学:2020各专业录取分数线!毕业当老师的都是铁饭碗...
- php的两种安装方式,PHP的安装方法
- 灯泡(信息学奥赛一本通 1438)
- 使用python对url编码解码 (转)