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)的更多相关文章

  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 [140]

    [题目] Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where ...

  9. [Leetcode Week9]Word Break II

    Word Break II 题解 题目来源:https://leetcode.com/problems/word-break-ii/description/ Description Given a n ...

随机推荐

  1. BZOJ 3445: [Usaco2014 Feb] Roadblock

    Description 一个图, \(n\) 个点 \(m\) 条边,求将一条边距离翻倍后使 \(1-n\) 最短路径增加的最大增量. Sol Dijstra. 先跑一边最短路,然后枚举最短路,将路径 ...

  2. Valgrind 3.11.0编译安装

    Valgrind 3.11.0编译安装 Valgrind是一款用于内存调试.内存泄漏检测以及性能分析的软件开发工具. Valgrind遵守GNU通用公共许可证条款,是一款自由软件. 到3.3.0版本为 ...

  3. Python面试题库

    1.Python输出中文怎么解决 方法一:用encode和decode如:import os.pathimport xlrd,sys Filename=’/home/tom/Desktop/1234. ...

  4. quickLinux

    在cmd下稍微模仿一下linux的习惯...配置...主要是环境变量...就不用说了吧... time 要用 time.exe...可以计时...这个命令接受一个命令...可以中间有空格... ls直 ...

  5. C# Winfrom 页面传值

    2个窗体 Parent,Children 代码: Parent public partial class Parent : Form { public string parentValue = &qu ...

  6. 用dygraphs图表分析xdebug的trace结果

    2015年12月1日 19:44:23 推荐这一篇用百度的ECharts图表工具 本文用到的js图表库: dygraphs 注: 测试数据量 19108个数据点, 最好将数据的量级调小, 这样渲染会很 ...

  7. Effective C++ -----条款18:让接口容易被正确使用,不易被误用

    好的接口很容易被正确使用,不容易被误用.你应该在你IDE所有接口中努力达成这些性质. “促进正确使用”的办法包括接口的一致性,以及与内置类型的行为兼容. “阻止误用"的办法包括建立新类型.限 ...

  8. oracle触发器,一个表新增、修改的同时同步另一张表

    oracle创建触发器,把本地新增.修改数据过程同步到另一个服务器上去. 如果是本地,加数据库名即可.如果是远程服务器,不是一台机器,做一个db_link操作即可. ----------------- ...

  9. Parallels Destop软件配置

    Parallels Destop个人感觉最好用的mac虚拟win软件 http://pan.baidu.com/s/1jHFwIGm 密码:ab21百度云下载(或者下载自己百度云的) 安装方法: 1. ...

  10. iOS - iPhone开发 UILocalNotification的使用

    OS下的Notification的使用 Notification 是智能手机应用编程中非常常用的一种传递信息的机制,而且可以非常好的节省资源,不用消耗资源来不停地检查信息状态(Pooling),在iO ...