[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 ...
随机推荐
- JavaScript之函数
控制语句(switch) switch(name){ case '1': age = 123; break; case '2': age = 456; break; case '3': age = 7 ...
- PyQt4软件打包成exe文件
使用py2exe进行打包 例: from distutils.core import setupimport py2exeimport sys sys.argv.append('py2exe') py ...
- python traceback 变量值
import sys import traceback import cgitb def handleException(excType, excValue, trace): print 'error ...
- DOM高级
表格应用 获取 tBodies, tHead, tFoot, rows, cells 隔行变色 鼠标移入高亮, 添加,删除一行 DOM的方法使用 <!DOCTYPE html PUBLIC &q ...
- 【Storage】Ubuntu LVM 安装配置
参考资料: https://www.centos.bz/2012/02/installation-and-usage-of-lvm/ http://blog.chinaunix.net/uid-206 ...
- ACM/ICPC 之 "嵌套"队列 -插队(POJ2259)
这里插队的意思就是排队时遇到熟人则插到其后,否则排到队尾.(这个习惯不太好)(题意) 题目要求我们模拟“插队模型”和队列的入队和出队完成此算法. 由于题目的输入输出很多,此题的查找操作(找到熟人)需要 ...
- Java for LeetCode 237 Delete Node in a Linked List
Java实现如下: public class Solution { public void deleteNode(ListNode node) { if(node==null||node.next== ...
- vps mysql自动关闭
买了个阿里云的vps 装了一个wordpress,mysql一直自动关闭,百思不得其解,只有搜索 最后才发现是因为服务器内存太小,毕竟是最便宜的才512m ---------------------- ...
- nyoj19_排列
擅长排列的小明 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想 ...
- Greedy:Sum of Consecutive Prime Numbers(POJ 2739)
素数之和 题目大意:一些整数可以表示成一个连续素数之和,给定一个整数要你找出可以表示这一个整数的连续整数序列的个数 方法:打表,然后用游标卡尺法即可 #include <iostream> ...