Word Break II 题解

题目来源:https://leetcode.com/problems/word-break-ii/description/


Description

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. You may assume the dictionary does not contain duplicate words.

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

Solution

class Solution {
public:
vector<string> wordBreak(string s, vector<string>& wordDict) {
int size = s.size();
string outputStr;
vector<string> result;
vector<bool> flags(size + 1, true);
dfs(s, 0, wordDict, flags, outputStr, result);
return result;
} void dfs(string& s, int startIdx, vector<string>& wordDict, vector<bool>& flags, string& outputStr, vector<string>& result) {
int size = s.size();
if (startIdx == size) {
result.push_back(outputStr.substr(0, outputStr.size() - 1));
return;
} for (int i = startIdx; i < size; i++) {
string tempSubStr = s.substr(startIdx, i + 1 - startIdx);
if (find(wordDict.begin(), wordDict.end(), tempSubStr) != wordDict.end() && flags[i + 1]) {
outputStr += tempSubStr + " ";
int preSize = result.size();
dfs(s, i + 1, wordDict, flags, outputStr, result);
if (preSize == result.size())
flags[i + 1] = false;
outputStr.resize(outputStr.size() - tempSubStr.size() - 1);
}
}
}
};

解题描述

这道题虽然表面上是第一道Word Break的升级版,但是不同于第一道,这道题使用动态规划可能会出现问题。刚开始做的时候我就想着改用第一道题的做法就好,但是几次提交都是MLE;修改了代码,减少了变量的使用之后,在后面一个比较长的测例上面一直TLE。自己本机上也是运行那个测例的时候被KILL掉,查看系统日志原因仍然是超内存。查阅了一些资料之后才发现,这道题使用动态规划比较繁琐而且效率不高,没有做好剪枝的话还很容易就会出现MLE。结合各种资料重新修改代码,最终还是采用了DFS来解决,并且采用一个bool数组flags来做剪枝标记。

[Leetcode Week9]Word Break II的更多相关文章

  1. 【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 ...

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

  3. [Leetcode Week9]Word Break

    Word Break 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-break/description/ Description Given ...

  4. leetcode 140. Word Break II ----- java

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

  5. Java for LeetCode 140 Word Break II

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

  6. 【leetcode】Word Break II (hard)★

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

  7. Leetcode#140 Word Break II

    原题地址 动态规划题 令s[i..j]表示下标从i到j的子串,它的所有分割情况用words[i]表示 假设s[0..i]的所有分割情况words[i]已知.则s[0..i+1]的分割情况words[i ...

  8. [LeetCode] 139. Word Break 单词拆分

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

  9. LeetCode之“动态规划”:Word Break && Word Break II

     1. Word Break 题目链接 题目要求: Given a string s and a dictionary of words dict, determine if s can be seg ...

随机推荐

  1. Spring实战第四章学习笔记————面向切面的Spring

    Spring实战第四章学习笔记----面向切面的Spring 什么是面向切面的编程 我们把影响应用多处的功能描述为横切关注点.比如安全就是一个横切关注点,应用中许多方法都会涉及安全规则.而切面可以帮我 ...

  2. 企业级Nginx Web服务优化实战

    web优化一览总结表 优化类型 优化说明 优化方法 安全优化 隐藏nginx版本信息优化 修改nginx配置文件实现优化 server_tokens off: 修改nginx版本信息优化 修改ngin ...

  3. (原创)最小生成树之Prim(普里姆)算法+代码详解,最懂你的讲解

    Prim算法 (哈欠)在创建最小生成树之前,让我们回忆一下什么是最小生成树.最小生成树即在一个待权值的图(即网结构)中用一个七拐八绕的折线串连起所有的点,最小嘛,顾名思义,要权值相加起来最小,你当然可 ...

  4. 关键词提取TF-IDF算法/关键字提取之TF-IDF算法

    TF-IDF(term frequency–inverse document frequency)是一种用于信息检索与信息探勘的常用加权技术.TF的意思是词频(Term - frequency),  ...

  5. thinkPHP form表单提交参数无法获取

    后台打印获取的数据为empty, 找了半天,是因为 input标签没有写name, 真是醉了!记一下,免得以后再犯错了.

  6. linux备忘录-例行性工作排程 (crontab)

    例行性工作排程 例行性工作排程分为两类 at at是只执行一次就结束的指令安排.要想使用at,必须要有atd服务的支持. crontab crontab是每隔一段时间自动执行的指令安排.crontab ...

  7. HDU 4757 Tree(可持久化字典树)(2013 ACM/ICPC Asia Regional Nanjing Online)

    Problem Description   Zero and One are good friends who always have fun with each other. This time, ...

  8. winform 不同语言(中文,英文等)

    Visual Studio 对于.NET 程序的本地化提供了完整的支持,这里仅介绍实现多语言版本本地化程序的简单步骤.注意黑体处为关键点.一. 窗体本地化    对于Windows 窗体,你需要做的第 ...

  9. Hadoop执行bin/stop-all.sh时no namenode to stop异常

    1 先关闭hadoop所有服务 命令: bin/stop-all.sh 2 格式化namenode 命令: bin/hadoop namenode -format 3 重新启动所有服务 命令: bin ...

  10. 【python】Python 字典(Dictionary)操作详解

    Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型.一.创建字典字典由键和对应值成对组成.字典也被称作关联数组或哈希表.基本语法如下: dict = {'} ...