https://leetcode.com/problems/word-break/

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

For example, given
s = "leetcode",
dict = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

class Solution {
public:
bool wordBreak(string s, unordered_set<string>& wordDict) {
if(s.length() == ) return false; vector<bool> canBreak(s.length(), false);
for(int i=;i<s.length();++i) {
if(wordDict.find(s.substr(, i+)) != wordDict.end()) {
canBreak[i] = true;
continue;
}
for(int pre=;pre<i;++pre) {
if(canBreak[pre] && wordDict.find(s.substr(pre+, i-pre)) != wordDict.end()) {
canBreak[i] = true;
break;
}
}
} return canBreak[s.length()-];
}
};

https://leetcode.com/problems/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"].

class Solution {
public:
void findBreakPoint(vector<bool>& canBreak, string& s, unordered_set<string>& wordDict) {
for(int i=;i<s.length();++i) {
if(wordDict.find(s.substr(, i+)) != wordDict.end()) {
canBreak[i] = true;
continue;
}
for(int pre=;pre<i;++pre) {
if(canBreak[pre] && wordDict.find(s.substr(pre+, i-pre)) != wordDict.end()) {
canBreak[i] = true;
break;
}
}
}
}
void dfs(vector<string>& res, vector<string>& load, vector<bool>& canBreak, string& s, unordered_set<string>& wordDict, int idx) {
if(idx == s.length()-) {
string tmp = "";
for(int i=;i<load.size()-;++i) tmp += load[i] + " ";
if(load.size()>) tmp+=load[load.size()-];
res.push_back(tmp);
return;
} for(int nx=idx+;nx<s.length();++nx) {
if(canBreak[nx] && wordDict.find(s.substr(idx+, nx-idx)) != wordDict.end()) {
load.push_back(s.substr(idx+, nx-idx));
dfs(res, load, canBreak, s, wordDict, nx);
load.pop_back();
}
}
}
vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
vector<bool> canBreak(s.length(), false);
vector<string> res; res.clear();
vector<string> load; load.clear(); findBreakPoint(canBreak, s, wordDict);
if(canBreak[s.length()-]) dfs(res, load, canBreak, s, wordDict, -);
return res;
}
};

leetcode@ [139/140] Word Break & Word Break II的更多相关文章

  1. leetcode 139 单词拆分(word break)

    一开始的错误答案与错误思路,幻想直接遍历得出答案: class Solution { public: bool wordBreak(string s, vector<string>& ...

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

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

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

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

  4. leetcode@ [211] Add and Search Word - Data structure design

    https://leetcode.com/problems/add-and-search-word-data-structure-design/ 本题是在Trie树进行dfs+backtracking ...

  5. (*medium)LeetCode 211.Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  6. LeetCode 5:Given an input string, reverse the string word by word.

    problem: Given an input string, reverse the string word by word. For example: Given s = "the sk ...

  7. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  8. [LeetCode] 211. Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  9. 【LeetCode】819. Most Common Word 解题报告(Python)

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

随机推荐

  1. linux 5.5 开xmanager远程

    http://bbs.cqsztech.com/dv_rss.asp?s=xhtml&boardid=3&id=11&page=9 linux 5.5 开xmanager远程 ...

  2. php获取类的实例变量

    <?php class Page {private $title; //构造函数固定名称为__construct,这样能将php的类构造函数独立于类名,以后修改类名就无需修改构造函数名称 fun ...

  3. CodeChef November Challenge 2014

    重点回忆下我觉得比较有意义的题目吧.水题就只贴代码了. Distinct Characters Subsequence 水. 代码: #include <cstdio> #include ...

  4. 使用session技术来实现网上商城购物车的功能

    首先.简单的了解session和cookie的区别: 一.session和cookie的区别: session是把用户的首写到用户独占的session中(服务器端) cookie是把用户的数据写给用户 ...

  5. ANDROID_MARS学习笔记_S01_007Linear_layout嵌套与layout_weight的设置

    一.介绍 二.1.linear_layout.xml <?xml version="1.0" encoding="utf-8"?> <Line ...

  6. Qt之界面数据存储与获取(使用setUserData()和userData())

    在GUI开发中,往往需要在界面中存储一些有用的数据,这些数据可以来配置文件.注册表.数据库.或者是server. 无论来自哪里,这些数据对于用户来说都是至关重要的,它们在交互过程中大部分都会被用到,例 ...

  7. WPF跨程序集共享样式(跨程序集隔离样式和代码)

    前记:WPF中的样式使用一般分为两种Statci和Dynamic.两者的区别可以理解为,前者在运行的时候已经确定了样式的风格,而后者可以根据资源在运行时的修改而修改也可以使用那些在运行时才存在的资源. ...

  8. tortoisesvn › prefer local prefer repository

    tortoisesvn › prefer local prefer repository

  9. I2C I2S SPDIF

    I2C总线 大多数是用于电视机等家用电器的(显卡与显示器之间的通讯也是)I2S: I2S(Inter—IC Sound)总线, 又称 集成电路内置音频总线,是飞利浦公司为数字音频设备之间的音频数据传输 ...

  10. Miles per gallon to kilometers per liter

    Miles per gallon to kilometers per liter 1 Imperial Gallon = 4.54609188 litres 1 Mile = 1.609344 kil ...