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"].

思路:

我自己用回溯做超时了,直接看大神的20ms代码吧

主要是即考虑了从前向后的连接,也考虑了从后向前的连接。

class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string> &dict)
{
vector<vector<int>> flag(s.size() + , vector<int>());
flag[].push_back();
for (int i = ; i <= s.size(); i++) //当前判断字符串的结束位置的下一个位置 如i=1 表示结束位置是 s[0] 从前向后
{
for (int j = ; j < i; j++) //当前判断的字符串的起始位置
{
if (!flag[j].empty() && dict.find(s.substr(j, i - j)) != dict.end()) //只有该单词前面的单词能够找到时才压入结果
{
flag[i].push_back(j); //flag[i]中存储 以i为结束位置的下一个位置的单词 的起始位置 如flag[2] 里面存储的都是以s[1]结束的单词的第一个字母的位置
}
}
}
vector<string> result;
getResult(result, flag, s, s.size()); //从后向前找结果, 只有能够划分到最后一个字母的单词切割方式才考虑
return result;
} void getResult(vector<string> &result, vector<vector<int>> &flag, string s, int n)
{
for (int j = ; j < flag[n].size(); j++)
{
int i = flag[n][j];
if (i == ) //找到起始点了,压入划分的答案
{
result.push_back(s);
continue;
}
s.insert(s.begin() + i, ' ');
getResult(result, flag, s, i);
s.erase(i, );
}
}
};

我自己TLE的代码,我只考虑了从前向后,非常繁琐。但具体为什么会慢那么多我还没想明白。仅仅是因为没有考虑从后向前吗?

class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string> &dict) {
vector<vector<bool>> issubstr(s.size(), vector<bool>(s.size(), false));
for(int i = ; i < s.size(); i++)
{
for(int j = ; j <= s.length() - i; j++)
{
if((find(dict.begin(), dict.end(), s.substr(i, j))) != dict.end())
{
issubstr[i][i + j - ] = true;
}
}
} vector<string> ans;
vector<string> X;
vector<vector<string>> S();
int k = ;
for(int i = ; i <= s.length(); i++)
{
if(issubstr[][i - ])
{
S[k].push_back(s.substr(, i));
}
} while(k >= )
{
while(!S[k].empty())
{
while(X.size() > k)
{
X.pop_back();
}
X.push_back(S[k].back());
S[k].pop_back();
int Xtotallen = ;
vector<string>::iterator it;
for(it = X.begin(); it != X.end(); it++)
{
Xtotallen += it->length();
}
if(Xtotallen == s.length())
{
string partans = X[];
for(it = X.begin() + ; it != X.end(); it++)
{
partans += " ";
partans += (*it);
}
ans.push_back(partans);
}
else
{
k++;
if(S.size() <= k)
{
S.push_back(vector<string>());
}
for(int i = ; i <= s.length() - Xtotallen; i++)
{
if(issubstr[Xtotallen][Xtotallen + i -])
{
S[k].push_back(s.substr(Xtotallen, i));
}
}
}
}
k--;
}
return ans;
}
};

【leetcode】Word Break II (hard)★的更多相关文章

  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 (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  3. 【LeetCode】Word Break 解题报告

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  4. 【leetcode】Word Ladder II

      Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...

  5. 【leetcode】Word Break

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  6. 【leetcode】Word Break(python)

    思路是这种.我们从第一个字符開始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到.那么就是False了. 在找到第一个后,接下来找下一个断句处,当然是从第一个断句 ...

  7. 【leetcode】Word Search II(hard)★

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. 【leetcode】Word Ladder II(hard)★ 图 回头看

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  9. [Leetcode Week9]Word Break II

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

随机推荐

  1. Contoso 大学 - 使用 EF Code First 创建 MVC 应用

    原文 Contoso 大学 - 使用 EF Code First 创建 MVC 应用 Contoso 大学 Web 示例应用演示了如何使用 EF 技术创建 ASP.NET MVC 应用.示例中的 Co ...

  2. mysql数据库分组(GROUP BY)查询实例

    1.使用松散(Loose)索引扫描实现 GROUP BY 何谓松散索引扫描实现 GROUP BY 呢?实际上就是当 MySQL 完全利用索引扫描来实现 GROUP BY 的时候,并不需要扫描所有满足条 ...

  3. Swift下标

    还记得字典吗? var numberOfLegs= ["spider": 8,"ant": 6, "cat":4] numberOfLegs ...

  4. PHP中的变量

    PHP中的变量 程序是由代码与数据两部分组成,数据存储在变量,变量的本质是内存中的一个存储空间.变量对应的空间有一个名子,叫变量名,变量名用于对数据进行读写. 变量的定义 在php变量名之前必须使用' ...

  5. 09_Java8操作集合的一些新特性

    [使用forEach()结合Lambda表达式遍历集合] public class ForEachDemo { public static void main(String[] args) { Col ...

  6. 一些常用sqlite语句

    1,如果表不存在就新建一个 CComBSTR bstrCreatBat(L”CREATE TABLE IF NOT EXISTS tb_Name (\ rowIdIndex  INTEGER PRIM ...

  7. 【转】Session与Cookie的比较

    最近发现写博客也是提高学习效率的有效途径之一.好记性不如烂笔头,归纳总结时,你会发现总有一些东西你认为很熟了,它却在细微处讽刺你的错误.我学习COOKIE与SESSION时,几乎把社区所有相关的帖子都 ...

  8. OpenCV3添加滑动条和鼠标事件到图形界面

    鼠标事件和滑动条控制在计算机视觉和OpenCV中非常有用,使用这些控件,用户可以直接与图形界面交互,改变输入图像或者变量的属性值. /* In this section, we are going t ...

  9. Android学习1

    Activity学习(1) 只有一个Activity 进行Toast通知 Toast是一种短小的提醒,显示一段时间就会消失,试验学习,可以通过一个Button来实现. Button reg=(Butt ...

  10. Mac下使用Web服务器性能/压力测试工具webbench、ab、siege

    Web开发,少不了的就是压力测试,它是评估一个产品是否合格上线的基本标准,下面我们来一一剖析他们的使用方式. 测试前,前面先把系统的端口限制数改大,看看Mac下面的默认限制 ulimit -a ope ...