首页 技术 正文
技术 2022年11月8日
0 收藏 583 点赞 1,943 浏览 2260 个字

I

title:

https://leetcode.com/problems/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".

思路:一开始就考虑直接使用dfs超时了。。。

class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
if (s.size() == )
return true;
string tmp;
for (int i = ; i < s.size(); i++){
tmp.push_back(s[i]);
if (wordDict.find(tmp) != wordDict.end()){
string tmp1(s.begin()+i+,s.end());
if (wordBreak(tmp1,wordDict))
return true;
}
}
return false;
}
};

其实对于这样的问题,是可以想到是可以使用动态规划的。

一个DP问题。定义possible[i] 为S字符串上[0,i]的子串是否可以被segmented by dictionary.

那么

possible[i] = true      if  S[0,i]在dictionary里面

= true      if   possible[k] == true 并且 S[k+1,j]在dictionary里面, 0<k<i

= false      if    no such k exist.

class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
int len = s.size();
vector<bool> dp(len+,false);
dp[] = true;
for (int i = ; i < len+; i++){
for (int k = ; k < i; k++){
if (dp[k] && wordDict.find(s.substr(k,i-k)) != wordDict.end()){
dp[i] = true;
break;//break是因为题目只是需要判断是否存在
}
}
}
return dp[len];
}
};

II

https://leetcode.com/problems/word-break-ii/

title:

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”].

思路:使用dp,并且保存中间结果。注意,在I中找到一个解就直接break,这里需要记录下这个信息。另外prev[i][j]为true表示字串[j,i]在字典中

class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string>& wordDict) { int len = s.size();
vector<bool> dp(len+,false);
vector<vector<bool> > prev(len+,vector<bool>(len,false));
dp[] = true;
for (int i = ; i < len+; i++){
for (int k = ; k < i; k++){
if (dp[k] && wordDict.find(s.substr(k,i-k)) != wordDict.end()){
dp[i] = true;
prev[i][k] = true;
}
}
}
vector<string> result;
vector<string> results;
genPath(len,s,dp,prev,result,results);
return results;
} void genPath(int cur,string s, vector<bool> dp,vector<vector<bool> > prev, vector<string> &result,vector<string> &results){
if (cur == ){
string tmp;
for (int i = result.size()-; i > ; i--){
tmp += result[i] + " ";
}
tmp += result[];
results.push_back(tmp);
return ;
}
for (int i = ; i < s.size(); i++){
if (prev[cur][i]){
result.push_back(s.substr(i,cur-i));
genPath(i,s,dp,prev,result,results);
result.pop_back();
}
}
}
};
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297