[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 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"].
这题没有AC,但是做出了递归回朔的版本,最后一个一串a后面跟个b的case超时了没法通过。
sand -- [dog] ->"cat sand dog"
/
cat --[sanddog]
/
[catsanddog]
\
cats --[anddog]
\
and -- [dog] -> "cats and dog"
从这个结构可以看出,这个问题可以被分解为当前的问题+更小规模的同类子问题,用递归求解。
我用了一个vector<stirng>来记录每个阶段(路径)上的结果,当搜索完字符串的时候(left >= right)意味着数组里的就是一个结果。
void wb(string s, int left, int right, unordered_set<string> &dict, vector<string> t, vector<string> &re) {
if (s.length() <= )
return;
if (left >= right) {
string k;
for (int i=; i<t.size(); i++) {
if (i != t.size()-) {
k = k + t[i] + " ";
}
else
k+=t[i];
}
re.push_back(k);
return;
}
//find matches
for (const auto& elem: dict) {
int len = (int)((string)elem).size();
if (left+len > s.length()) continue;
string sub = s.substr(left, len);
if (elem.compare(sub) == ) {
t.push_back(sub);
wb(s, left+len, right, dict, t, re);
if (t.size())
t.pop_back();
}
}
}
vector<string> wordBreak(string s, unordered_set<string> &dict) {
vector<string> ret;
vector<string> t;
wb(s, , s.size()-, dict,t, ret);
return ret;
}
在编写过程中遇到的两个bug都是因为没有很好理解程序造成的,第一个是t数组,一开始我传起引用,结果发现最后结果集翻倍。。。 第二个是调用wb后 t 没有pop,这样就把前一个分支的内容带到了下一分支造成错乱。
求大神指导如何AC
[LeetCode] Word Break II (TLE)的更多相关文章
- 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 [140]
[题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where ...
- [Leetcode Week9]Word Break II
Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...
随机推荐
- linux 终端全局代理设置
http://www.webupd8.org/2010/10/how-to-set-proxy-for-terminal-quick.html 即 export http_proxy='http:// ...
- CRC在线计算器
On-line CRC calculation and free library https://www.lammertbies.nl/comm/info/crc-calculation.html
- [转载]python gevent
原地址: http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001407503089 ...
- 11. javacript高级程序设计-DOM扩展
1. DOM扩展 1.1 选择符API l querySelector() 接收一个css选择符,返回与该模式匹配的第一个元素 l querySelectorAll() 接收一个css选择符,返回所有 ...
- HTML5 video 支持air play
< video src="/path/to/video.mp4" x-webkit-airplay="allow" preload controls> ...
- Effective C++ -----条款40:明智而审慎地使用多重继承
多重继承比单一继承复杂.它可能导致新的歧义性,以及对virtual继承的需要. virtual继承会增加大小.速度.初始化(及赋值)复杂度等等成本.如果virtual base classes不带任何 ...
- codeforces 518B. Tanya and Postcard 解题报告
题目链接:http://codeforces.com/problemset/problem/518/B 题目意思:给出字符串 s 和 t,如果 t 中有跟 s 完全相同的字母,数量等于或者多过 s,就 ...
- 【Python】range和xrange区别
转自:http://www.cnblogs.com/zhangjing0502/archive/2012/05/16/2503880.html range 函数说明:range([start,] ...
- 中等难度SQL语句(存储过程,分页,拼接字段、游标,日期类型转换,动态行转列,视图)汇总
一.创建存储过程 if Exists(select name from sysobjects where NAME = 'sp1LoginUser' and type='P')drop procedu ...
- IIS7.0提示---无法识别的属性“targetFramework”。请注意属性名称区分大小写。
当我把我做的网站放在IIS7.0的服务器上的时候,浏览时提示这个错误信息 配置错误 说明: 在处理向该请求提供服务所需的配置文件时出错.请检查下面的特定错误详细信息并适当地修改配置文件. 分析器错误消 ...