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 ...
随机推荐
- Windows下gvim配置
Windows下gvim配置原作地:http://hi.baidu.com/leemoncc/blog/item/a6be15cf40d7ab31b600c806.html 0.准备软件及插件. (a ...
- 搭建web服务器环境
一. 安装apache 安装好之后测试:浏览器地址栏输入:localhost,若弹出"It works!"表明已成功安装. 管理方式:1.通过Apache自带的镜像管理器:2.wi ...
- GUI用户界面编程
Java的GUI编程(Graphic User Interface,图形用户接口),是在它的抽象窗口工具箱(Abstract Window Toolkit,AWT)上实现的,java.awt是AWT的 ...
- 解决iOS8安装企业版无反应问题
iOS7可以下载没有任何问题,iOS8发现挂在官网上的企业版的app点击了提示是否安装应用程序,但是确认以后没有反应,找了很久,都没有发现问题.后来查看了的device console发现安装的时候出 ...
- ubuntu频繁掉线 转
好长好长时间没来百度空间了,最近闲来无事,正好弥补之前的空缺了!跟Ubuntu打交道已有很长一段时间了,期间遇到了很多问题,我把遇到的一些问题及找到的解决方案记录下来,我想这可能会对那些跟我有同样境遇 ...
- HANS123
//策略:HANS123//周期:日内//类别:趋势突破 作为外汇市场上广为流行的一种突破交易策略,HANS123以其简洁的开盘后N根K线的高低点突破,作为交易信号触发的评判标准.这也是一种入场较早的 ...
- HDU 4822----其实不会这个题
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4822 并不会做这个题,题解说是LCA(最近公共祖先),并不懂,说一下我自己的思路吧,虽然没能实现出来. 题 ...
- 黑马程序员:Java编程_String
=========== ASP.Net+Android+IOS开发..Net培训.期待与您交流!=========== 描述字符串对象的类是java.lang.String,String类是不可变(f ...
- [专题汇总]AC自动机
1.The 2011 ACM-ICPC Asia Dalian Regional Contest ZOJ 3545 Rescue the Rabbit 简单的AC自动机+状压DP, 状态DP[nod ...
- PAT (Basic Level) Practise:1012. 数字分类
[题目链接] 给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数的和: A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3- ...