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

基本思路:

深度递归(暴力偿试法)

再结合剪枝操作。

剪枝操作思路为:

使用一个数组:breakable[i],  表示从第 i个字符向后直至结束。是否可分解为句子。

初始皆为true.

当发现从一个位置不可分隔成数组。则置上标志,以避免兴许的反复偿试。

推断不可分隔也非常easy,即从一个位置起開始递归后。假设结果集没有添加,则该位置不可分隔。

此代码在leetcode上实际运行时间为4ms。

class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
vector<string> ans;
vector<int> breakable(s.size()+1, true);
dfs(ans, s, wordDict, "", 0, breakable);
return ans;
} void dfs(vector<string> &ans, const string &s, const unordered_set<string> &wordDict,
string sentence, int start, vector<int> &breakable) {
if (start == s.size()) {
ans.push_back(sentence);
return;
} if (!sentence.empty())
sentence.push_back(' '); const int old_size = ans.size();
for (int i=start+1; i<=s.size(); i++) {
if (!breakable[i])
continue;
const string word(s.substr(start, i-start));
if (wordDict.find(word) != wordDict.end()) {
dfs(ans, s, wordDict, sentence + word, i, breakable);
}
}
if (old_size == ans.size())
breakable[start] = false;
}
};

Word Break II -- leetcode的更多相关文章

  1. Word Break II leetcode java

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

  2. Word Break II——LeetCode

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

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

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

  5. [Leetcode Week9]Word Break II

    Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...

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

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

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

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

  9. 17. Word Break && Word Break II

    Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...

随机推荐

  1. 分治法(divide & conquer)与动态规划(dynamic programming)应用举例

    动态规划三大重要概念:最优子结构,边界,状态转移公式(问题规模降低,如问题由 n 的规模降低为 n−1 或 n−2 及二者之间的关系): 0. 爬台阶 F(n)⇒F(n−1)+F(n−2) F(n−1 ...

  2. netty reactor线程模型分析

    netty4线程模型 ServerBootstrap http示例 // Configure the server. EventLoopGroup bossGroup = new EpollEvent ...

  3. 结合Wireshark捕获分组深入理解TCP/IP协议栈

    摘要:     本文剖析了浏览器输入URL到整个页面显示的整个过程,以百度首页为例,结合Wireshark俘获分组进行详细分析整个过程,从而更好地了解TCP/IP协议栈.   一.俘获分组 1.1 准 ...

  4. imresize() 函数——matlab

    功能:改变图像的大小. 用法:B = imresize(A,m)B = imresize(A,m,method)B = imresize(A,[mrows ncols],method) B = imr ...

  5. matlab 程序发布

    将matlab程序发布为可执行程序包 说明,这种可执行程序包可以在没有安装matlab的计算机上运行. 1. 打开Applicaiton Compler 如果下拉列表中没有这个APPLICATIOND ...

  6. [TypeStyle] Add type safety to CSS using TypeStyle

    TypeStyle is the only current CSS in JS solution that is designed with TypeSafety and TypeScript dev ...

  7. vim修复,telnet安装启动,linux更新软件源

     vim修复: 修复前提,你到UBUNTU能够联网.否则仅仅能卸载,不能安装 1.sudo apt-get remove vim-common 2.sudo apt-get install vim ...

  8. JDK8 直接定义接口中静态方法

    JDK8前,接口只能是抽象方法. 但是在JDK8中,静态方法是可以直接定义方法体,可以直接用接口名调用.实现类和实现是不可以调用的 一.直接调用接口的静态方法 二.实现接口的子类来调用接口的静态方法 ...

  9. 终端复用工具tmux的使用

    tmux的作用在于终端复用. 1. 在server上启动一个bash.并在里面执行tmux 2. 通过ssh远程登录server,执行tmux attach,就会切换到server上的那个bash中, ...

  10. ios开发网络学习:一:NSURLConnection发送GET,POST请求

    #import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelegate> ...