LintCode Word Break II
来源:互联网 发布:arcgis js 轨迹运动 编辑:程序博客网 时间:2024/05/10 13:52
题目
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.
Gieve s = lintcode
,
dict = ["de", "ding", "co", "code", "lint"]
.
A solution is ["lint code", "lint co de"]
.
解法
之前做了一道单词切分的题目,直接深度优先搜索,超时后改成非递归形式的就过了,所以认为这道题就是考深度优先搜索的。面对这个加强版的题也如法炮制,发现非递归形式也会超时,上网查了下才知道这题的核心是一个判断字符串是否能被划分的dp算法。
直接深度优先太慢,所以要先判断一下排除掉不可能划分的情况。dp[i] 表示从 i 开始到最后的子字符串可以被切分, len 表示字符串的长度。则有 dp[len] = true,当从 i 到 j 的字符串在字典中且 dp[j + 1] = true 时 dp[i] = true。有了这个表再进行深度优先搜索找出所有可能的划分,不可能划分的就不用算了。
vector<string> wordBreak(string s, unordered_set<string> &wordDict) { // Write your code here vector<bool> dp(s.size() + 1, true); string dps; for(int i = s.size() - 1; i >= 0; --i) { dps.clear(); dp[i] = false; for(int j = i; j < s.size(); ++j) { dps.append(1, s[j]); if(dp[j + 1] && wordDict.find(dps) != wordDict.end()) { dp[i] = true; break; } } } vector<string> r; vector<string> buf; sub(s, wordDict, 0, buf, r, dp); return r; } void sub(string& s, unordered_set<string> &dict, int cur, vector<string>& buf, vector<string>& r, vector<bool>& dp) { string t; if(cur >= s.size()) { t = buf[0]; for(int i = 1; i < buf.size(); ++i) { if(i > 0) t.append(1, ' '); t.append(buf[i]); } r.push_back(t); return; } if(!dp[cur]) return; for(int i = cur; i < s.size(); ++i) { t.append(1, s[i]); if(dict.find(t) != dict.end()) { buf.push_back(t); sub(s, dict, i + 1, buf, r, dp); buf.pop_back(); } } }
阅读全文
0 0
- LintCode Word Break II
- Word Break && Word Break II
- Word Break && Word Break II
- lintcode:Word Break
- LintCode: Word Break
- lintcode:Word Break
- Word Break--lintcode
- [leetcode]Word Break II
- Word Break II
- Word Break II
- LeetCode:Word Break II
- Word Break II
- Leetcode: Word Break II
- [LeetCode] Word Break II
- [LeetCode]Word Break II
- LeetCode | Word Break II
- Word Break II
- Word Break II
- 删除db重复记录
- S3C6410启动方式
- 【润乾报表5】HTML事件--获取单元格值
- 长沙理工大学第12届ACM大赛 G-跑路ing(概率DP)
- leetcode10. Regular Expression Matching
- LintCode Word Break II
- 安卓开发类似QQ、微信优化APP启动速度,记录返回桌面前的任务栈状态
- Linux删除(清空)正在运行的应用日志文件内容
- sqoop数据导入
- UNICODE环境下CString转char*
- 如何实现JDBC连接mysql数据库
- Hibernate注解配置与XML配置区别
- fastText Ngram 的处理过程
- 第六节:SpringBoot之事务管理@Transactional