原题地址:https://oj.leetcode.com/problems/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了。不过直接用dfs解题是不行的,为什么?因为决策树太大,如果全部遍历一遍,时间复杂度太高,无法通过oj。那么我们需要剪枝,如何来剪枝呢?使用word break题中的动态规划的结果,在dfs之前,先判定字符串是否可以被分割,如果不能被分割,直接跳过这一枝。实际上这道题是dp+dfs。

代码:

class Solution:
# @param s, a string
# @param dict, a set of string
# @return a list of strings
def check(self, s, dict):
dp = [False for i in range(len(s)+1)]
dp[0] = True
for i in range(1, len(s)+1):
for k in range(0, i):
if dp[k] and s[k:i] in dict:
dp[i] = True
return dp[len(s)] def dfs(self, s, dict, stringlist):
if self.check(s, dict):
if len(s) == 0: Solution.res.append(stringlist[1:])
for i in range(1, len(s)+1):
if s[:i] in dict:
self.dfs(s[i:], dict, stringlist+' '+s[:i]) def wordBreak(self, s, dict):
Solution.res = []
self.dfs(s, dict, '')
return Solution.res

[leetcode]Word Break II @ Python的更多相关文章

  1. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

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

  3. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  4. [LeetCode] Word Break II 拆分词句之二

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  5. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  6. [LeetCode] Word Break II 解题思路

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  7. [Leetcode] word break ii拆分词语

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  8. [LeetCode] Word Break II (TLE)

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  9. LeetCode: Word Break II [140]

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

随机推荐

  1. Orleans安装

    一.Nuget包Orleans NuGet软件包从v1.5.0开始在大多数情况下,您需要使用4个关键的NuGet包: 1,Microsoft Orleans Build-time Code Gener ...

  2. 1063 合并果子 2004年NOIP全国联赛普及组

    题目描述 Description 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等 ...

  3. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem H. Parallel Worlds 计算几何

    Problem H. Parallel Worlds 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c7 ...

  4. mySql---剖析InnoDB索引原理

    摘要: 本篇为参考别人的文章(http://blog.csdn.net/voidccc/article/details/40077329) 1 各种树形结构 本来不打算从二叉搜索树开始,因为网上已经有 ...

  5. 用Docker下搭建GitLab

    最近试了一下Docker,发现用它搭建服务十分方便,就用它搭建了一个gitlab练练手. 首先下载gitlab镜像:         docker image pull gitlab/gitlab-c ...

  6. RabbitMQ消息交换模式简介

    RabbitMQ是AMQP的一个典型实现,它消息发布者的消息发布到Exchange上,同时需要制定routingkey,可以通过指定交换机的不同模式实现不同的行为. RabbitMQ提供了四种Exch ...

  7. 利用webBrowser获取页面iframe中的内容

    1.获取frame的document HtmlDocument htmlDoc = webBrowser1.Document;  htmlDoc = webBrowser1.Document.Wind ...

  8. [Go] 命令行参数解析包(flag 包)使用详解

    Go 的 flag 包可以解析命令行的参数. 一.命令行语法 命令行语法主要有以下几种形式: cmd -flag       // 只支持bool类型 cmd -flag=xxx cmd -flag ...

  9. [Go] panic 和 recover

    通常情况下,函数向其调用方报告错误的方式都是返回一个 error 类型的值.但是,当遇到致命错误的时候,很可能会使程序无法继续运行.这时,上述错误处理方式就太不适合了,Go 推荐通过调用 panic ...

  10. 使用position:relative制作下边框下的小三角

    在制作tab选项卡的时候,有时会有下边框,且下边框下另一个头向下的小三角,这全然能够用css来实现,而不必使用背景图片. 由于使用背景图片时会有一个问题,选项卡内容字数不同.导致使用背景图片时无法控制 ...