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

题意:找出S用dict表示的各种可能。

思路:这题和Word break的区别是上题中,只要考虑是否在的情况,不用考虑各种组合的问题。参考了GeekFans的。其整体思路是,先使用动态规划,找到字符串S中的各种分类,然后使用DFS找到满足条件的各种情况。代码如下:

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

注:在牛客网通过了,然后一段时间后再次运行时,报错,的结果让我无话可说,见图:

网友GrangyangCode Gander分别给出不错解法。

[Leetcode] word break ii拆分词语的更多相关文章

  1. [LeetCode] Word Break II 拆分词句之二

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

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

  3. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  4. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  5. [LeetCode] 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 @ Python

    原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words  ...

  7. [LeetCode] Word Break II (TLE)

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

  8. LeetCode: Word Break II [140]

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

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

随机推荐

  1. jQuery 打气球小游戏 点击气球爆炸效果

    最近在学习前端,看到偶尔看到前端小游戏,就想自己写一个小游戏,奈何水平有限,只能写打气球这种简单的,所有的气球都是动态生成的,气球的颜色也是随机的 html部分 <div class=" ...

  2. MetInfo最新网站漏洞如何修复以及网站安全防护

    metinfo漏洞于2018年10月20号被爆出存在sql注入漏洞,可以直接拿到网站管理员的权限,网站漏洞影响范围较广,包括目前最新的metinfo版本都会受到该漏洞的攻击,该metinfo漏洞产生的 ...

  3. Manacher(马拉车)学习笔记

    Manacher可以有效的在\(O(n)\)时间内解决一个字符串的回文子串的题目 目录 简介 讲解 推介 简单的练习 恐怖的练习QAQ 小结 简介 开头都说了,Manacher是目前解决回文子串的最有 ...

  4. Spring BindingResult验证框架Validation特殊用法

    使用注解@Valid(实体属性校验) Springboot实现 Spring实现 一.准备校验时使用的JAR validation-api-1.0.0.GA.jar:JDK的接口: hibernate ...

  5. (数据科学学习手札31)基于Python的网络数据采集(初级篇)

    一.简介 在实际的业务中,我们手头的数据往往难以满足需求,这时我们就需要利用互联网上的资源来获取更多的补充数据,但是很多情况下,有价值的数据往往是没有提供源文件的直接下载渠道的(即所谓的API),这时 ...

  6. 三角形xjoi 8.14

    问题描述:离圣诞节只有一个月了,家里要你准备一个很大的星星,然后把它粘在圣诞树的顶端.你已经准备好了一个三角形的银色包装纸来做星星,可忽然有一天你发现在这张大纸上被弄了好多的小洞,原来是你的弟弟妹妹已 ...

  7. x86的控制寄存器CR0,CR1,CR2,CR3

    状态和控制寄存器组除了EFLAGS.EIP ,还有四个32位的控制寄存器,它们是CR0,CR1,CR2和CR3. 这几个寄存器中保存全局性和任务无关的机器状态. CR0中包含了6个预定义标志,0位是保 ...

  8. Git的升级版本

    关于升级版本,例如我们要升级service版本,我们可以这样子操作 1.在master里面pull完了之后,到自己的分支,然后merge master里面的代码,然后把pom文件 里面的版本升一级,然 ...

  9. PHP.40-TP框架商城应用实例-后台15-商品属性与库存量1-不同商品(唯一属性、可选属性),属性类型

    思路: 1.不同商品属于不同的类型,如:手机.服装.电脑等类型 2.不同的类型有不同的属性,其中分为唯一属性和可选属性,如服装:可选属性{尺寸:S,M,L……;颜色:白色,黑色……}唯一属性:材质 首 ...

  10. jmeter更改启动编码设置

    项目中碰到这样的问题,在eclipse经过utf-8转码的代码,能正常运行,放到了jmeter里面运行,就是乱码,如下: String s = "乔佳飞"; String ss = ...