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. codeforces 489B. BerSU Ball 解题报告

    题目链接:http://codeforces.com/problemset/problem/489/B 题目意思:给出 n 个 boys 的 skills 和 m 个 girls 的 skills,要 ...

  2. CodeForces - 417E(随机数)

    Square Table Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit ...

  3. IE的浏览器模式和文档模式

    只有IE浏览器中才会有“浏览器模式”和“文档模式”,兼容性视图涉及两个重要的功能 便是“浏览器模式[browser mode]”和“文档模式[document mode]”,在IE8/IE9中按F12 ...

  4. osgEarth例子

    #include <osgViewer/Viewer>#include <osgViewer/ViewerEventHandlers>#include <osgGA/St ...

  5. Lucene查询索引(分页)

    分页查询只需传入每页显示记录数和当前页就可以实现分页查询功能 Lucene分页查询是对搜索返回的结果进行分页,而不是对搜索结果的总数量进行分页,因此我们搜索的时候都是返回前n条记录 package c ...

  6. javascript实现的图数据结构的广度优先 搜索(Breadth-First Search,BFS)和深度优先搜索(Depth-First Search,DFS)

    最后一例,搞得快.三天之内走了一次.. 下一步,面象对像的javascript编程. function Dictionary(){ var items = {}; this.has = functio ...

  7. AutoCompleteTextView自动填充文本

    布局: <AutoCompleteTextView android:id="@+id/auto" android:layout_width="match_paren ...

  8. 将long型转换为多少MB的方法

    Formatter.formatFileSize( this, processInfo.getMemsize()) public class DensityUtil { /** * 根据手机的分辨率从 ...

  9. 改变传统的开单模式------手持POS终端移动销售开单 移动进销存的利器

    手持POS终端高清彩屏,清晰.美观.大方,适用于仓库.超市.服装.食品.批发零售.手机电脑等企业管理.可与管理软件灵活对接.1:员工记不住价格,产品名称,只要有PDA扫描,价格,库存,直接开销售单,打 ...

  10. HDU4812 D Tree(树的点分治)

    题目大概说给一棵有点权的树,输出字典序最小的点对,使这两点间路径上点权的乘积模1000003的结果为k. 树的点分治搞了.因为是点权过根的两条路径的LCA会被重复统计,而注意到1000003是质数,所 ...