[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 拆分词句之二相关推荐

  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 [140]

    [题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where ...

  3. Leetcode word break I II 词句拆分I和II的java实现及解析

    Leetcode word break I & II 词句拆分I和II的java实现及解析 word break i是leetcode 里面中等难度的题目而word break II 更是ha ...

  4. 【DFS + 记忆化递归】LeetCode 140. Word Break II

    LeetCode 140. Word Break II Solution1:我的答案 纯DFS,在第31个case时超时,还是记录一下.. class Solution { // DFS public ...

  5. Word Break Word Break II

    leetcode上面的两道动态规划题. 题目1 Word Break Given a string s and a dictionary of words dict, determine if s c ...

  6. LeetCode——Word Break

    LeetCode--Word Break Question Given a non-empty string s and a dictionary wordDict containing a list ...

  7. leetcode 140. Word Break II | 140. 单词拆分 II(动态规划)

    题目 https://leetcode.com/problems/word-break-ii/ 题解 由 leetcode 139. Word Break | 139. 单词拆分(动态规划) 改造而来 ...

  8. 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 ...

  9. 139. Word Break 单词拆分

    Title 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词. 说明: 拆分时可以重复使用字典中的单词. 你可以假设字 ...

最新文章

  1. springboot输出hello world_Spring Boot 入门教程 | 图文讲解
  2. hdu2561 第二小整数(排序)
  3. 事务的四个特性-ACID
  4. linux中多进程调试,linux下用gdb调试多进程
  5. 利用memcached实现CAS单点登录集群部署
  6. 细分将成为2011手机市场的主旋律
  7. 霸榜 | 微软CV模型收获近 2k star
  8. android int 首位值_Android中数值的精确计算
  9. JAVA redis 常用函数
  10. F5-yumnfsftp
  11. TCP/IP之封装,分用,server模型
  12. SQL的bit列名转换成access是/否数据类型,True无效的问题
  13. 批量给excel表中的每行添加表头
  14. 在 Java 应用程序中访问USB设备
  15. 解决Chrome、360自动填充用户名和密码行为带来的困扰
  16. Java 中文转拼音/汉字转拼音, 中文转五笔/汉字转五笔, 下载字典!
  17. 华东师范大学计算机坏老师,华东师范大学:2020各专业录取分数线!毕业当老师的都是铁饭碗...
  18. php的两种安装方式,PHP的安装方法
  19. 灯泡(信息学奥赛一本通 1438)
  20. 使用python对url编码解码 (转)

热门文章

  1. guvcview调试UVC摄像头
  2. 阿良学python:dict和set
  3. ES6 Generator函数 深入应用
  4. html中字体移动怎么设置彩铃,电话彩铃怎么设置
  5. 最小二乘法的矩阵表达
  6. 二级建造师学习心得总结
  7. 菜鸟教程java_JAVA笔记(菜鸟教程)
  8. 极简主义设计的魅力在哪里
  9. 模拟信号电流转电压模块0-3V/5v/4-20ma转0-10V/0-200MV/400mv
  10. 在linux下 nasm 编译,Ubuntu上安装nasm以及nasm在Ubuntu上的简单使用