由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 帮俺看一下代码DP+DFS为什么过不了work break II 那个大case : aaaaaaa...
相关主题
leetcode的新题是1337c0d3r本人在更新吗?leetcode 3sum c++解法超时
leetcode word break II DFS 超时lc 上面4 sum的时间复杂度要求多少?
Leetcode Word Break I 有o(n^2)的算法吗?leetcode出了新题word ladder
请教leetcode上的那道Word Break II,多谢!请教一道G题的代码量
转划单词题的优解这种backtracking的问题怎么算时间复杂度?比如palindrom patitioning.
wordBreak问题,有非递归的方法么Word Search large case TLE
Groupon新鲜面经leetcode Palindrome Partitioning
贡献一道题网盘电面一道
相关话题的讨论汇总
话题: col话题: dfs话题: int话题: string话题: vector
进入JobHunting版参与讨论
1 (共1页)
r*******n
发帖数: 3020
1
vector wordBreak(string s, unordered_set &dict) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
vector result;
int n = s.size();
vector> table(n, vector(n,false));
int max_len=0;
for(auto& word:dict){
if(max_len max_len=word.size();
}
//DP
for(int i=0; i for(int j=i;j if(dict.find(s.substr(i,j-i+1))!=dict.end()){
table[i][j]=true;
}
}
}

//Backtracking using DFS
function dfs;
dfs=[&](int col, string path){
if(col==n){
result.push_back(path);
return;
}
for(int k=col; k if(table[col][k]){
dfs(k+1, path+" "+s.substr(col, k-col+1));
}
}
};
dfs(0, "");
return result;
}
b*******e
发帖数: 123
2
My approach is very similar to yours. the difference is the meaning of DP
table. My DP table is 1-D bool, it tracks whether there exist a word break
from i to end. When you do DFS, you can skip the index that doesn't have a
solution when going forward.
r*******n
发帖数: 3020
3
多谢,改了后过了OJ。
如你所说,我加了1-D bool table来记录有没有解
后面DFS根据这个如果没有解就停止递归搜索
把整个code贴下面
vector wordBreak(string s, unordered_set &dict) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
vector result;
int n = s.size();
vector> table(n, vector(n,false));
int max_len=0;
for(auto& word:dict){
if(max_len max_len=word.size();
}
vector has_solution(n+1,false);
has_solution[n]=true;
for(int j=n-1; j>=0; --j){
for(int i=j; i>=0; --i){
if(dict.find(s.substr(i,j-i+1))!=dict.end()&&has_solution[j+
1]){
has_solution[i]=true;
}
}
}
//DP
for(int i=0; i for(int j=i;j if(dict.find(s.substr(i,j-i+1))!=dict.end()){
table[i][j]=true;
}
}
}

//Backtracking using DFS
function dfs;
dfs=[&](int col, string path){
if(col==n){
result.push_back(path.substr(1));
return;
}
for(int k=col; k if(table[col][k]&&has_solution[k+1]){
dfs(k+1, path+" "+s.substr(col, k-col+1));
}
}
};
dfs(0, "");
return result;
}

【在 b*******e 的大作中提到】
: My approach is very similar to yours. the difference is the meaning of DP
: table. My DP table is 1-D bool, it tracks whether there exist a word break
: from i to end. When you do DFS, you can skip the index that doesn't have a
: solution when going forward.

1 (共1页)
进入JobHunting版参与讨论
相关主题
网盘电面一道转划单词题的优解
帮忙看道题:[leetcode] word breakwordBreak问题,有非递归的方法么
走迷宫的 时间复杂度是多少?谢谢Groupon新鲜面经
word break 2的时间复杂度是多少 这个解法贡献一道题
leetcode的新题是1337c0d3r本人在更新吗?leetcode 3sum c++解法超时
leetcode word break II DFS 超时lc 上面4 sum的时间复杂度要求多少?
Leetcode Word Break I 有o(n^2)的算法吗?leetcode出了新题word ladder
请教leetcode上的那道Word Break II,多谢!请教一道G题的代码量
相关话题的讨论汇总
话题: col话题: dfs话题: int话题: string话题: vector