【leetcode】Word Break II
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"].
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的更多相关文章
- 【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 ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【LeetCode】Word Break 解题报告
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Word Ladder II
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...
- 【leetcode】Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Word Break(python)
思路是这种.我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到.那么就是False了. 在找到第一个后,接下来找下一个断句处,当然是从第一个断句 ...
- 【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 ...
- 【leetcode】Word Ladder II(hard)★ 图 回头看
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [Leetcode Week9]Word Break II
Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...
随机推荐
- linux标准输入输出及错误输出
Linux Shell 环境中支持输入输出重定向,用符号"<"和">"来表示. 0.1和2分别表示标准输入.标准输出和标准错误信息输出,可以用来指定 ...
- tamper参数
"tamper/apostrophemask.py","tamper/equaltolike.py","tamper/greatest.py" ...
- XML简介
xml的简介(了解) * eXtensible Markup Language:可扩展标记型语言 ** 标记型语言:html是标记型语言 ...
- span设为inline-block之后,未包含文字时下面会多出一条空白问题
1.问题的引出: 产品列表页面场景: 上面是产品图片[img], 中间是提示库存信息[span](始终存在,有库存则不显示文字,但元素占位.所以设置display:inline-block), 下面是 ...
- java 的 AccessController.doPrivileged使用
AccessController.doPrivileged意思是这个是特别的,不用做权限检查. 在什么地方会用到呢:加入1.jar中有类可以读取一个文件,现在我们要使用1.jar去做这个事情.但是我们 ...
- JSF页面中使用js函数回调后台bean方法并获取返回值的方法
由于primefaces在国内使用的并不是太多,因此,国内对jsf做系统.详细的介绍的资料很少,即使有一些资料,也仅仅是对国外资料的简单翻译或者是仅仅讲表面现象(皮毛而已),它们的语句甚至还是错误的, ...
- oracle 中的Ipad()函数
本文基于转载: lpad函数从左边对字符串使用指定的字符进行填充.lpad意思是从左边填充的意思. 语法格式如下: lpad( string, padded_length, [ pad_string ...
- matplotlib basic and boxplot
============================================matplotlib 绘图基础========================================= ...
- gspx请求周期(备忘)
- src 小心得
关于src的引用,不要用相对路径, ../ 虽然省事,但是跳转页面时容易出错. 举个例子: 在web页面引用 D:\phpStudy\WWW\ueditor\utf8-php 这个文件夹下面 ...