LeetCode: Word Break II [140]
【题目】
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]的更多相关文章
- 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 ...
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- [LeetCode] Word Break II 解题思路
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [leetcode]Word Break II @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words ...
- [Leetcode] word break ii拆分词语
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [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 ...
- leetcode 139. Word Break 、140. Word Break II
139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...
随机推荐
- RTP协议学习大总结从原理到代码
from:http://wenku.baidu.com/view/aaad3d136edb6f1aff001fa5.html 一.流媒体概念 流媒体包含广义和狭义两种内涵:广义上的流媒体指的是使音频和 ...
- 多线程程序设计学习(4)guarded suspension模式
Guarded Suspension[生产消费者模式] 一:guarded suspension的参与者--->guardedObject(被防卫)参与者 1.1该 ...
- TCP/IP详解学习笔记(6)-UDP协议
1.UDP简要介绍 UDP是传输层协议,和TCP协议处于一个分层中,但是与TCP协议不同,UDP协议并不提供超时重传,出错重传等功能,也就是说其是不可靠的协议. 2.UDP协议头 2.1.UDP端口号 ...
- POJ 1042 Gone Fishing
题意:一个人要在n个湖中钓鱼,湖之间的路径是单向的,只能走1->2->3->...->n这一条线路,告诉你每个湖中一开始能钓到鱼的初始值,和每钓5分钟就减少的数量,以及湖之间的 ...
- SDUT 3568 Rock Paper Scissors 状压统计
就是改成把一个字符串改成三进制状压,然后分成前5位,后5位统计, 然后直接统计 f[i][j][k]代表,后5局状压为k的,前5局比和j状态比输了5局的有多少个人 复杂度是O(T*30000*25*m ...
- IOS AVAUDIOPLAYER 播放器使用
1. 导入 AVFoundation.framework 2.导入头文件 #import <AVFoundation/AVFoundation.h> 3. player = [[AVAu ...
- 纯css实现扁平化360卫士logo demo
前几天在w3ctech上看到有人用纯css写出了360卫士的logo,感觉蛮好玩的. 因为自己用css以来,还没有写过这种玩意,出于娱乐,我也来试着尝试一下. 开始也不知到怎么下手,最棘手的是那两个像 ...
- mybatis系列-11-一对多查询
11.1 需求 查询订单及订单明细的信息. 11.2 sql语句 确定主查询表:订单表 确定关联查询表:订单明细表 在一对一查询基础上添加订单明细表关联即可. SELECT order ...
- ArcMap10.1无法保存编辑的内容
问题描述:在arcMap10.1中编辑SDE库中要素,保存编辑内容时报错: 无法保存编辑内容.基础DBMS错误[ORA-29877:failed in the execution of the ODC ...
- The Datastore
[中央数据库模式难扩展]绝大多数的Web应用在处理一个为了以后的请求作检索用的请求时,需要存储信息.<1.Most useful web applications need to store i ...