原题地址

动态规划题

令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. cmd中无法运行svn命令

    Svn 不是内部或外部命令,也不是可运行的程序 解决方法: 增加“svn安装目录/bin”,例如:C:\Program Files\TortoiseSVN\bin

  2. css3选择器——导图篇

    css3选择器主要有:基本选择器 , 层次选择器,  伪类选择器 ,  伪元素选择器 , 属性选择器 基本选择器  层次选择器 伪类选择器分为 动态伪类选择器, 目标伪类选择器, 语言伪类选择器, U ...

  3. MySQL的相关概念介绍

    MySQL 为关系型数据库(Relational Database Management System), 这种所谓的"关系型"可以理解为"表格"的概念, 一个 ...

  4. PhpStrom 配置Xdebug

    1 到 http://xdebug.org/download.php下载xdebug.注意找到自己对应的php版本.或者可以通过 http://xdebug.org/wizard.php页面,将php ...

  5. RecyclerView的基本创建

    线性显示 类似于listview: 线性宫格显示 类似于grid view: 用线性宫格显示 类似于瀑布流: 结构图: 测试代码: activity_main.xml: <RelativeLay ...

  6. 复习后台代码(与前面clentHttp连接网络结合)

    package com.zzw.LoginServelt; import java.io.IOException; import java.io.PrintWriter; import javax.s ...

  7. MapReduce数据流

    图4.5细节化的Hadoop MapReduce数据流 图4.5展示了流线水中的更多机制.虽然只有2个节点,但相同的流水线可以复制到跨越大量节点的系统上.下去的几个段落会详细讲述MapReduce程序 ...

  8. 【Windows】如何判断当前鼠标是否按下左键或右键

    在delphi中,很多窗体和控件的鼠标事件里面已经将鼠标按键状态封装好传给响应事件的函数,所以这种情况直接使用就可以,但在某些时候,我们没有这些事件可以处理时,想判断鼠标按键是否按下的状态,就需要借助 ...

  9. 菜鸟学习Spring——初识Spring

    一.概念. Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Develop ...

  10. o2o的一些看法

    最近一段时间o2o(online to offline)真是越来越来热了,尤其是微信支持支付功能之后.现在随便哪个地方都可以看到二维码,然后手机一扫就可以看到跑到线上看到卖家一些产品信息了.看到这里面 ...