Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences.

Note:

  • The same word in the dictionary may be reused multiple times in the segmentation.
  • You may assume the dictionary does not contain duplicate words.

Example 1:

Input:
s = "catsanddog"
wordDict = ["cat", "cats", "and", "sand", "dog"]
Output:
[
  "cats and dog",
  "cat sand dog"
]

Example 2:

Input:
s = "pineapplepenapple"
wordDict = ["apple", "pen", "applepen", "pine", "pineapple"]
Output:
[
  "pine apple pen apple",
  "pineapple pen apple",
  "pine applepen apple"
]
Explanation: Note that you are allowed to reuse a dictionary word.

Example 3:

Input:
s = "catsandog"
wordDict = ["cats", "dog", "sand", "and", "cat"]
Output:
[] Solution:
  方法一,使用递归:【通不过牛客的例子】
 class Solution {
public:
unordered_map<string, vector<string>>map;
vector<string> wordBreak(string s, unordered_set<string> &dict) {
if (map.find(s) != map.end())return map[s];
if (s.empty())return { "" };
vector<string>res;
for (auto word : dict)
{
if (s.substr(, word.size()) != word)continue;
vector<string>rem = wordBreak(s.substr(word.size()), dict);
for (auto str : rem)
res.push_back(word + (str.empty() ? "" : " ") + str);
}
return map[s] = res;
}
};

  方法二:使用动态规划
 //使用动态规划

 class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string> &dict) {
int len = s.length();
dp = new vector<bool>[len];
for (int pos = ; pos < len; pos++) {
for (int i = ; i < len - pos + ; i++) {
if (dict.find(s.substr(pos, i)) != dict.end())
dp[pos].push_back(true);
else
dp[pos].push_back(false);
}
}
dfs(s, len - );
return res;
}
void dfs(string s, int n) {
if (n >= ) {
for (int i = n; i >= ; i--) {
if (dp[i][n - i]) {
mid.push_back(s.substr(i, n - i + ));
dfs(s, i - );
mid.pop_back();
}
}
}
else {
string r;
for (int j = mid.size() - ; j >= ; j--) {
r += mid[j];
if (j > )
r += " ";
}
res.push_back(r);
}
}
vector<bool> *dp;
vector<string> res;
vector<string> mid;
};
												

力扣算法——140WordBreakII【H】的更多相关文章

  1. 力扣算法——135Candy【H】

    老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分. 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分配到 1 个糖果.相邻的孩子中,评分高 ...

  2. 力扣算法题—069x的平方根

    实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...

  3. 力扣算法经典第一题——两数之和(Java两种方式实现)

    一.题目 难度:简单 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数, 并返回它们的数组下标. 你可以假设每种输入只会对应一 ...

  4. C++双指针滑动和利用Vector实现无重复字符的最长子串—力扣算法

    题目: 力扣原题链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 给定一个字符串, ...

  5. 力扣算法题—060第K个排列

    给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...

  6. 力扣算法题—050计算pow(x, n)

    #include "000库函数.h" //使用折半算法 牛逼算法 class Solution { public: double myPow(double x, int n) { ...

  7. 力扣算法题—079单词搜索【DFS】

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用. ...

  8. 力扣算法题—052N皇后问题2

    跟前面的N皇后问题没区别,还更简单 #include "000库函数.h" //使用回溯法 class Solution { public: int totalNQueens(in ...

  9. 力扣算法题—051N皇后问题

    #include "000库函数.h" //使用回溯法来计算 //经典解法为回溯递归,一层一层的向下扫描,需要用到一个pos数组, //其中pos[i]表示第i行皇后的位置,初始化 ...

随机推荐

  1. vue 项目配置sass

    1.运行npm install node-sass --save-dev npm install sass-loader --save-dev 2.打开build文件夹下面的webpack.base. ...

  2. 初识redis基础

    一.redis 的五大数据类型: 1.String(字符串): 2.List(列表): 3.Set(集合): 4.Hash(哈希,类似于Java里的Map); 5.Zset(sorted set:有序 ...

  3. PAT甲级【2019年3月考题】——A1158 TelefraudDetection【25】

    Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting ...

  4. .net分页方法

    .net分页方法 //记录分页的总条数 DX.Model.Container.PagerDataContainer Container = new DX.Model.Container.PagerDa ...

  5. python基本数据类型的问题

    关于python中的索引和切片: 在之前看的视屏中是这么描述的:索引值以 0 为开始值,-1 为从末尾的开始位置. 然后今天忽然有了醍醐灌顶的感觉,索引值以 0 为开始值: 就是说从左向右以0开始递增 ...

  6. SpringBoot-SpringCloud-版本对应关系

    刚开始研究SpringCloud和SpringBoot的时候,困惑点比较多,SpringBoot是从1.X.X到目前的2.X.X版本,但是SpringCloud基于SpringBoot,版本之间肯定是 ...

  7. java CountDownLatch、CyclicBarrier和 Semaphore用法

    一.CountDownLatch用法 CountDownLatch类位于java.util.concurrent中包下,利用它可以实现类似计数器的功能.比如有一个任务A,它要等待其他4个任务执行完毕之 ...

  8. go语言从例子开始之Example29.关闭通道

    关闭 一个通道意味着不能再向这个通道发送值了.这个特性可以用来给这个通道的接收方传达工作已经完成的信息. Example: package main import "fmt" // ...

  9. 转载:Think in AngularJS:对比jQuery和AngularJS的不同思维模式(大漠穷秋)

    导言 stackoverflow上有一个人问了一个问题:如果我有jQuery背景,我应该如何切换到AngularJS的思维模式? 有一个回复非常经典,获得了两千多票. 为了让国内开发者也能领略到其中的 ...

  10. 入门phantomjs

    前言 phantomjs是一个无界面浏览器,用来操作web页面的一个工具,比如登录,提交表单等等. 语法框 //创建一个浏览器对象 var page = require('webpage').crea ...