前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >**LeetCode—Word Break

**LeetCode—Word Break

作者头像
bear_fish
发布2018-09-20 15:57:12
5810
发布2018-09-20 15:57:12
举报

http://blog.csdn.net/xietingcandice/article/details/43705383

Given a string s and a dictionary of words dict, determine ifs 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".

在这个例子当中,主要是为了看一个句子字符串中的词语能不能够都划分为词典中的词语

flag[j]为true,那么就是要求存在一个变量k,能够满足flag[k]为ture,同时substr(k,j-k)是在字典当中的

但是在这个例子当中是不关心最终的划分方式

[cpp] view plaincopyprint?

  1. class Solution {    
  2. public:    
  3. bool wordBreak(string s, unordered_set<string> &dict) {    
  4. int length = s.size();  
  5. vector<bool> val(length+1,false);  
  6. val[0] = true; //<根据长度进行设置
  7. int i = 0;  
  8. int j = 0;  
  9. for(i = 0; i < length; i++)  
  10. {  
  11. if(val[i])//<前一个长度是可以进行分解的
  12.     {  
  13. for(j = 1; (i+j) <= length; j++)  
  14.         {  
  15.             string tmp = s.substr(i,j);  
  16. if(dict.count(tmp) > 0)  
  17.             {  
  18.                 val[i+j] = true;  
  19.             }  
  20.         }  
  21.     }  
  22. }  
  23. return val[length];  
  24.        }    
  25.    };    

对应着有第二个例子:

Word Break II

Given a string s and a dictionary of words dict, add spaces ins 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"].

在这个例子当中,主要是需要将分解的结果放在一个vector的结构体当中

有一种方法是DFS 方法,还没有研究

这里主要还是借鉴了第一个例子当中的方法,先检测是否能够被划分,如果可以被划分,之后再通过遍历进行存储

[cpp] view plaincopyprint?

  1. class Solution {  
  2. public:  
  3. void breakWord(vector<string> &res, string &s, unordered_set<string> &dict, string str, int idx, vector<bool> &dp) {  
  4.         string substr;  
  5. for (int len = 1; idx + len <= s.length(); ++len) {  
  6. if (dp[idx + len] && dict.count(s.substr(idx,len)) > 0) {  
  7.                 substr = s.substr(idx, len);  
  8. if (idx + len < s.length()) {  
  9.                     breakWord(res, s, dict, str + substr + " ", idx + len, dp);  
  10.                 } else {  
  11.                     res.push_back(str + substr);  
  12. return;  
  13.                 }  
  14.             }  
  15.         }  
  16.     }  
  17.     vector<string> wordBreak(string s, unordered_set<string> &dict) {  
  18.         vector<bool> dp(s.length() + 1, false);  
  19.         dp[0] = true;  
  20. for (int i = 0; i < s.length(); ++i) {  
  21. if (dp[i]) {  
  22. for (int len = 1; i + len <= s.length(); ++len) {  
  23. if (dict.count(s.substr(i, len)) > 0) {  
  24.                         dp[i + len] = true;  
  25.                     }  
  26.                 }  
  27.             }  
  28.         }  
  29.         vector<string> res;  
  30. if (dp[s.length()])   
  31.             breakWord(res, s, dict, "", 0, dp);  
  32. return res;  
  33.     }  
  34. };  
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年02月10日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Word Break II
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档