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 ...
随机推荐
- Codeforces Round #370 (Div. 2) E. Memory and Casinos 线段树
E. Memory and Casinos 题目连接: http://codeforces.com/contest/712/problem/E Description There are n casi ...
- 实现用CSS切割图片的方法
切割图片这里不是真正的切割,只是用CSS取图片中的一部分而已.这样做的好处就是减少了打开网页时请求图片的次数.主要有两种方式,一是做为某一元素的背景图片,二是用img元素的属性. 方法一: 用CSS中 ...
- NPOI 操作Excel
关于NPOI NPOI是POI项目的.NET版本,是由@Tony Qu(http://tonyqus.cnblogs.com/)等大侠基于POI开发的,可以从http://npoi.codeplex. ...
- Android 命令管理项目
今天介绍一下Android怎么用命令管理项目,用命令管理Android项目需要用到Android命令,首先介绍一下用Android命令创建新项目,打开命令提示窗口,导航到Android SDK 的to ...
- java编码转换 unicode to utf-8
private String decodeUnicode(String theString) { char aChar; int len = theString.length(); StringBuf ...
- 【OpenCV】图像的遍历
Mat类的两种遍历比较快的方式,分别给出了按行和按列遍历,以及运行过程图. 原图: 按行遍历过程图 按列遍历过程图 代码如下: //ptr逐行访问 void ptrScanX(Mat& src ...
- JSTL 将double值保留小数点后两位
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <div ...
- C++ 中的形参与返回值
函数调用时,形参对象和返回对象均采用引用方式进行(临时对象作为中介),当一个对象作为参数(非引用)被函数调用时,该对象会通过复制构造函数获得一个临时对象,该临时对象以引用方式传递给函数,简言之,函数会 ...
- HttpClient_4 用法 由HttpClient_3 升级到 HttpClient_4 必看
转自:http://www.blogjava.net/stevenjohn/archive/2012/09/26/388609.html HttpClient程序包是一个实现了 HTTP 协议的客户端 ...
- MFC各种控件的常见操作(逐步添加中......)
由于经常切换系统或界面框架进行编程,难免有时会忘记之前的编程函数等等.所有在此自己做一个备份,以防止重新充电带来的痛苦! 1.CButton 1.1 在VC中编程实现按钮的启用(enable)和禁用( ...