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. linux标准输入输出及错误输出

    Linux Shell 环境中支持输入输出重定向,用符号"<"和">"来表示. 0.1和2分别表示标准输入.标准输出和标准错误信息输出,可以用来指定 ...

  2. tamper参数

    "tamper/apostrophemask.py","tamper/equaltolike.py","tamper/greatest.py" ...

  3. XML简介

    xml的简介(了解) * eXtensible Markup Language:可扩展标记型语言                       ** 标记型语言:html是标记型语言          ...

  4. span设为inline-block之后,未包含文字时下面会多出一条空白问题

    1.问题的引出: 产品列表页面场景: 上面是产品图片[img], 中间是提示库存信息[span](始终存在,有库存则不显示文字,但元素占位.所以设置display:inline-block), 下面是 ...

  5. java 的 AccessController.doPrivileged使用

    AccessController.doPrivileged意思是这个是特别的,不用做权限检查. 在什么地方会用到呢:加入1.jar中有类可以读取一个文件,现在我们要使用1.jar去做这个事情.但是我们 ...

  6. JSF页面中使用js函数回调后台bean方法并获取返回值的方法

    由于primefaces在国内使用的并不是太多,因此,国内对jsf做系统.详细的介绍的资料很少,即使有一些资料,也仅仅是对国外资料的简单翻译或者是仅仅讲表面现象(皮毛而已),它们的语句甚至还是错误的, ...

  7. oracle 中的Ipad()函数

    本文基于转载: lpad函数从左边对字符串使用指定的字符进行填充.lpad意思是从左边填充的意思. 语法格式如下: lpad( string, padded_length, [ pad_string ...

  8. matplotlib basic and boxplot

    ============================================matplotlib 绘图基础========================================= ...

  9. gspx请求周期(备忘)

  10. src 小心得

    关于src的引用,不要用相对路径,  ../   虽然省事,但是跳转页面时容易出错. 举个例子: 在web页面引用  D:\phpStudy\WWW\ueditor\utf8-php 这个文件夹下面  ...