17. Word Break && Word Break II
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的更多相关文章
- reverse the string word by word
题目:Given an input string, reverse the string word by word. For example,Given s = "the sky is bl ...
- 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 ...
- Microsoft.Office.Interop.Word 创建word
Microsoft.Office.Interop.Word 创建word 转载:http://www.cnblogs.com/chenbg2001/archive/2010/03/14/1685746 ...
- C#用Microsoft.Office.Interop.Word进行Word转PDF的问题
之前用Aspose.Word进行Word转PDF发现'\'这个字符会被转换成'¥'这样的错误,没办法只能换个方法了.下面是Microsoft.Office.Interop.Word转PDF的方法: p ...
- 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 ...
- 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 ...
- 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 ...
- 18. Word Ladder && Word Ladder II
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- leetcode@ [79/140] Trie树应用 Word Search / Word Search II
https://leetcode.com/problems/word-search/ class Solution { public: struct Trie{ Trie *next[]; bool ...
随机推荐
- 【转】 linux下的g++编译器安装
再debian下直接apt-get install gcc g++就可以了.按照类似的逻辑,再Fedora下yum install gcc g++ 报告无法找到g++包. 查了一下,原来这个包的名字叫 ...
- 2014年4月份第4周51Aspx源码发布详情
精灵豆会员管理系统源码 2014-4-21 [VS2010]功能介绍:精灵豆会员管理系统业务管理平台采用微软选进的C#语言开发,采用大型数据库,具有比较高的执行效率和高安全性.系统分为消费管理,会员 ...
- springmvc学习第二天
一.pojo Spring mvc 会按请求参数名和pojo属性名进行自动匹配,自动为该对象填充属性值,并且支持级联属性 表单: <form action="springmvc/tes ...
- UIImagePickerController 获取相片视频
1.UIImagePickerController的静态方法: imagepicker = [[UIImagePickerController alloc]init]; //UIImagePic ...
- Activity 和 生命周期: 创建
了解了整体的android创建流程之后,就分析一下到底这个过程中做了什么? activity创建中开始时由activityStack中的realstartActivityLocked函数中调用了act ...
- android4.x获取(也可监测)外置sd路径和读写
先上图: 这个小demo是判断手机上是否插入了sd卡(手动插入到手机卡槽的情况),如果拔出sd卡,也会检测到,检测到没有sd的话会提示退出.大家可以修改代码达到自己想要的效果. sd的卡装载状态是从系 ...
- Understanding Linux /proc/cpuinfo
http://www.richweb.com/cpu_info A hyperthreaded processor has the same number of function units as a ...
- 在Swift中应用Grand Central Dispatch(上)转载自的goldenfiredo001的博客
尽管Grand Central Dispatch(GCD)已经存在一段时间了,但并非每个人都知道怎么使用它.这是情有可原的,因为并发很棘手,而且GCD本身基于C的API在 Swift世界中很刺眼. 在 ...
- RC上电复位时间计算
高电平复位电路图 V0 为电容上的初始电压值:V1 为电容最终可充到或放到的电压值:Vt 为t时刻电容上的电压值.则, Vt="V0"+(V1-V0)* [1-exp(-t/ ...
- javaScript DOM JQuery AJAX
http://www.cnblogs.com/wupeiqi/articles/5369773.html 一 JavaScript JavaScript是一门编程语言,浏览器内置了JavaScript ...