Word Break

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

说明: 深度搜索,一定要记忆下每次走完的结果(此处记下筛掉的情况)。

bool judge(string s, unordered_set<string> &dict, vector<bool> &tag) {
if(s == "") return true;
for(int i = 1; i <= s.length(); ++i) {
if(tag[s.size()-i] && dict.find(s.substr(0, i)) != dict.end()) {
if (judge(s.substr(i, s.size()-i), dict, tag)) return true;
else tag[s.size()-i] = 0;
}
}
return false;
} class Solution {
public:
bool wordBreak(string s, unordered_set<string> &dict) {
if(s == "") return true;
vector<bool> tag(s.size()+1, true); //the value is the result that (index) length of reserved string can return;
return judge(s, dict, tag);
}
};

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

说明: 方法比较巧妙。记忆下每个位置开始的所有能成回文串的结束位置。然后深搜。

void dfs(string s, vector<vector<int> > & Reach, int Id, string path, vector<string> &vec) {
if(Id == s.size()) { vec.push_back(path); return; }
for(size_t i = 0; i < Reach[Id].size(); ++i) {
path = path + (Id == 0 ? s.substr(Id, Reach[Id][i]) : " " + s.substr(Id, Reach[Id][i]-Id));
dfs(s, Reach, Reach[Id][i], path, vec);
path.erase(path.end()-(Id == 0 ? Reach[Id][i] : (Reach[Id][i]-Id+1)), path.end());
}
}
class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string> &dict) {
vector<string> vec;
int n = s.size();
if(n == 0) return vec;
vector<vector<int> > reachable(n, vector<int>());
for(int end = n; end > 0; --end) {
if(end < n && reachable[end].empty()) continue;
for(int start = 0; start < end; ++start) {
if(dict.find(s.substr(start, end-start)) != dict.end())
reachable[start].push_back(end);
}
}
dfs(s, reachable, 0, string(""), vec);
return vec;
}
};

两题公有的易超时反例:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaa……aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab

17. Word Break && Word Break II的更多相关文章

  1. reverse the string word by word

    题目:Given an input string, reverse the string word by word. For example,Given s = "the sky is bl ...

  2. LeetCode 5:Given an input string, reverse the string word by word.

    problem: Given an input string, reverse the string word by word. For example: Given s = "the sk ...

  3. Microsoft.Office.Interop.Word 创建word

    Microsoft.Office.Interop.Word 创建word 转载:http://www.cnblogs.com/chenbg2001/archive/2010/03/14/1685746 ...

  4. C#用Microsoft.Office.Interop.Word进行Word转PDF的问题

    之前用Aspose.Word进行Word转PDF发现'\'这个字符会被转换成'¥'这样的错误,没办法只能换个方法了.下面是Microsoft.Office.Interop.Word转PDF的方法: p ...

  5. LeetCode之“动态规划”:Word Break && Word Break II

     1. Word Break 题目链接 题目要求: Given a string s and a dictionary of words dict, determine if s can be seg ...

  6. leetcode@ [139/140] Word Break & Word Break II

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

  7. LeetCode ||& Word Break && Word Break II(转)——动态规划

    一. Given a string s and a dictionary of words dict, determine if s can be segmented into a space-sep ...

  8. 18. Word Ladder && Word Ladder II

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

  9. leetcode@ [79/140] Trie树应用 Word Search / Word Search II

    https://leetcode.com/problems/word-search/ class Solution { public: struct Trie{ Trie *next[]; bool ...

随机推荐

  1. DAO JDBC 学生成绩管理系统

    1:student.course类 package JDBCU; public class Student { private String no; private String name; publ ...

  2. redhat6.4安装storm集群-4节点

    0.搭建ftp服务器并建立yum源 1.在每个节点上安装java并设置环境变量 2.在三个节点上安装zookeeper 3.安装zeromq 过程中发现运行./configure时出现问题: conf ...

  3. CENTOS/UBUNTU一键安装IPSEC/IKEV2 VPN服务器

    1.在azure上创建ubuntu虚拟机 选择v15.04 server 版本 2.添加端口号 3.远程桌面到ubuntu 命令行 输入 sudo su  输入创建 ubuntu虚拟机 时候的 密码 ...

  4. jQuery固定浮动侧边栏(jQuery fixed Sidebar)

    这个功能现在应用的非常普遍,如果页面比较高,当滚动条拖到页面的下面的时候,侧边栏会出现一个固定跟随浏览器的DIV框,现思路是这样的:首先获取需要跟随的DIV距离页面顶部的距离,然后判断,当浏览器滚动的 ...

  5. 2016 - 1 - 24 NSURLSession (一)

    一: NSURLSession简介 1.实施步骤 1.1 使用 NSURLSession对象 创建TASK ,然后执行TASK 2.TASK的类型: 二: NSURLSession的简单使用: - ( ...

  6. php大力力 [038节] 全栈工程师的含义

    管理时间 http://www.nowamagic.net/librarys/eight/posts/2753 从知乎上看到“全栈开发者”讨论之后的自黑 什么是全栈开发者 https://beeclo ...

  7. 黑马程序员——【Java基础】——集合框架

    ---------- android培训.java培训.期待与您交流! ---------- 一.集合框架概述 (一)集合框架中集合类关系简化图 (二)为什么出现集合类? 面向对象语言对事物的体现都是 ...

  8. C++ Daily 《1》----关于对象

    1. 问题 请问如下的一个 class 的一个对象占了多少内存? 具体包含哪些东西? non-static 变量? static member 变量? member function?? virtua ...

  9. 纯CSS3实现轮播切换效果

    使用纯css3实现与轮播器一样的功能. HTML代码: <div class="slide-container"> <input type="radio ...

  10. ASP.NET编程模型之ASP.NET页面生命周期图解

    ASP.NET编程模型中ASP.NET页面生命周期是指什么呢?它包括什么呢?ASP.NET编程模型之ASP.NET页面生命周期具体的过程有哪些呢?下面就开始我们的讲解吧: ASP.NET 页运行时,此 ...