[Leetcode] 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"].
题意:找出S用dict表示的各种可能。
思路:这题和Word break的区别是上题中,只要考虑是否在的情况,不用考虑各种组合的问题。参考了GeekFans的。其整体思路是,先使用动态规划,找到字符串S中的各种分类,然后使用DFS找到满足条件的各种情况。代码如下:
class Solution {
private:
vector<string> midress;
vector<string> res;
vector<bool> *dp;
public:
vector<string> wordBreak(string s, unordered_set<string> &dict)
{
int len=s.size();
dp=new vector<bool>[len];
for(int i=;i<len;++i)
{
for(int j=i;j<len;j++)
{
if(dict.find(s.substr(i,j-i+)) !=dict.end())
dp[i].push_back(true);
else
dp[i].push_back(false);
}
}
dfs(s,len-);
return res;
}
void dfs(const string &s,int i)
{
if(i>=)
{
for(int j=;j<=i;++j)
{
if(dp[j][i-j])
{
midress.push_back(s.substr(j,i-j+));
dfs(s,j-);
midress.pop_back();
}
}
return;
}
else
{
string str;
for(int k=midress.size()-;k>=;--k)
{
str+=midress[k];
if(k>)
str+=" ";
}
res.push_back(str);
return;
}
}
};
注:在牛客网通过了,然后一段时间后再次运行时,报错,的结果让我无话可说,见图:

网友Grangyang和Code Gander分别给出不错解法。
[Leetcode] word break ii拆分词语的更多相关文章
- [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 解题报告
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
原题链接在这里: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 (TLE)
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode: Word Break II [140]
[题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where ...
- [LeetCode] 140. Word Break II 单词拆分II
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...
随机推荐
- maven-坐标与依赖
1.坐标-找到项目依赖的重要依据 <groupId>cmbc.com.cn</groupId> <artifactId>myapp</artifactId&g ...
- android 按钮动态点击
关键代码: 1.创建一个btn_selector.xml的文件 <?xml version="1.0" encoding="utf-8"?>< ...
- 强化记忆之php
php 输出的区分 新手摸索道路,有说不对的地方,还请多多包涵. echo 能够输出一个以上的字符串,也能输出html标签 print 一次只能接受一个字符串(区分与echo),也能输出html标签 ...
- Linux mysql启动与关闭
service mysql stop service mysqld start
- Win10正式版激活
参考:https://jingyan.baidu.com/article/47a29f2457af76c015239942.html https://jingyan.baidu.com/article ...
- POJ-2421-Constructing Roads(最小生成树 普利姆)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26694 Accepted: 11720 Description The ...
- python2.7练习小例子(五)
5):题目:输入三个整数x,y,z,请把这三个数由小到大输出. 程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比 ...
- 人工智能,图片识别,与GUI编程
GUI编程: https://sourceforge.net/projects/pyqt/ 百度aip图片识别 https://pypi.python.org/pypi/baidu-aip
- malloc函数分配失败处理的严重性
本次在实际测试情况下,发现程序无缘无故的异常,导致看门狗超时复位,经过排查是malloc函数分配失败的时候,依然对指针进行了操作,导致异常.以前没重视这个问题是因为,总觉的malloc基本都会成功的, ...
- 【jQuery】 选择器
[jQuery] 选择器 资料: w3school http://www.w3school.com.cn/jquery/jquery_ref_selectors.asp 1. 标签选择器 : $(& ...