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. chromium中的性能优化工具syzyProf

    函数性能分析工具SyzyProf 我先开始介绍SyzyProf.这个工具可以捕获每个线程调用每个函数执行的时间,然后把结果生成一个KCacheGrind能够识别的数据格式文件,然后通过KCacheGr ...

  2. go语言基础之二维数组

    1.二维数组 示例: package main //必须有个main包 import "fmt" func main() { //有多少个[]就是多少维 //有多少个[]就用多少个 ...

  3. iOS开发-CoreMotion框架(加速计和陀螺仪)

    CoreMotion是一个专门处理Motion的框架,其中包含了两个部分加速度计和陀螺仪,在iOS4之前加速度计是由UIAccelerometer类来负责采集数据,现在一般都是用CoreMotion来 ...

  4. retrofit okhttp RxJava bk Gson Lambda 综合示例【配置】

    项目地址:https://github.com/baiqiantao/retrofit2_okhttp3_RxJava_butterknife.git <uses-permission andr ...

  5. Web页面切图和CSS注意事项

    一.Asp.net中的线程池设置 在Asp.net的服务处理中,每当服务器收到一个请求,HttpRuntime将从HttpApplication池中获取一个HttpApplication对象处理此请求 ...

  6. 理解闭包的微观世界和JS垃圾回收机制

    function a() { ; function b() { alert(++i); } return b; } var c = a(); c(); 一.闭包的微观世界 如果要更加深入的了解闭包以及 ...

  7. 解决WordPress 页面无法评论的问题

    最近在使用WordPress制作一个企业网站,因为是企业网站所以文章和页面都不需要评论功能,因此在主题里禁用掉了评论功能 //禁用页面和文章的评论功能//add_filter('the_posts', ...

  8. POJ 1265:Area

    Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4725   Accepted: 2135 Description ...

  9. [Functional Programming] Build a Linear congruential generator

    What we are going to do in this post, is to build a random number generator. As you might know that ...

  10. 在Lotus Notes设置邮件转发

    Notes里面设置邮件转发,一种是创建一个Agent代理,但这种方式有弊端,就是邮件标题缺失,这个比较别扭.这里就不推荐了. 另一种方法是创建Rule规则,这种方式完美.具体方法如下: 1.点Tool ...