139. Word Break

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.

For example, given
s = "leetcode",
dict = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

思路:

定义状态矩阵dp[i]表示0-i能被切割,需要先找到0-(j -1),然后j -i 这个区间是否是字典里面的,这样找。思路就是序列性动态规划,事件复杂度是n^2.

注意本题的初始化方法,需要从0开始进行查找符合条件的字典字符串,要找到从0位置开始的所有符合条件的字符串,比如go,goal,goals。

class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
if(s.size() == ){
return false;
}
if(wordDict.size() == ){
return ;
}
int n = s.size();
vector<bool> dp(n,false);
//hashset
unordered_set<string> wordSet;
for(int i = ;i < wordDict.size();++i){
wordSet.insert(wordDict[i]);
}
//find first true
int ix = ;
for(ix = ;ix < n;++ix){
if(wordSet.find(s.substr(,ix + )) != wordSet.end()){
dp[ix] = true;
//break;
}
}
// if(ix == n){
// return dp[ix - 1];
// }开始这里没注释,直接每次退出循环ix都等于n,总是出错,因为是原来break掉,才有这句
//funciton
for(int i = ;i < n;++i){
for(int j = ;j <= i;++j){
if((dp[j - ] == true) && (wordSet.find(s.substr(j,i - j + )) != wordSet.end())){
dp[i] = true;
}
}
} return dp[n - ];
}
};

Word breakII需要找出所有符合条件的分割字符串,并且输出。首先考虑DFS模板,这里的巧妙之处就是start取代了j这个变量,但是复杂度还是平方级别。不熟悉的是string的append,insert,erase(pos,arg),记住是包含pos位置的。参考:水中的鱼

class Solution {
public:
void helper(unordered_set<string> &wordSet,vector<string> &res,string &s,string &tmp,int start){
if(start == s.size()){
res.push_back(tmp.substr(,tmp.size() - ));
}
for(int i = start;i < s.size();++i){
string sub = s.substr(start,i - start + );
if( wordSet.find(sub) == wordSet.end()){
continue;
}
tmp.append(sub).append(" ");//只有每步满足条件之后才开始位置为该步的下一步
helper(wordSet,res,s,tmp,i + );
tmp.erase(tmp.size() - sub.size() - );
}
}
vector<string> wordBreak(string s, vector<string>& wordDict) { if(s.size() == ){
return {};
}
if(wordDict.size() == ){
return {};
}
vector<string> res;
unordered_set<string> wordSet;
for(string tmp : wordDict){
wordSet.insert(tmp);
}
string tmp;
helper(wordSet,res,s,tmp,);
return res;
}
};

这样有很多重复计算,需要引入一个isOK矩阵,记录之前访问的结果,如果在之前i这个位置已经切割了一次,并且没有找到结果,那么就是false,下次不需要再访问了。这里首先都初始化为true。

int oldSize = res.size();
helper(wordSet,res,s,tmp,i + ,isOk);
if(oldSize == res.size()){
isOk[i] = false;
}

这里理解起来比较困难,记住每次经过helper函数应该是有一个结果压入res中的,但是前后size一样大,所以从i这个元素切割没有的得到结果,那下次切割到这个i的时候,就不需要再计算了,直接跳过。

class Solution {
public:
void helper(unordered_set<string> &wordSet,vector<string> &res,string &s,string &tmp,int start,vector<bool> &isOk){
if(start == s.size()){
res.push_back(tmp.substr(,tmp.size() - ));
}
for(int i = start;i < s.size();++i){
string sub = s.substr(start,i - start + );
if((isOk[i] == false) || wordSet.find(sub) == wordSet.end()){
continue;
} tmp.append(sub).append(" ");//只有每步满足条件之后才开始位置为该步的下一步
int oldSize = res.size();
helper(wordSet,res,s,tmp,i + ,isOk);
if(oldSize == res.size()){
isOk[i] = false;
}
tmp.erase(tmp.size() - sub.size() - );
}
}
vector<string> wordBreak(string s, vector<string>& wordDict) { if(s.size() == ){
return {};
}
if(wordDict.size() == ){
return {};
}
vector<string> res;
unordered_set<string> wordSet;
for(string tmp : wordDict){
wordSet.insert(tmp);
}
string tmp;
vector<bool> isOk(s.size(),true);//代表从i位置分割是否能得到一个结果
//isOk[0] = false;
helper(wordSet,res,s,tmp,,isOk);
return res;
}
};

139. Word Break 以及 140.Word Break II的更多相关文章

  1. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  2. 140. Word Break II(hard)

    欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...

  3. [LeetCode] 140. Word Break II 单词拆分II

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

  4. LeetCode笔记:140. Word Break II

    题目描述 给定一个非空的字符串s,一个非空的字符串list作为字典.通过在s中添加空格可以将s变为由list中的word表示的句子,要求返回所有可能组成的句子.设定list中的word不重复,且每一个 ...

  5. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  6. C#操作Word (1)Word对象模型

    Word对象模型  (.Net Perspective) 本文主要针对在Visual Studio中使用C# 开发关于Word的应用程序 来源:Understandingthe Word Object ...

  7. Java实现 LeetCode 140 单词拆分 II(二)

    140. 单词拆分 II 给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中.返回所有这些可能的句子. 说明: 分 ...

  8. Java实现 LeetCode 140 单词拆分II

    class Solution { public List<String> wordBreak(String s, List<String> wordDict) { List&l ...

  9. C#中操作Word(1)—— word对象模型介绍

    一.开发环境布置 C#中添加对Word的支持,只需添加对Microsoft.Office.Interop.Word的命名空间,如下图所示,右键点击“引用”,在弹出的“添加引用”对话框中选中COM标签页 ...

随机推荐

  1. Go安装gRPC

    grpc-go的官方安装命令 go get google.golang.org/grpc 无法正常使用. 我们可以用以下的命令替代,达到同样的效果 git clone https://github.c ...

  2. IntelliJ IDEA常用快捷键大全

    如果想要非常高效的使用IDEA这款工具,应该掌握图中已被标记的快捷键. 另: 代码实时模板生成:psvm/sout/ifn等 按Tab键快速生成模板. 转载请保留或注明出处:http://www.cn ...

  3. Python基础-2 变量与常量

    变量与常量 变量:在程序运行过程中,值会发生变化的量 常量:在程序运行过程中,值不会发生变化的量 无论是变量还是常量,在创建时都会在内存中开辟一块空间,用于保存它的值. 这里有一点需要注意的是,在py ...

  4. 时间和日期实例-<Calender计算出生日期相差几天>

    String day1="1994:10:04"; String day2="1994:10:03"; SimpleDateFormat format= new ...

  5. android 简单列表对话框(AlertDialog.Builder().setItems())

    ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...

  6. json 常用的方法

    JSON 是用于存储和传输数据的格式. JSON 通常用于服务端向网页传递数据 . -------  菜鸟网 1.  JSON.parse() :用于将一个 JSON 字符串转换为 JavaScrip ...

  7. 捣鼓Haskell

    最近想学这门语言,于是做了一些准备工作,配置好一切后,打算玩一玩. 先扔一段官方简介: Introduction Haskell is a computer programming language. ...

  8. 「luogu4135」作诗

    「luogu4135」作诗 传送门 分块好题. 预处理出 \(f[i][j]\) 表示 \(i\) 号块到 \(j\) 号块的答案,\(num[i][k]\) 表示 \(k\) 在前 \(i\) 块的 ...

  9. 【JAVA随摘笔记一】进制转换

    // 十进制转其它进制(二进制,八进制,十六进制) ; System.out.println(Integer.toBinaryString(k));// 转二进制 10001 System.out.p ...

  10. 4 ehcache 配置

    拷贝ehcache.xml文件到工程的resources目录下面 <?xml version="1.0" encoding="UTF-8"?> &l ...