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. 使用webBrowser进行C#和JS通讯

    .前台调用后台: 在webBrowser使用过程中为了C#和js通讯,webBrowser必须设置ObjectForScripting的属性,它是一个object,这个object可以提供给webBr ...

  2. thinkphp5 前台模板的引入css,js,images

    一:在公共的静态文件夹中建立我们模块的名称用来放置css,js,images 二:在配置文件config中定义需要的路径 三:在视图页面引入

  3. x-pack本地安装方式

    一.首先下载本地安装包,我使用的ELK是5.6.1版本: https://artifacts.elastic.co/downloads 二.进入到elasticsearch/bin(所有节点)和kib ...

  4. Java学习笔记八:Java的流程控制语句之循环语句

    Java的流程控制语句之循环语句 一:Java循环语句之while: 生活中,有些时候为了完成任务,需要重复的进行某些动作.如参加 10000 米长跑,需要绕 400 米的赛道反复的跑 25 圈.在 ...

  5. Jersey2+swagger组建restful风格api及文档管理

    1.jar包引入 <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId& ...

  6. kylin实战系列(一)

    kylin实战系列(一) 把之前kylin的实践小结一下,以备以后查看.

  7. 13 ThreadLocal

    ThreadLocal 在多线程环境下,每个线程都有自己的数据.一个线程使用自己的局部变量比使用全局变量好,因为局部变量只有线程自己能看见,不会影响其他线程,而全局变量的修改必须加锁. 1. 使用函数 ...

  8. 13 IO多路复用 (未完成)

    IO多路复用 6.select版-TCP服务器:最多1024 import select import socket import sys server = socket.socket(socket. ...

  9. Encrypted bootloader (程序BIN文件加密及在线升级)

    了解更多关于bootloader 的C语言实现,请加我QQ: 1273623966 (验证信息请填 bootloader),欢迎咨询或定制bootloader(在线升级程序). 在上一个博客随笔,我介 ...

  10. 事务消息中心-TMC

    此文已由作者杨凯明授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 背景 为什么要做事务消息中心 原有kqueue的方式缺点: 降低业务库性能 占用业务库磁盘 历史数据管理成本 ...