【题目】

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. 视频捕捉全教程(vc+vfw)

    目 录 一. 视频捕获快速入门 二.基本的捕获设置 1.设置捕获速度: 2.设置终止捕获 3.捕获的时间限制 三.关于捕获窗口 1.创建一个AVICAP捕获窗口 2.将一个捕获窗口连接至捕获设备 3. ...

  2. distinct数据去重关键字

    在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值.关键词 distinct用于返回唯一不同的值. 表A: 示例1 select distinct nam ...

  3. Android 快速开发框架XUtils

    转载自:http://www.apkbus.com/forum.php?mod=viewthread&tid=241060&highlight=xUtils 最近搜了一些框架供初学者学 ...

  4. T-SQL查询进阶-10分钟理解游标

    转:http://www.cnblogs.com/CareySon/archive/2011/11/01/2231381.html 概述 游标是邪恶的! 在关系数据库中,我们对于查询的思考是面向集合的 ...

  5. Linux下的sniffer工具--TcpDump的安装和使用

    在如今众多的黑客技术中,嗅探器(sniffer)是最常见,也是最重要的技术之一. 用过windows平台上的sniffer工具(例如,netxray和sniffer pro软件)的朋友可能都知道,在共 ...

  6. 未能加载文件或程序集“Oracle.DataAccess, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342"

    转载自原文 未能加载文件或程序集"Oracle.DataAccess, Version=2.112.1.0,..." 若本机的Oracle版本是32位系统,则在调用Oracle数据 ...

  7. TabHost Tab的添加和删除

    TabHost 添加Tab项: tabhost = this.getTabHost(); TabSpec tabSpec = tabhost.newTabSpec("news"); ...

  8. POJ 2096-Collecting Bugs(概率dp入门)

    题意: 有n种bug和s种系统bug,每天发现一种bug(可能已经发现过了)所有种bug被发现的概率相同,求所有bug被发现的期望天数. 分析: dp[i][j]发现i种bug,j种系统bug期望天数 ...

  9. Hadoop的partitioner、全排序

    按数值排序 示例:按气温字段对天气数据集排序问题:不能将气温视为Text对象并以字典顺序排序正统做法:用顺序文件存储数据,其IntWritable键代表气温,其Text值就是数据行常用简单做法:首先, ...

  10. 迁移web.py项目至git@osc的项目演示平台

    1. 开启演示平台 选择WSGI,输入应用名称,即是演示网页的网址. 2. web.py代码迁移 将Python的site-packages目录下的web文件夹复制到代码目录下,与网页程序在同一个文件 ...