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. GO语言学习(十九)Go 错误处理

    Go 错误处理 Go 语言通过内置的错误接口提供了非常简单的错误处理机制. error类型是一个接口类型,这是它的定义: type error interface { Error() string } ...

  2. 十分钟上手-搭建vue开发环境(新手教程)

    想写一些关于vue的文章已经很久了,因为这个框架已经火了很久,在公司里用的框架都比较老旧,但怎么也得跟上前端发展变化的潮流,这不,开始使用vue开发项目了,一遍开发一边踩坑中,今天要记录的是五分钟搭建 ...

  3. 使用ajax发送图片等文件

    1.使用H5的FormData()对象,append()添加文件 2.processData: false, // 告诉jQuery不要去处理发送的数据    contentType: false, ...

  4. jQuery知识点汇总

    $(this) 当前 HTML 元素 $("p") 所有 <p> 元素 $("p.intro") 所有 class="intro" ...

  5. loadrunne-- Analysis 分析器

    本文转自:https://www.cnblogs.com/Chilam007/p/6445165.html Analysis简介 分析器就是对测试结果数据进行分析的组件,它是LR三大组件之一,保存着大 ...

  6. System.Text.Encoding.Default

    string strTmp = "abcdefg某某某";int i= System.Text.Encoding.Default.GetBytes(strTmp).Length;/ ...

  7. keil出现一些库函数没有定义

  8. JavaScript中双叹号“!!”作用

    1.JavaScript的逻辑非(!)操作符的作用 (逻辑非) 如果操作数能够转换为true则返回false:否则返回true. 2.!!的作用 !!一般用来将后面的表达式强制转换为布尔类型的数据(b ...

  9. swift项目第四天:动态加载控制器

    一:Appdelegate import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate ...

  10. WPF遍历当前容器中某种控件的方法

    原文:WPF遍历当前容器中某种控件的方法 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37591671/article/details/79 ...