Leetcode#140 Word Break II
动态规划题
令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的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
- 140. Word Break II(hard)
欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...
- 【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 ...
- [Leetcode Week9]Word Break II
Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...
- 【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 ...
- 【LeetCode】140. Word Break II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...
随机推荐
- MongoDB数据库的主从配对与迁移示例
数据中心在运行中有可能遇到各种硬件.电力.网络故障等问题,需要设计良好的系统来隔离,尽量减少对上层应用的影响,持续对外提供服务:一旦发生业务中断,也应尽快恢复.通过主从备份设计,在主应用系统发生故障时 ...
- 3种方式实现Java多线程
java中实现多线程的方法有两种:继承Thread类和实现runnable接口. 1.继承Thread类,重写父类run()方法 public class thread1 extends Thread ...
- while循环中不支持循环使用curl
<?php $link = mysql_connect('localhost', 'sms', 'sms'); mysql_select_db('sms', $link); mysql_quer ...
- 编写测试类,了解ArrayList的方法
这篇文章主要介绍了C#中动态数组用法,实例分析了C#中ArrayList实现动态数组的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了C#中动态数组用法.分享给大家供大家参考.具体分析如下 ...
- GetProperties(BindingFlags)说明
Instance|Public:获取公共的的实例属性(非静态的) Instance|NonPublic:获取非公共的的实例属性(非静态的).(private/protect/internal) Sta ...
- UCOS2_STM32F1移植详细过程(二)
Ⅰ.概述 打开上一篇文章新建的工程,是提取的ST标准库里面源代码文件和UCOS工程包源代码文件.下载过的朋友可能会知道,直接编译那个工程会有大片的错误和警告,原因在于那个工程是没有经过修改源代码的工程 ...
- 一些关于python的小感想
python是一门优秀的语言,但随之而来的是大量的知识,各种模块,相信一个人的大脑是很难记住如此多的内容.这时后的我们就应该想办法避免去记忆这么多的内容. 1.查看官方文档(英语很重要,啥也不说了) ...
- python 使用联动优势支付接口的sign与verify
直接上代码 if options.umpay_private_key is not None and len(options.umpay_private_key) > 0: try: with ...
- openSUSE13.1安装搜狗输入法 for Linux
一句话总结:爽死我了!什么叫输入的快感终于体会到了,搜狗输入法,码农的好伙伴!!! 转自openSUSE论坛 女王陛下 https://forum.suse.org.cn/viewtopic.php? ...
- 小心指针被delete两次
C++类中,有时候使用到传值调用(对象实体做参数),遇到这种情况,可要小心了!特别是当你所传值的对象生命周期较长,而非临时对象(生命周期段)的时候.来看看下面的情况: #include <ios ...