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. PHP实现SMTP邮件的发送实例

    当你还在纠结php内置的mail()函数不能发送邮件时,那么你现在很幸运,此时的这篇文章可以帮助到你! php利用smtp类来发邮件真是屡试不爽,我用过很久了,基本上没出过问题.本博客后台,当博主回复 ...

  2. laravel路由组+中间件

    在rotues中的web.php

  3. 程序设计的SOLID原则

    要想设计一个良好的程序,建议采用SOLID原则,若考虑了SOLID,可以使程序在模块内具有高内聚.而模块间具有低耦合的特点. SOLID原则包括5方面的内容: S---单责任原则(SRP) 一个模块只 ...

  4. Scrapy进阶

    当我们使用scrapy框架爬取网站的时候,我们会有一个入口的url,一个名为start_urls,我们爬取的第一个网页是从这一开始的. 需求: 现在我们有一个这样的需求,比如说我们对起始的URL有一个 ...

  5. ruby URI类

    一. URI require 'uri' uri = URI("http://foo.com/posts?id=30&limit=5#time=1305298413") # ...

  6. u-boot.bin生成过程分析

    ELF格式“u-boot”文件的生成规则如下,下面对应Makefile的执行过程分别分析各个依赖. $(obj)u-boot: depend version $(SUBDIRS) $(OBJS) $( ...

  7. NO-ZERO(空格补全)

    The NO-ZERO command follows the DATA statement REPORT Z_Test123_01. DATA: W_NUR(10) TYPE N. MOVE 50 ...

  8. SAPの販売管理で、価格設定をするまでの関連カスタマイズ画面

    この記事ではSAP SDで.価格を決めるまでに必要な設定画面について述べています. condition table (条件テーブル) 条件レコードのキー項目を定義したもの.3桁の数字で名前がついている ...

  9. atlas+mysql主主集群实现读写分离

     atlas+mysql主主集群实现读写分离 前言: 目前线上系统数据库采用的是主主架构.其中一台主仅在故障时切换使用,(仅单台服务器对外提供服务,当一台出现问题,切换至另一台).该结构很难支撑较大并 ...

  10. Android Studio modify language level to Java 8

    If you need use lambda, should modify language level File -> Project Structure -> app -> Pr ...