LeetCode -- WordBreak II
2015-09-01 15:04
477 查看
题目描述:
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个字符串s,和一个字典(字符串数组),对s进行分词,求出所有可能情况。
从题目本身来看,本题的解法是一个DFS,拿到s.substr[0...n],如果在dic中存在,则拿到当前index对s.substr[index...n]递归执行,如果s在dic中找到,添加到结果集。 由于没有剪枝,存在很多不必要的递归。
剪枝:可以使用长度为s.Length的数组来存放s在每一位是否存在可能解。在继续递归之前,把当前的解集存一下,递归之后,解集没有改变,说明没有继续执行的必要。
实现代码:
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个字符串s,和一个字典(字符串数组),对s进行分词,求出所有可能情况。
从题目本身来看,本题的解法是一个DFS,拿到s.substr[0...n],如果在dic中存在,则拿到当前index对s.substr[index...n]递归执行,如果s在dic中找到,添加到结果集。 由于没有剪枝,存在很多不必要的递归。
剪枝:可以使用长度为s.Length的数组来存放s在每一位是否存在可能解。在继续递归之前,把当前的解集存一下,递归之后,解集没有改变,说明没有继续执行的必要。
实现代码:
public IList<string> WordBreak(string s, ISet<string> wordDict) { var results = new List<string>(); var possible = new bool[s.Length]; for(var i = 0;i < possible.Length; i++){ possible[i] = true; } DfsWithCut(0,s, wordDict, "", results, possible); return results; } public void DfsWithCut(int start , string s, ISet<string> wordDic, string result, IList<string> results, bool[] possible){ var left = s.Substring(start , s.Length - start); if(wordDic.Any(x=> x == left)){ var r = result == "" ? left : result + " " + left; results.Add(r); } for(var i = start ;i < s.Length ; i++){ var w = s.Substring(start, i - start + 1); if(wordDic.Any(x=>x == w) && i < s.Length - 1){ if(possible[i+1] /*only if possible do recursion*/){ var r = result == "" ? w : result + " " + w; var before = results.Count; // track current results count before dfs DfsWithCut(i + 1, s , wordDic, r, results, possible); if(results.Count == before){ // since no new result found , mark possible[i+1] as false to cut it possible[i+1] = false; } } } } }
相关文章推荐
- idea快捷键
- hdu5416 树上XOR
- C++基础篇--运算符重载
- 技术文章
- Unreal Engine API Reference
- 软工视频第三章之需求分析
- 游戏服务器主程白皮书-3.1 运行稳定性
- MySql之慢查询
- Windows远程桌面连接的利器-mRemote
- Git+gerrit user guide
- Mongodb installation & userguide
- 会跳舞的树(只用HTML+CSS)
- 技术文章
- 技术文章
- 找不到ADO.NET Entity Data Model模板或 sql server database project模板
- Zabbix 2.4 for Ubuntu 14.04 LTS:
- leetCode #135 Candy
- Windows支持的4种类型的同步对象:临界区、互斥量、事件和信号量
- 代理模式(二):代理模式应用实例(收费商务信息查询系统)
- 线段覆盖长度