原题地址

动态规划题

令s[i..j]表示下标从i到j的子串,它的所有分割情况用words[i]表示

假设s[0..i]的所有分割情况words[i]已知。则s[0..i+1]的分割情况words[i+1] = words[k] + s[k+1..i+1],其中(有三个条件要满足)(1) 0 <= k <= i,(2) words[k]非空,(3) s[k+1..i+1]在字典中。

根据这个递推公式求解,有两种枚举方式:

1. 对于每个待求解的位置i,从0到i枚举所有的k,然后检验words[k]是否非空,以及s[k+1..i+1]是否在字典中

2. 对于每个待求解的位置i,枚举字典中的所有单词w,计算出k=i-w.length,然后检验是否0 <= k <= i,以及s[k+1..i+1]和w是否相等

两种方式各有优缺点,如果枚举k,则当原串s特别长的时候,效率比较低;如果枚举字典,当字典里的单词很多的时候,效率比较低。

感觉第一种方式(枚举k)更加自然一些。

最后需要注意的是,最坏情况下枚举的结果是2^n数量级的,此时如果把每个s[0..i]的所有分割情况都保存下来,内存会爆掉。所以只保存s[0..i]分割后的最后一个单词,最后用广搜构造所有解。

代码:

注:上面所说的"words"对应下面代码中的"record"

 vector<string> wordBreak(string s, unordered_set<string> &dict) {
map<int, vector<string> > record;
int len = s.length(); // DP枚举
for (int i = ; i < len; i++) {
vector<string> words; if (dict.find(s.substr(, i + )) != dict.end())
words.push_back(s.substr(, i + )); for (int j = ; j <= i; j++) {
vector<string> pres = record[j - ];
string post = s.substr(j, i - j + );
if (!pres.empty() && dict.find(post) != dict.end()) {
words.push_back(post);
}
} record.insert(pair<int, vector<string> >(i, words));
} // BFS构造
vector<string> res;
queue<pair<int, string> > que;
for (auto r : record[len - ])
que.push(pair<int, string>(len - r.length(), r));
while (!que.empty()) {
pair<int, string> p = que.front();
que.pop();
if (p.first <= )
res.push_back(p.second);
else {
for (auto w : record[p.first - ])
que.push(pair<int, string>(p.first - w.length(), w + " " + p.second));
}
} return res;
}

Leetcode#140 Word Break II的更多相关文章

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

  2. leetcode 140. Word Break II ----- java

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

  3. Java for LeetCode 140 Word Break II

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

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

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

  5. 140. Word Break II(hard)

    欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...

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

  7. [Leetcode Week9]Word Break II

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

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

  9. 【LeetCode】140. Word Break II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...

随机推荐

  1. Yii中使用PHPExcel导出Excel

    最近在研究PHP的Yii框架,很喜欢,碰到导出Excel的问题,研究了一下,就有了下面的方法: 1.首先在config/main.php中添加对PHPExcel的引用,我的方式是这样: // auto ...

  2. Delphi 2010初体验,是时候抛弃Delphi 7了

    Delphi 2010已于近日由Embarcadero公司发布.作者Kim Madsen作为一名资深的Delphi开发者,在他的博客中谈到了Delphi 2010的新性能.它的使用感受以及对Delph ...

  3. 小木棍 (codevs 3498)题解

    [问题描述] 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过100. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它们的长度. 给出每段小木棍的长 ...

  4. STM32F0xx_FLASH编程(片内)配置详细过程

    Ⅰ.概述 关于数据的储存,我觉得编程的人基本上都会使用到,只是看你储存在哪里.STM32的芯片内部FLASH都是可以进行编程的,也就是说可以拿来储存数据.但是,很多做一些小应用程序开发的人都没有利用好 ...

  5. Linux驱动开发之字符设备模板

    /***************************** ** 驱动程序模板* 版本:V1* 使用方法(末行模式下):* :%s/xxx/"你的驱动名称"/g********* ...

  6. Python学习教程(learning Python)--1.2Python输入输出与处理

    一般在做Python程序设计时,通常程序的结构由三部分组成: 输入语句,主要用于输入数据: 数据处理语句,一般对数据进行算术.逻辑等运算处理操作: 输出语句,将输入或者处理结果输出,用于与用户交互. ...

  7. C#学习笔记(补充)——扩展方法、事件

    (搬运自我在SegmentFault的博客) 一.扩展方法 扩展方法使你能够向现有类型"添加"方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 注意事项: 扩展方法 ...

  8. 'mysql.column_stats' doesn't exist and Table 'mysql.index_stats' doesn't exist

    在生产库MariabDB中修改字段类型,提示如下错误:​Table 'mysql.column_stats' doesn't existTable 'mysql.index_stats' doesn' ...

  9. EntityFramwork(2Database First) 源地址https://msdn.microsoft.com/zh-cn/data/jj193542

    必备条件 要完成本演练,需要安装 Visual Studio 2010 或 Visual Studio 2012. 如果使用的是 Visual Studio 2010,还需要安装 NuGet.     ...

  10. 无法解决“Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”与“Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”之间的冲突。正在随意选择“Newtonsoft.Jso

    今天的程序莫名报错:  无法解决“Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed” ...