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. Git sparse checkout

    Git的sparse checkout在clone项目仓库时只clone指定路径下的信息. 步骤如下: (1) mkdir yourdir(2) cd yourdir(3) git init(4) g ...

  2. YSPASYS 中小型企业简单员工评价考核系统

    背景:公司运营接近2年时间了,随着不断的有员工入职.离职,使用信息化管理员工各类信息是一件很有必要的事儿.诸如员工基本信息,内部公告,资产盘点,客户管理,工作周报,优秀员工评选,请假.外出.报销.采购 ...

  3. Windows 右键添加「cmd 打开」

    1. 2. 3. 参考: 1.Windows右键添加"使用CMD打开" 2.WIN7.WIN8 右键在目录当前打开命令行Cmd窗口(图文)

  4. C++四种强制类型转换关键字

    C语言使用强制类型转换(Type Cast)很简单,不管什么类型的转换,形式都如下: TYPE b = (TYPE)a; c++提供了4种类型转换操作符来应对不同场合的应用. const_cast   ...

  5. java学习笔记_MIDI

    import javax.sound.midi.*; public class Midi { public void play(int instrument, int note) { try { Se ...

  6. Git 安装与使用(二)

    一.分支管理 在Git里,master是主分支,同时可以创建其他分支,支持各分支合并到主分支上,基本命令如下 1.创建分支 git checkout -b dev       创建dev分支,并切换到 ...

  7. sql 查询练习

    1. 用一条SQL 语句 查询出每门课都大于80 分的学生姓名name kecheng fenshu 张三 语文 81张三 数学 75李四 语文 76李四 数学 90王五 语文 81王五 数学 100 ...

  8. [转]DRY原则和Shy原则

    转自:http://blog.csdn.net/hukeab/article/details/2944675   保障可维护性的主要诀窍是遵循DRY原则和Shy原则. 在一个系统的整个生命周期里,理解 ...

  9. PHP+MYSQL 出现乱码的解决方法

    PHP+MYSQL 出现乱码的解决方法 使用PHP+MYSQL时遇到过字符乱问题,解决方法: 在mysql_connect后面加一句SET NAMES UTF8,即可使得UTF8的数据库消除乱码,对于 ...

  10. 通过MyEclipse生成Hibernate类文件和hbm.xml文件,或者annotation文件

    1.   前言 很多人都在使用myEclipse,很多公司也都使用hibernate框架,老版本的hibernate中,由于没有annotation,我们需要写两个文件来维护表与对象的关系,写一个类, ...