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. Ubuntu-C++环境设置

    在学习C++,顺便想熟悉一下Linux下开发 所以就开始搭建Linux环境 第一步就是下载虚拟机和Ubuntu 虚拟机 vm 12.1 Ubuntu 16 VM安装省略 Ubuntu安装省略 下面是我 ...

  2. DP入门(2)——DAG上的动态规划

    有向无环图(DAG,Directed Acyclic Graph)上的动态规划是学习动态规划的基础.很多问题都可以转化为DAG上的最长路.最短路或路径计数问题. 一.DAG模型 [嵌套矩形问题] 问题 ...

  3. Week2 Teamework from Z.XML 软件分析与用户需求调查(二)应用助手功能评测

    评测人:薛亚杰 周敏轩. 说明:言辞激烈,请勿介意. 软件使用概述 我们团队这次评测的必应助手是必应缤纷桌面的一个小功能,根据评测人员试用几天后发现,它的作用大概就是能够用一种看上去比较生动的形式来给 ...

  4. linux中升级安装python2.7

    打算自建VPN,新购买了一个虚拟服务器,centOS6.6 自带的是python2.6,因为比较习惯python2.7,所以就升级到最新的python2.7.12 首先要安装:sudo yum ins ...

  5. lintcode-127-拓扑排序

    127-拓扑排序 给定一个有向图,图节点的拓扑排序被定义为: 对于每条有向边A--> B,则A必须排在B之前 拓扑排序的第一个节点可以是任何在图中没有其他节点指向它的节点 找到给定图的任一拓扑排 ...

  6. lintcode-57-三数之和

    57-三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 注意事项 在三元组(a, b, c),要求a <= b <= ...

  7. CentOS7 php 安装 amqp扩展

    继续安装完 rabbitmq后,安装最新 php amqp扩展 http://www.cnblogs.com/8000cabbage/p/7788575.html 参考:carson 1.安装rabb ...

  8. java生成和解析二维码

    前言 现在,二维码的应用已经非常广泛,在线生成器也是诸多,随手生成. 所以就和大家分享一个小案例,用zxing来做一个的二维码生成器,当然这个例子是比较简单,若是写的不好请多多包涵. ZXING项目是 ...

  9. AndroidStudio3.0 注解报错Annotation processors must be explicitly declared now. The following dependencies on the compile classpath are found to contain annotation processor.

    体验最新版AndroidStudio3.0 Canary 8的时候,发现之前项目的butter knife报错,用到注解的应该都会报错 Error:Execution failed for task ...

  10. SQL Server “超过了锁请求超时时段”错误

    错误提示:“已超过了锁请求超时时段. (Microsoft SQL Server,错误: 1222)”(英文:“Lock Request time out period exceeded.(Micro ...