Work Break I

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

For example, given
s = "leetcode",
dict = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

DP很容易就出来了。possible[i]保存[0,i]是否可以被分割的结果。

possible[i] = true, 当存在possible[k] = true,且[k,i]是dict里的一个word时。否则possible[i] = false。

这种是自底而下的。

 class Solution {
public:
bool wordBreak(string s, unordered_set<string> &dict) {
int n = s.length();
if (n == ) return true;
vector<bool> possible(n, false); for (int i = ; i < n; ++i) {
for (int j = i; j >= ; --j) {
if ((j == || possible[j - ]) && dict.find(s.substr(j, i - j + )) != dict.end()) {
possible[i] = true;
break;
}
}
} return possible[n - ];
}
};

算法的时间复杂度最坏情况是O(n^2),空间复杂度是O(n)。

网上也有人用前缀树(Trie树、字典树)实现的。私以为用前缀树还得先将dict里的所有word插进去,时间复杂度为O(n*l+s),l为word的最大长度,s为dict的大小。如果dict的大小比n大得多,那么整个开销也是不菲的。

只要稍微将上面的代码优化一下,先求出word的最大长度,那么时间复杂度也可以优化到O(n*l+s)。

Work Break II

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"].

直接用回溯就可以了。自顶而下。

class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string> &dict) {
vector<string> ret;
bt(s, dict, "", s.length(), ret);
return ret;
} void bt(string &s, unordered_set<string> &dict, string str, int index, vector<string> &ret) {
if (index < ) {
ret.push_back(str.substr(, str.length() - ));
return;
} for (int i = index; i >= ; --i) {
string tmp = s.substr(i, index - i + );
if (dict.find(tmp) != dict.end()) {
bt(s, dict, tmp + " " + str, i - , ret);
}
}
}
};

Leetcode | Work Break I & II的更多相关文章

  1. LeetCode: Word Break I && II

    I title: https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, ...

  2. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  3. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  4. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

  5. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

  6. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

  7. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  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] Word Break II 拆分词句之二

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

随机推荐

  1. 【动态规划】skiing_深度搜索_动态规划

    问题 B: [动态规划]skiing 时间限制: 1 Sec  内存限制: 128 MB提交: 28  解决: 11[提交][状态][讨论版] 题目描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪 ...

  2. [Java基础] SequenceInputStream输入合并流

    转载: http://blog.csdn.net/xuefeng1009/article/details/6955707 public SequenceInputStream(Enumeration& ...

  3. [Ant] Ant之MacroDef—“宏

    转载自:http://lhq1013.iteye.com/blog/1157234 Ant是一个基于Java的跨平台构建工具,它易于使用,并且可扩展.可升级.    MacroDef——“宏”,在An ...

  4. backslash and newline separated by space

    原来是因为\  后面多了一个空格 检查写的代码中将\后面的空格去掉就可以了.

  5. CSS“反转”为LESS

    LESS(官网在此:http://lesscss.net/)的魅力相信大家都已明了,个人认为它最大的魅力在于能够清晰的展现嵌套关系. 针对现有的项目,它的应用难点主要在于—— 何时转换为css,即是否 ...

  6. 深入C++中构造函数、拷贝构造函数、赋值操作符、析构函数的调用过程总结

    转自 http://www.jb51.net/article/37527.htm,感谢作者 #include "stdafx.h"      #include <iostre ...

  7. repo的用法

    转自:http://blog.csdn.net/junglyfine/article/details/6299636 注:repo只是google用Python脚本写的调用Git的一个脚本,主要是用来 ...

  8. Wcf for wp8 使用iis Express 承载Wcf服务部署发布网站(三)

    我们接下来要做的是 本地电脑当作服务器(模拟外网服务器)来承载Wcf服务程序,通过引用本地电脑ip地址访问wcf服务程序接口 http://192.168.1.123/Service1.svc 一.先 ...

  9. 铭飞MCMS内容管理系统完整开源版J2EE代码

    当前版本:4.6.0铭飞MS官网:http://ms.mingsoft.net官网同时提供一键运行版本下载,请步移官网....QQ交流群号1:221335098很多人说铭飞MCMS是大天朝国唯一完整开 ...

  10. Hark的数据结构与算法练习之鸡尾酒排序

    算法说明 鸡尾酒排序又叫定向冒泡排序,鸡尾酒搅拌排序,搅拌排序,涟漪排序,回来排序,快乐小时排序. 鸡尾酒排序是交换排序的一种,它是冒泡排序的一个轻微的变种.冒泡是从低向高比较排序,鸡尾酒从低向高,从 ...