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"].

 
与Word Break I类似,只不过需要把所有情况都表示出来。
首先利用Word Break I中的动态规划
 
dp[i]=true 代表了0,1,...,i-1可以用字典中的元素分割
同时新建一个map<int,vector<int>> slice,用于记录分割的位置
如slice[i]=j代表了0,1,...,i-1可以分割为0,1,j-1和j,j+1,...,i-1
 
利用这个slice,利用dfs便可以找到所有的结果
  
 
 class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string> &dict) { vector<bool> dp(s.length()+);
dp[]=true;
map<int,vector<int> > slice; // dp[i] 0,1,2...i-1可以被分割
// hash[i]=j 表示0,1,2...i-1可以分割为0,1,2,...,j-1和j,j+1,...,i
for(int i=;i<s.length()+;i++)
{
for(int j=;j<i;j++)
{
if(dp[j]&&dict.count(s.substr(j,i-j)))
{
dp[i]=true; if(slice.find(i)!=slice.end()) slice[i].push_back(j);
else slice[i]=vector<int>(,j);
}
}
} vector<string> result; dfs(result,slice,s.length(),s,s);
return result;
} void dfs(vector<string> &result,map<int,vector<int>> &slice,int start,string &s,string cur)
{
if(start==)
{
cur.erase(,);
result.push_back(cur);
return;
} vector<int> sliceV=slice[start];
for(int i=;i<sliceV.size();i++)
{
string tmp=cur;
cur.insert(sliceV[i]," ");
dfs(result,slice,sliceV[i],s,cur);
cur=tmp;
}
} };
 

【leetcode】Word Break II的更多相关文章

  1. 【leetcode】Word Break II (hard)★

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  2. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  3. 【LeetCode】Word Break 解题报告

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  4. 【leetcode】Word Ladder II

      Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...

  5. 【leetcode】Word Break

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  6. 【leetcode】Word Break(python)

    思路是这种.我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到.那么就是False了. 在找到第一个后,接下来找下一个断句处,当然是从第一个断句 ...

  7. 【leetcode】Word Search II(hard)★

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. 【leetcode】Word Ladder II(hard)★ 图 回头看

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  9. [Leetcode Week9]Word Break II

    Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...

随机推荐

  1. struts2 拦截器和actioninvocation、PreResultListener

       Interceptor说明 Interceptor的接口定义没有什么特别的地方,除了init和destory方法以外,intercept方法是实现整个拦截器机制的核心方法.而它所依赖的参数Act ...

  2. webstorm 文件历史找回~ 恢复正确状态~

    事情的经过时这样的~  我写好的HTML 我新下载了sublime text3 用这个打开了下 结果都变乱码了~ 大概截个图 都恢复了 就不瞎搞了 webstorm有错误的记录都有真好~ 恢复的具体操 ...

  3. Linux查看软件安装路径

    Linux中查看某 个软件的安装路径(地址)有时显得非常重要.比如某个文件的快速启动项被删除,或者你要建立快速启动项,或者想删除. 添加安装文件等等,很多地方都要用到查案文件安装路径的命令. 这里给大 ...

  4. Python开发【第三篇】:Python基本数据类型

    运算符 1.算数运算: 2.比较运算: 3.赋值运算: 4.逻辑运算: 5.成员运算: 基本数据类型 1.数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31- ...

  5. yii2 数据库操作(转)

    开始使用数据库首先需要配置数据库连接组件,通过添加 db 组件到应用配置实现("基础的" Web 应用是 config/web.php),DSN( Data Source Name ...

  6. Task异常处理

    http://www.cnblogs.com/xray2005/archive/2011/08/24/2151459.html

  7. fedora23忘记root密码怎么办??

    fedora23使用的是uefi, 不是 传统的grub 所以在编辑grub的时候, 跟以前的版本略有不同 最最重要的是: 在编辑启动条目的时候, 那个 linuxefi ... vmlinuz... ...

  8. Spring与Quartz的整合实现定时任务调度(转)

    源:http://kevin19900306.iteye.com/blog/1397744 最近在研究Spring中的定时任务功能,最好的办法当然是使用Quartz来实现.对于一个新手来说,花了我不少 ...

  9. mybatis 简单使用示例(单独使用):

    mybatis的单独使用简单示例: 步骤1: 新建xml文件. 示例: <?xml version="1.0" encoding="UTF-8" ?> ...

  10. spring学习

    http://blog.csdn.net/chjttony/article/details/6301523 http://blog.csdn.net/chjttony/article/details/ ...