问题描述(139):
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.
Note:
- The same word in the dictionary may be reused multiple times in the segmentation.
- You may assume the dictionary does not contain duplicate words.
Example 1:
Input: s = "leetcode", wordDict = ["leet", "code"] Output: true Explanation: Return true because"leetcode"can be segmented as"leet code".
Example 2:
Input: s = "applepenapple", wordDict = ["apple", "pen"] Output: true Explanation: Return true because"applepenapple"can be segmented as"apple pen apple". Note that you are allowed to reuse a dictionary word.
Example 3:
Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false
源码(139):
看到这一题我的第一想法是和leetcode【131】Palindrome Partitioning【c++版,时间99%,空间100%】一样,用字串深搜。还想着用hash来判断元素是否存在,于是喀喀喀写出来。发现——超时了(我抑郁了)。

class Solution {
public:
string str;
unordered_map<string, bool> has;
bool word(int start){
// int n = wordDict.size();
int len = str.length();
if(start == len) return true;
for(int i=start; i<len; i++){
string tmp = str.substr(start, i-start+1);
// cout<<tmp<<" "<<i<<endl;
if(!has.count(tmp)) continue;
// cout<<tmp<<" "<<i<<endl;
if(word(i+1)) return true;
}
return false;
}
bool wordBreak(string s, vector<string>& wordDict) {
str = s;
for(int i=0; i<wordDict.size(); i++){
has[wordDict[i]] = true;
}
return word(0);
}
};
后来看了看Discuss区,觉得没必要每个在循环体的时候查找for(int i=start; i<len; i++)。可以根据wordDict查找。而且这题没有要求出最优解,只需要最优值,动态规划可能更合适。
dp[i]就表示s的第i个元素能否被表达。
dp[i] = dp[i-x] && s.substr[i-x, x]
时间96%,空间100%。

class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
int n=s.length(), m=wordDict.size();
vector<bool> dp(n+1);
dp[0] = true;
for(int i=1; i<=n; i++){
for(int j=0; j<m; j++){
int tmp = wordDict[j].length();
if(tmp<=i && s.substr(i-tmp, tmp)==wordDict[j]){
dp[i] = dp[i-tmp];
}
if(dp[i]) break;
}
}
return dp[n];
}
};
问题描述(140):
源码(140):
想想这题需要求最优解,而不是最优值,应该是要用DFS了,于是喀喀喀,写了一下,在同一个用例上超时了。【笑脸】

class Solution {
public:
string str;
void word(int start, string path, vector<string>& result, vector<string>& wordDict){
int n = str.length();
int m = wordDict.size();
if(start == n){
result.push_back(path.erase(0, 1));
return;
}
for(int i=0; i<m; i++){
string tmp = wordDict[i];
if(tmp == str.substr(start, tmp.length())){
word(start+tmp.length(), path+" "+tmp, result, wordDict);
}
}
}
vector<string> wordBreak(string s, vector<string>& wordDict) {
str = s;
vector<string> result;
word(0, "", result, wordDict);
return result;
}
};
没办法,google了一下,看到了一种回溯法,他最主要的剪枝就在于用一个hash存了默写特定字符串的hash值,不用再重复计算。时间48%,空间93%
class Solution {
public:
unordered_map<string, vector<string>> hash;
vector<string> wordBreak(string s, vector<string>& wordDict) {
if(hash.count(s)) return hash[s];
if(s.empty()) return {""};
vector<string> result;
for(string item: wordDict){
if(item == s.substr(0, item.length())){
vector<string> tmp = wordBreak(s.substr(item.length()), wordDict);
for(string tmp_item: tmp) result.push_back(item + (tmp_item.empty() ? "":" ") + tmp_item);
}
}
return hash[s] = result;
}
};
在solution区看到一个动态规划法,但是这个可能是以前的,现在这样用c++编出来已经不能满足时间限制了,而且还是那个用例。
class Solution {
public:
vector<string> wordBreak(string s, vector<string>& wordDict) {
int n = s.length();
unordered_set<string> word(wordDict.begin(), wordDict.end());
vector<vector<string>> dp(n+1, vector<string>(0));
vector<string> initial;
initial.push_back("");
dp[0] = initial;
for(int i=1; i<=n; i++){
for(int j=0; j<i; j++){
string str = s.substr(j, i-j);
if(dp[j].size()>0 && word.count(str)){
for(string tmp: dp[j]){
dp[i].push_back(tmp + (tmp.empty()?"" : " ") + str);
}
}
}
}
return dp[n];
}
};
在google上还翻到了一种结合了动态规划和DFS的方法,先用dp[i]存着所有的j,s的j到i的子串是在wordDict中存在的。然后再遍历,省了一点时间。但是效果也不理想,我猜可能是最近Leetcode服务器的问题,discuss区的方法,要吗跑不通,要么效率低,自己solution的方法也是。
class Solution {
public:
vector<string> result;
void helper(vector<vector<int>>& dp, string s, int i, string path){
if(i<0){
result.push_back(path);
return;
}
for(int item: dp[i]){
string nextpath = path.length()==0? s.substr(item, i-item+1) : s.substr(item, i-item+1)+" "+path;
helper(dp, s, item-1, nextpath);
}
}
vector<string> wordBreak(string s, vector<string>& wordDict) {
int n = s.length();
unordered_set<string> word(wordDict.begin(), wordDict.end());
vector<vector<int>> dp(n+1, vector<int>(0));
for(int i=0; i<n; i++){
for(int j=0; j<=i; j++){
if(word.count(s.substr(j, i-j+1)) && (j==0 || dp[j-1].size()>0))
dp[i].push_back(j);
}
}
helper(dp, s, n-1, "");
return result;
}
};
1251




被折叠的 条评论
为什么被折叠?



