原题地址

动态规划题

令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. CentOS下MySQL忘记root密码解决方法【转载】

    1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 状态下,其他的用户也可以任意地登录 ...

  2. C#和.NET版本

    1999年,就听说微软公司在研发一种名为“cool”的新开发语言,而具体内幕一直是个谜,直到2000年6月26日微软在奥兰多举行的“职业开发人员技术大会”(PDC 2000)上,这个谜底终于揭晓了,这 ...

  3. android 中单选和复选框监听操作

    单选按钮RadioGroup.复选框CheckBox都有OnCheckedChangeListener事件,我们一起了解一下. package com.genwoxue.oncheckedchange ...

  4. C语言接口的写法(以toyls命令为例)

    #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h&g ...

  5. SQL语句中各种数据类型转换方法总结

    1.Access 每个函数都可以强制将一个表达式转换成某种特定数据类型. 语法 CBool(expression) CByte(expression) CCur(expression) CDate(e ...

  6. swift学习(二)--基本运算符、字符串、集合操作

    在这一篇博客里面,我想要介绍一下swift里面一些常用的基本运算符,还有涉及到的字符串,集合操作.你会发现在swift里面还是有许多其他语言所不具有的特性运算操作的. 首先最基本的+,-,*,/,&g ...

  7. dede 忘记密码在数据库中修改方法

    如何找回或修改dedecms后台管理员登录密码呢? 一个客户把密码忘了,找了很长一会没几个靠谱的回答,dede是使用md5加密,但是,它是显示32位md5加密码从第6位开始的20位 方法是直接修改其m ...

  8. centos rsync安装配置

    安装 1 yum -y install rsync ---------------------服务器安装------------------------------- 创建基础配置文件 1 2 3 4 ...

  9. DataTemplate 以及Template Selector 学习笔记

    1. 内容控件通过ContentTemplate,列表控件通过itemTemplate 来支持模板绑定.例子如下: 模板定义: xaml: <DataTemplate x:Key="R ...

  10. OpenGL学习笔记之配置OpenGL

    OpenGL是计算机图形学领域的一门入门语言,OpenGL开发库的一些文件在官网上可以下载到.里面包含三个文件,如下: 1.把在OpenGL开发库中LIB(库文件)glut.lib和glut32.li ...