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. TortoiseSVN使用方法

    1.初始化本地SVN目录,在某个文件夹鼠标右键点击. 初始化后目录 2.将某个目录下代码,添加到SVN目录中.                            3.将添加进去代码提取出来. 在某 ...

  2. linux中用shell获取昨天、明天或多天前的日期

    linux中用shell获取昨天.明天或多天前的日期 时间 -- :: BlogJava-专家区 原文 http://www.blogjava.net/xzclog/archive/2015/12/0 ...

  3. Different Ways to Add Parentheses

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  4. 最近开始做Android了

    最近开始做Android,在学习的过程中发现找以前知识很不方便啊,于是决定以后还是把知识记录在博客里吧,说不定也能为他人提供参考!

  5. ABAP 供应商、工厂对应公里数维护

    *&---------------------------------------------------------------------* *& Report  ZMMR011 ...

  6. 真机测试无缘无故finish了。程序也没有启动

    去钥匙串里边把多余的证书删除, 然后reset xcode - preference - 选中你的appleID - iOS Development  -  reset

  7. Sql order by 和 group BY一起使用时需要注意

    ORDER BY 子句中的列必须包含在聚合函数或 GROUP BY 子句中. slect * from table group by class,id order by id slect * from ...

  8. VS2013调试时,IIS Express Worker Process 已停止工作

    之前调试都没有报错的,今天突然报错了,然后网上找了下资料,很快解决了问题 这是我报错的提示 解决办法: 用管理员身份运行CMD,输入netsh winsock reset并回车(注意,必须是已管理员身 ...

  9. php dirname($path) 中文路径不对问题

    将$path中的\改为/ $dir=__FILE__;$dir = str_replace("\\","/", $dir);$dir=dirname($dir) ...

  10. python eval和literal_eval

    eval是python中一个相当智能的函数,把参数当成表达式,进行最大限度的解析, 比如: a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]" b ...