Word Break II

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

解分为三步:

(1)构造两级向量(vector<vector<int> > v)

链式存放前一个字符的位置。

(2)基于向量v逆向递归寻找词,借助栈

[dog  -->  [dog, sand  -->  [dog, sand, cat

  [dog, and   -->  [dog, and, cats

(3)出栈时词以空格隔开,存入ret向量

"cat sand dog"

"cats and dog"

class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
vector<string> ret;
string news = "" + s;
int n = news.size();
vector<vector<int> > v(n);
vector<bool> bpos(n, false);
bpos[] = true;
for(int i = ; i < n; i ++)
{
for(int j = ; j < i; j ++)
{
if(bpos[j] == true && wordDict.find(news.substr(j+, i-j)) != wordDict.end())
{
bpos[i] = true;
v[i].push_back(j);
}
}
}
if(bpos[n-] == false)
return ret;
else
{
stack<string> stk;
genRet(ret, news, v, stk, n-);
return ret;
}
}
void genRet(vector<string>& ret, string news, vector<vector<int> > v, stack<string> stk, int bpos)
{
if(bpos == )
{// generate final string
string str;
while(!stk.empty())
{
string top = stk.top();
stk.pop();
str += (top + " ");
}
str.erase(str.end()-);
ret.push_back(str);
}
else
{
for(int i = ; i < v[bpos].size(); i ++)
{
string cur = news.substr(v[bpos][i]+, bpos-v[bpos][i]);
stk.push(cur);
genRet(ret, news, v, stk, v[bpos][i]);
stk.pop();
}
}
}
};

【LeetCode】140. Word Break II的更多相关文章

  1. 【LeetCode】140. Word Break II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...

  2. 【LeetCode】139. Word Break 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【LeetCode】139 - Word Break

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

  4. 【leetcode】212. Word Search II

    Given an m x n board of characters and a list of strings words, return all words on the board. Each ...

  5. 【LeetCode】212. Word Search II 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀树 日期 题目地址:https://leetco ...

  6. 【leetcode】126. Word Ladder II

    题目如下: 解题思路:DFS或者BFS都行.本题的关键在于减少重复计算.我采用了两种方法:一是用字典dic_ladderlist记录每一个单词可以ladder的单词列表:另外是用dp数组记录从star ...

  7. leetcode 139. Word Break 、140. Word Break II

    139. Word Break 字符串能否通过划分成词典中的一个或多个单词. 使用动态规划,dp[i]表示当前以第i个位置(在字符串中实际上是i-1)结尾的字符串能否划分成词典中的单词. j表示的是以 ...

  8. 140. Word Break II(hard)

    欢迎fork and star:Nowcoder-Repository-github 140. Word Break II 题目: Given a non-empty string s and a d ...

  9. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

随机推荐

  1. [GIt] 团队工作效率分析工具gitstats

    copy : http://www.cnblogs.com/ToDoToTry/p/4311637.html 如果你是团队领导,关心团队的开发效率和工作激情:如果你是开源软件开发者,维护者某个repo ...

  2. otl中遇到的一些字符数组长度问题

    最近做的项目中对于数据库的操作为了有一个统一的操作,所以采用了otl技术作为中间媒介,当然,由于是用了别人的库,所以还是出现了很多问题.通过对问题进行分析,也形成了一些常用问题的处理方法. 在用otl ...

  3. oracle和mysql功能相同的函数

    wm_concat ---->mysql 的group_concat decode (两条件的)----->mysql 的 if decode (3个及以上条件的)---------> ...

  4. 关于Java的一些NIO框架的一点想法

    闲着有点无聊想写点东西. 问题:生活中工作中,会有人问我javaNIO框架里面 Netty Mina  xSocket Grizzly 等等哪个比较好? 在这里写一下自己的感受,也算是总结一下吧 在我 ...

  5. Binary Tree Preorder Traversal leetcode java

    题目: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given bina ...

  6. Longest Consecutive Sequence leetcode java

    题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...

  7. [置顶] Spring中自定义属性编辑器

    Spring中的属性编辑器能够自动的将String类型转化成需要的类型,例如一个类里面的一个整型属性,在配置文件中我们是通过String类型的数字进行配置的,这个过程中就需要一个转化操作,当然这个转化 ...

  8. 使用Gnupg对Linux系统中的文件进行加密

    GnuPG(GNU Privacy Guard或GPG)是一个以GNU通用公共许可证释出的开放源码用于加密或签名的软件,可用来取代PGP.大多数gpg软件仅支持命令行方式,一般人较难掌握.由于gpg软 ...

  9. (回溯法)和为n的所有不增正整数和式分解算法

    题目: 利用递归算法输出正整数和为n的所有不增的正整数和式.例如当n=5时,不增的和式如下: 5=5 5=4+1 5=3+2 5=3+1+1 5=2+2+1 5=2+1+1+1 5=1+1+1+1+1 ...

  10. Python面向对象编程 - 一个记事本程序范例(二)

    给程序加上控制台菜单 menu.py import sys from notebook import Notebook, Note class Menu: '''Display a menu and ...