leetcode Word Break I II 算法分析
来源:互联网 发布:全民枪战刷钻石软件 编辑:程序博客网 时间:2024/05/17 03:14
今天就挑两个题,这两个题的解法多种多样。思想就两个dp和dfs。看来oj还是非常的喜欢这两个技术。可能面试的时候,大家都喜欢这类题目吧。
题目1:
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given
s = "leetcode"
,
dict = ["leet", "code"]
.
Return true because "leetcode"
can be segmented as "leet code"
.
这个题目比较一般,就是判断一个串是否能被分解成几个出现在词典中的词。这个dp的思想就很明显了。关键是子问题的想法,我写了三种,其实就是看子问题不同。
如:leetcode 可以看为l,le,lee,leet,leetc,leeco,leetcod是否能够被词典中的词表示,然后加入e,判断如果某个前缀可以表示如:leet可以被词典中的词表示,那么就可以判断code是否出现在词典中。(ie:如果lee能就判断tcode是否出现),这样就可以判断leetcode是否可以被词典中词表示了。
dp[i]=dp[j] && find(str(j,i)) 0=<j&&j<=i
class Solution {public: bool wordBreak(string s, unordered_set<string> &dict) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(find(dict.begin(),dict.end(),s)!=dict.end()) return true; int n=s.size(); bool dp[n]; for(int i=0;i<n;i++) dp[i]=false; for(int j=0;j<n;j++) { for(int i=j;i>=0;i--) { string t(s.begin()+i,s.begin()+j+1); if( find(dict.begin(),dict.end(),t)!=dict.end() ) { if( (i-1>=0 && dp[i-1])|| i==0) dp[j]=true; } } } return dp[n-1]; }};
既然可以从左往右看问题其实也可以从右往左看,思想完全相反就可以了。代码和上面的基本一致。
class Solution {public: bool wordBreak(string s, unordered_set<string> &dict) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(find(dict.begin(),dict.end(),s)!=dict.end()) return true; int n=s.size(); bool dp[n]; for(int i=0;i<n;i++) dp[i]=false; for(int j=n-1;j>=0;j--) { for(int i=j;i<n;i++) { string t(s.begin()+j,s.begin()+i+1); if( find(dict.begin(),dict.end(),t)!=dict.end() ) { if( ( i+1<n && dp[i+1])|| i==n-1) dp[j]=true; } } } return dp[0]; }};
其实我第一个想法是按照步长来判断,这种思想可以找出任意i,j之间的可以被词典表示的。题目当然不需要这么多信息,但是我们可以把题目扩展成判断任意子串是否能被词典表示,然后这种dp就可以用到了。
class Solution {public: bool wordBreak(string s, unordered_set<string> &dict) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(find(dict.begin(),dict.end(),s)!=dict.end()) return true; int n=s.size(); bool dp[n][n]; for(int i=0;i<n;i++) for(int j=0;j<n;j++) dp[i][j]=false; for(int k=0;k<n;k++) { for(int i=0;i<n-k;i++) { for(int j=i;j<=i+k;j++) { if(dp[i][j]&&dp[j+1][i+k]) { dp[i][i+k]=true; break; } string t(s.begin()+i,s.begin()+i+k+1); if(find(dict.begin(),dict.end(),t)!=dict.end()) { dp[i][i+k]=true; } } } } return dp[0][n-1]; }};
题目2:
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"]
.
第一次直接dfs,然后判断是否出现在词典中,超时。。、虽然用的是unordered_set但是多次的递归,特别是出现aaaaaaaaaaaaaa很多重复的时候,而a出现在词典中时,会很慢。
我们现在只能用一些信息来控制过多的递归。当深搜的时候,如果右边的串不能被表示,那么就可以不继续递归,从而避免了过多的不能表示而出现的递归。这里需要的就是搜索到i时,看一下i+1到n-1是否可以被词典表示,如果不能被表示就直接返回。这里需要的信息就是第一题那个从右往左的dp。表示从i到n-1是否能被词典中的词表示。做完一看这个题的ac率只有12%,看来好多人都因为这个卡了很久。还是那句,虽然思想很简单,但是不容易写对,因为要处理一些细节问题。
class Solution {public: vector<string> ss; bool dp[1000]; vector<string> wordBreak(string s, unordered_set<string> &dict) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int n=s.size(); ss.clear(); for(int i=0;i<n;i++) dp[i]=false; for(int j=n-1;j>=0;j--) { for(int i=j;i<n;i++) { string t(s.begin()+j,s.begin()+i+1); if( find(dict.begin(),dict.end(),t)!=dict.end() ) { if( ( i+1<n && dp[i+1])|| i==n-1) dp[j]=true; } } } dfs(0,n,s,string (""),dict); return ss; } void dfs(int st,int n,string str,string cur,unordered_set<string> &dict) { if(st==n) { ss.push_back(cur); return ; } for(int i=st;i<n;i++) { string t(str.begin()+st,str.begin()+i+1); if((dp[i+1]||i+1==n) && (find(dict.begin(),dict.end(),t)!=dict.end() )) { int c=cur.size(); cur+=t; if(i<n-1) cur.push_back(' '); dfs(i+1,n,str,cur,dict); cur.resize(c); } } }};
代码还算好懂吧,就是改了几次思路。还是不够迅速的找到问题关键。
解题思路都是自己思考的过程,肯定会出现没有考虑,或者没有想法的更好的方法。还是希望大家指正出错的地方。
- leetcode Word Break I II 算法分析
- leetCode: Word Break I & Word Break II
- 【LeetCode】word break I && II
- leetcode word break I && II
- LeetCode题解:Word Break I and II
- leetcode day7 -- Word Break I II
- leetcode -- Word Break I & II -- 重点
- Leetcode 139. Word Break I&&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
- [LeetCode] Word Break II
- IE调试工具
- [LeetCode] Word Search
- 硬链接与符号链接
- Ubuntu 下 配置 jdk1.7
- Cloudera Impala 编译、安装与配置
- leetcode Word Break I II 算法分析
- 中缀变后缀(nyoj)
- 分享一个自定义的View--SimpleClock
- 两台主机可以无需密码而直接互相登录的SSH配置方法
- IE调试工具2
- 大话存储笔记——1~2章
- 浅谈android的selector,背景选择器
- 该月有几天
- 一些机器视觉网站