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

    安装 装载点 : /u01 (第一个挂载点 LUN1) /u01 (第二个挂载点LUN2) /disk01 目录 : /u01/app/oracle /u01/app/app 文件 : 控制文件 :c ...

  2. android贪吃蛇(超级简陋版)

    public class body { public int ax;//代表X周变量 public int ay;//代表Y轴变量 public int getAx() { return ax; } ...

  3. python3批量删除豆瓣分组下的好友

    python3批量删除豆瓣分组下的好友 """ python3批量删除豆瓣分组下的好友 2016年6月7日 03:43:42 codegay 我两年前一时冲动在豆瓣关注了 ...

  4. jQuery 菜单项切换

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. sqoop的export导入到oracle中

  6. iOS6的旋屏控制技巧

    在iOS5.1 和 之前的版本中, 我们通常利用 shouldAutorotateToInterfaceOrientation: 来单独控制某个UIViewController的旋屏方向支持,比如: ...

  7. Javascript中封装window.open的例子

    对window.open进行封装, 使其更好用, 且更兼容, 很多人说window.open不兼容,其实不是, 因为不能直接执行, 必须通过用户手动触发才行;看代码: 代码如下 复制代码 var op ...

  8. Bubble Sort_树状数组

    Problem Description P is a permutation of the integers from 1 to N(index starting from 1).Here is th ...

  9. 浅谈SEO-提交(一)

    前段时间,花了点时间研究了下SEO,Search Engine Optimization,百度百科这样描述: 中文意译为“搜索引擎优化”.SEO是指通过对网站内部调整优化及站外优化,使网站满足搜索引擎 ...

  10. HDU 1536 & 1944

    http://acm.hdu.edu.cn/showproblem.php?pid=1536 http://acm.hdu.edu.cn/showproblem.php?pid=1944 一样的题 题 ...