Leetcode_word-break-ii
来源:互联网 发布:腾讯内部软件 编辑:程序博客网 时间:2024/06/08 14:12
地址:http://oj.leetcode.com/problems/word-break-ii/
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"]
.
1. solution里的vector<string>里肯定没有逗号的,只要把string push 到vector里就好,逗号只是leecode的一种表示方式。因为笔者经常WA,表示wa的case时候,如果是一些树啊链表啊等复杂的数据结构时都是通过数组来意思一下的。
2. dfs确定解法一般都有个回溯的过程,可以计算好要pop()的字符个数,又因为涉及到空格,所以较麻烦,所以用copystr代替,同一个dfs中每次又dfs搜索开始都用同一个copystr。
参考代码, 60ms
class Solution {public: void dfs(vector<string>&res, string tmpstr, map<char, vector<string>>&dm, string s) { if(s.empty()) { res.push_back(tmpstr); return; } auto it = dm.find(s[0]); if(it == dm.end()) return; int len; string copystr; for(auto str_it = it->second.begin(); str_it != it->second.end(); ++str_it) { len = (*str_it).length(); copystr = tmpstr; if(len<=s.length() && *str_it==s.substr(0,len)) { if(copystr.empty()) copystr+=*str_it; else { copystr+=' '; copystr+=*str_it; } dfs(res, copystr, dm, s.substr(len)); } } } vector<string> wordBreak(string s, unordered_set<string> &dict) { vector<string>res; if(dict.empty() || s.empty()) return res; vector<bool>dp(s.length()+1, false); dp[0]=true; for(int i = 0; i<s.length(); ++i) { for(int j = 1; i+j<=s.length(); ++j) { if(dp[i] && dict.find(s.substr(i,j)) != dict.end()) dp[i+j] = true; } } if(!dp[s.length()]) return res; map<char, vector<string>>dm; for(auto it = dict.begin(); it!=dict.end(); ++it) dm[(*it)[0]].push_back(*it); dfs(res, "", dm, s); return res; }};
Second trail
class Solution {private: void dfs(vector<string>&ans, vector<string>&vec, string s, unordered_set<string> &dict) { if(s.empty()) { string str = vec[0]; for(int i = 1; i<vec.size(); ++i) { str += " "; str += vec[i]; } ans.push_back(str); } string word; for(int i = 1; i<=s.length(); ++i) { word = s.substr(0, i); if(dict.find(word)!=dict.end()) { vec.push_back(word); dfs(ans, vec, s.substr(i), dict); vec.pop_back(); } } }public: vector<string> wordBreak(string s, unordered_set<string> &dict) { vector<string>ans, vec; if(s.empty() || dict.empty()) return ans; vector<bool>dp(s.length()+1, false); dp[0] = true; int len; for(int i = 0; i<s.length(); ++i){ for(auto it = dict.begin(); it != dict.end(); ++it) { len = (*it).length(); if(dp[i] && i+len<=s.length() && s.substr(i, len)==*it) dp[i+len] = true; } if(dp[s.length()]) break; } if(dp[s.length()]) dfs(ans, vec, s, dict); return ans; }};
0 0
- Leetcode_word-break-ii
- LeetCode_Word Break II
- LeetCode_Word Break
- leetcode_Word Break
- leetCode_Word Break
- Leetcode_word-ladder-ii
- Leetcode_word-break(c++ and python version)
- Word Break && Word Break II
- Word Break && Word Break II
- [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
- java适配器模式1
- JDK环境变量
- 高效开发Android App的10个建议
- Flvplayer.swf 代码引用方法:
- java适配器模式2
- Leetcode_word-break-ii
- 我觉得对于我这样一个大学2年半换了4个手机的人,是最有资格来进行评价下了。
- 刚装上的centos装不上客户端怎么办?
- TWaver HTML5学习笔记 —— 可定制的右键菜单
- AC自动机加强版 uva 1449 - Dominating Patterns
- 一些VC的快捷键以及调试技巧
- 程序员的修养及我的体会
- BNUOJ--29064 硬币水题II
- java原型模式