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"].

思路:利用I的dp判断是否有解,如果有解,使用DFS存储结果(类似 Palindrome Partitioning)

class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
//dp[i]: s[0...i] can be egemented in dict
//dp[i] = dp[0][k] && d[k][i]
int len = s.length();
vector<bool> dp(len,false);
for(int i = ; i < len; i++){
if(wordDict.find(s.substr(,i+))!=wordDict.end()) dp[i]=true;
for(int j = ; j <= i; j++){
if((wordDict.find(s.substr(j,i-j+))!=wordDict.end()) && dp[j-]) dp[i]=true;
}
}
if(dp[len-]) dfs(s,,"",wordDict);
return ret;
}
void dfs(string s, int depth, string strCur, unordered_set<string>& wordDict){
for(int i = depth; i < s.length(); i++){
if(wordDict.find(s.substr(depth,i-depth+))==wordDict.end()) continue;
if(i==s.length()-) ret.push_back(strCur+s.substr(depth,i-depth+));
else dfs(s,i+,strCur+s.substr(depth,i-depth+)+" ", wordDict);
}
}
private:
vector<string> ret;
};

140. Word Break II (String; DP,DFS)的更多相关文章

  1. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  2. 140. Word Break II(hard)

    欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...

  3. 140. Word Break II

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

  4. 【LeetCode】140. Word Break II

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  5. LeetCode:Word Break II(DP)

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

  6. [LeetCode] 140. Word Break II 单词拆分II

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

  7. 139. Word Break 以及 140.Word Break II

    139. Word Break Given a non-empty string s and a dictionary wordDict containing a list of non-empty  ...

  8. LeetCode笔记:140. Word Break II

    题目描述 给定一个非空的字符串s,一个非空的字符串list作为字典.通过在s中添加空格可以将s变为由list中的word表示的句子,要求返回所有可能组成的句子.设定list中的word不重复,且每一个 ...

  9. 【LeetCode】140. Word Break II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...

随机推荐

  1. 服务注册发现Eureka之一:Spring Cloud Eureka的服务注册与发现

    Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁 ...

  2. dubbo框架及dubbo环境搭建

    https://blog.csdn.net/liuhaiabc/article/details/52781351 dubbo框架及dubbo环境搭建

  3. 转载-java基础学习汇总

    共2页: 1 2 下一页  Java制作证书的工具keytool用法总结 孤傲苍狼 2014-06-24 11:03 阅读:25751 评论:3     Java基础学习总结——Java对象的序列化和 ...

  4. 3dsMax模型转UE4

    转自:http://blog.csdn.net/qq_24835213/article/details/68063344 一.模型设置: 1.将Vary材质转成标准材质 2.将模型减面 3.加一套UV ...

  5. 属性,类方法@classmethod

    # 属性的初识# class Person:## def __init__(self,name,hight,weight):# self.name = name# self.__hight = hig ...

  6. selenium ie 模拟request pahonjs

  7. spring Ioc和DI

    spring的“控制反转”和“依赖注入”,个人看来是一个意思. 传统java程序中,使用一个对象的时候,都需要先new Object()创建一个新对象,才能使用.对象的控制权,在程序手里. 使用spr ...

  8. WDA-FPM-4-用OVP做查询跳转到明细

    转载:https://www.cnblogs.com/sapSB/p/10100697.html   FPM四:用OVP做查询跳转到明细 前面做了查询的UIBB配置,在这边可以直接复用,查询的feed ...

  9. Datatable数据分组

    datatable里面的数据是按照这个顺序排列的 姓名    性别        年龄 a1          男           12 a1         女             11 a ...

  10. Nginx_status显示结果详解

    打开:http://aabb.com/nginx_status会有如下显示Active   connections: 2872server   accepts  handled requests294 ...