【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 ...
随机推荐
- phpcms v9 0day
index.php?m=member&c=index&a=login 后缀 username=phpcms&password=123456%26username%3d%2527 ...
- RabbitMQ service is already present - only updating service parameters
如果你安装RabbitMQ不是那么一番顺利..那么你有可能会重装多次.. So..问题来了..重装时你执行 rabbitmq-service install 的时候..有可能就会报这个错了.. ...
- mysql配置mysql-proxy读写分离
MySQL配置读写分离 192.168.23.131与192.168.23.132两台服务器,131是主,132是从,131是读写,132是只读.myql-proxy的IP是192.168.23.13 ...
- 巧用svn create patch(打补丁)方案解决定制版需求
最近项目定制版越来越多,维护,同步代码非常费事.以前的思路如下图: 以前的svn目录结构如下图: 这样问题有2个: 若在一个定制包中修复了其他定制包也有的bug,同步更新其他包的代码时,非常费劲+机械 ...
- jquery的$.extend、$.fn.extend、 jQuery.extend( target, object1, [objectN])作用及区别
jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(); jQuery.extend(); 虽然 javascript 没有明确的类的概念,但是用类来理解它,会更方便. ...
- 管理员必备的Linux系统监控工具
管理员必备的Linux系统监控工具 #1: top - 进程活动 top提供一个当前运行系统实时动态的视图, 也就是正在运行进程.在默认情况下,显示系统 中CPU使用率最高的任务,并每5秒钟刷新一次. ...
- Lnmp的安装、配置
一.首先在本地安装好虚拟机,在虚拟机上安装centos6.5,由于习惯问题,不喜欢直接在虚拟机上操作linux系统,习惯了ssh过去,直接用xshell操作,这完全是个人习惯问题: 1. 用xshe ...
- 手机注册获取验证码的PHP代码
php代码 <?php require dirname(__FILE__).'/include/common.inc.php';//这是在cms2008下面做的测试 header("c ...
- MySql的安装及配置详细指引!
一.安装My Sql数据库 1.1,首先下载MySQL与HeidiSQL工具,双击打开后可以看到名为”mysql-5.0.22-win32 Setup.exe”的安装程序,双击执行该程序. 1.2,打 ...
- UI第三节——UIView详解
- (void)viewDidLoad { [super viewDidLoad]; UIView *redView = [[UIView alloc] initWithFrame:CGRectMak ...