leetcode-Word Break II
来源:互联网 发布:石破天 武功 知乎 编辑:程序博客网 时间:2024/06/04 18:07
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"]
.
题意:有字符串s和字典dict,将s分割成由字典中出现的若干单词,用空格分开,求所有分割方法。
分析:首先,建立从s从头到尾的可行路径,即dp[i]为从i出发下一个可行位置列表。
为了建立dp[i],要从尾向头找单词,即从n-1到0枚举单词结尾位置j,每个位置找所有可行单词的起始位置i,dp[i].push_back(j)
这样反向建立路径,正向遍历时就一定是可行路径了
注意:若正向建立,那么会有很多路径不能到达终点,导致搜索空间变大,我就是因为这个问题TLE很多次
然后,用dfs或bfs从头搜索,记录结果即可。
代码:
class Solution {public: vector<string> wordBreak(string s, unordered_set<string> &dict) { vector<string> ans; if(dict.empty()) return ans; if(s.empty()) { ans.push_back(s); return ans; } int len = s.length(); vector<int> tmp; vector<vector<int> > dp(len,tmp); for(int i=len-1; i>=0; i--) { if(i<len-1 && dp[i+1].size()==0) continue; for(int j=i; j>=0; j--) { string cur = s.substr(j,i-j+1); if(dict.find(cur)!=dict.end()) dp[j].push_back(i); } } stack<int> st; stack<string> sts; st.push(0); sts.push(""); while(!st.empty()) { int ind = st.top(); string pre = sts.top(); st.pop(),sts.pop(); for(int i=0; i<dp[ind].size(); i++) { int j = dp[ind][i]; string now = s.substr(ind,j-ind+1); string add = pre; if(add.length()>0) add += ' '; add += now; if(j+1>=len) { ans.push_back(add); } else { st.push(j+1); sts.push(add); } } } return ans; }};
0 0
- [leetcode]Word Break II
- LeetCode:Word Break II
- Leetcode: Word Break II
- [LeetCode] Word Break II
- [LeetCode]Word Break II
- LeetCode | Word Break II
- [LeetCode] - Word Break II
- LeetCode - Word Break II
- Leetcode Word Break II
- [LeetCode] Word Break II
- Word Break II -- LeetCode
- LeetCode (Word Break II )
- Leetcode--Word Break II
- Leetcode: Word Break II
- LeetCode Word Break II
- Word Break II leetcode
- [leetcode]Word Break II
- leetcode -- word break II
- 回调函数
- OpenGL: Alpha融合和图片透明度[AlphaBlend]
- 性能调优攻略
- 黑马程序员 用OC实现的一个计算文件夹下代码行数的小程序
- mysql导入导出
- leetcode-Word Break II
- 2014 Multi-University Training Contest 1小记
- Hdu 1002 A + B Problem II (高精度相加)
- 【Linux内存管理】深入x86的内存寻址
- AJAX 跨域请求 - JSONP获取JSON数据
- Java集合源码学习(17)_BlockingQueue接口的实现LinkedBlockingQueue
- osx10.9.4安装php开发环境(nginx1.6.0+mysql5.6.19+php5.5.14)
- hdu4870 Rating---2014 Multi-University Training Contest 1
- 【Linux内存管理】深入x86的内存寻址之I/O 外设硬件开发举例