【题目】

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

【题意】

给定一个字符串s和词典dict, 返回全部切分情况。使得切分后每一个单词都是dict中的单词

【思路】

依次确定以每一个位置i结尾的单词的前驱单词集合(仅仅要记住前驱单词的结束位置)

        然后从后往前恢复切分路径就可以。

DP问题

【代码】

class Solution {
public: void recoverPath(vector<string>&result, vector<string>&words, string&s, map<int, vector<int> >preorders, int end){
vector<int>preorder=preorders[end];
for(int i=0; i<preorder.size(); i++){
string word=s.substr(preorder[i]+1, end-preorder[i]);
if(preorder[i]==-1){
string cutstr=word;
int size=words.size();
for(int j=size-1; j>=0; j--){
cutstr+=" "+words[j];
}
result.push_back(cutstr);
}
else{
words.push_back(word);
recoverPath(result, words, s, preorders, preorder[i]);
words.pop_back();
}
}
} vector<string> wordBreak(string s, unordered_set<string> &dict) {
vector<string> result;
if(s.length()==0)return result;
map<int, vector<int> >preorders; //记录各个可分位置的前驱集合
vector<int> pos(1,-1); //以确定可分的位置 for(int i=0; i<s.length(); i++){
vector<int>preorder;
for(int k=0; k<pos.size(); k++){
if(dict.find(s.substr(pos[k]+1, i-pos[k]))!=dict.end()){
preorder.push_back(pos[k]);
}
}
if(preorder.size()>0){
preorders[i]=preorder;
pos.push_back(i);
}
} //恢复全部可能的切分路径
if(preorders.find(s.length()-1)==preorders.end())return result;
vector<string>words;
recoverPath(result, words, s, preorders, s.length()-1);
return result;
}
};

LeetCode: Word Break II [140]的更多相关文章

  1. LeetCode: Word Break II 解题报告

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  2. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  3. [LeetCode] Word Break II 拆分词句之二

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

  4. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  5. [LeetCode] Word Break II 解题思路

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

  6. [leetcode]Word Break II @ Python

    原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words  ...

  7. [Leetcode] word break ii拆分词语

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

  8. [LeetCode] Word Break II (TLE)

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

  9. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

随机推荐

  1. 【Unity3D】Unity自带组件—完成第一人称人物控制

    1.导入unity自带的Character Controllers包 2.可以看到First Person Controller组件的构成 Mouse Look() : 随鼠标的移动而使所属物体发生旋 ...

  2. mysql的几种隐式转化

    1. 表定义是字符型,传入的是Int 2. 字符集不一致.表定义的字段是gbk,传入的是utf8:这种在存储过程中出现得比较多. 数据库的字符集utf8 mysql> show create d ...

  3. delete drop truncate

    一.相同点 1 truncate.不带where子句的delete.drop都会删除表内的数据2 drop.truncate都是DDL语句(数据定义语言),执行后会自动提交 二.不同点 1trunca ...

  4. (原创)LAMP教程5-配置VirtualBox虚拟机中centos6.4的网卡

    (原创)LAMP教程5-配置VirtualBox虚拟机中centos6.4的网卡 是的,今天我们要讲的是如何配置VirtualBox虚拟机中centos6.4的网卡,毕竟我们是要做网站开发的,没有网络 ...

  5. 【SQL server】安装和配置

    (1)SQL sever 版本问题1: SQL sever 2000 .SQL sever 2005.SQL sever 2008 .SQL sever 2008 R2 安装的时候需要注意是SQL s ...

  6. asp.net中遍历界面上所有控件进行属性设置

    * 使用方法: *  前台页面调用方法,重置:    protected void Reset_Click(object sender, EventArgs e)        {           ...

  7. 微信公众平台开发 ACCESS TOKEN

    获取access token 返回 access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token.正常情况下access_token有效期为7200秒,重复获取将 ...

  8. bzoj 1419 Red is good(期望DP)

    [题意] R红B蓝,选红得1选蓝失1,问最优状态下的期望得分. [思路] 设f[i][j]为i个Rj个B时的最优期望得分,则有转移式为: f[i][j]=max{ 0,(f[i-1][j]+1)*(i ...

  9. C#中的堆和栈

    看到一篇讲堆和栈的文章,是我目前为止见到讲的最易懂,详细和深入的.我翻译成中文.以此总结. 原文=>C#Heap(ing) Vs Stack(ing) in .NET 在net framewor ...

  10. Codevs No.2144 砝码称重2

    2016-05-31 22:01:16 题目链接: 砝码称重2 (Codevs No.2144) 题目大意: 给定N个砝码,求称出M的重量所需砝码最小个数 解法: 贪心 使砝码数量最小,当然是每个砝码 ...