【题目】

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. User Agent

    Android: Mozilla/5.0 (Linux; U; Android 2.3.5; zh-cn; MI-ONE Plus Build/GINGERBREAD) AppleWebKit/533 ...

  2. HDU 2222 (AC自动机模板题)

    题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...

  3. Android的Eclipse升级笔记

    Eclipse优化: 1.可以删除的插件有: EGit CVS Mylyn cdt 1.不用多次加载布局容器的版本: ADT-22.3.0 adt-bundle-windows-x86-2013103 ...

  4. asmcmd报错

    在进入asm的命令行时报错: 报错内容如下 [oracle@kel dbs]$ asmcmd asmcmd: command disallowed by current instance type 从 ...

  5. 在ubuntu下安装chrome

    To add PPA in Ubuntu 14.04 / 13.10 / 13.04 / 12.10 / 12.04 First download and install the key from G ...

  6. PHP中我经常容易混淆的三组函数

    原文:http://www.ido321.com/1252.html 一.htmlentities() 和htmlspecialchars() 1.htmlentities() 1.1  功能:把字符 ...

  7. 《Linux命令行与shell脚本编程大全》 第三章 学习笔记

    第三章:基本的bash shell命令 bash程序使用命令行参数来修改所启动shell的类型 参数 描述 -c string 从string中读取命令并处理他们 -r 启动限制性shell,限制用户 ...

  8. WordPress的SEO技术

    原文:http://blog.wpjam.com/article/wordpress-seo/ 文章目录[隐藏] 内容为王 页面优化 标题 链接(URL) Meta 标签 语义化 H1 H2 H3 等 ...

  9. 二分+最短路 uvalive 3270 Simplified GSM Network(推荐)

    // 二分+最短路 uvalive 3270 Simplified GSM Network(推荐) // 题意:已知B(1≤B≤50)个信号站和C(1≤C≤50)座城市的坐标,坐标的绝对值不大于100 ...

  10. win7旗舰版通知windows不是正版副本解决方法

    原文转载http://www.cnblogs.com/simple_666/archive/2013/04/13/win7%E6%97%97%E8%88%B0%E7%89%88%E9%80%9A%E7 ...