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. ActivePython2.7 +Firefly1.2.2+WIN7服务器搭建过程(已通过)

    原地址:http://www.9miao.com/question-15-54027.html 1.ActivePython2.7 版本(内部已经包含easy_install,pywin32)2.所需 ...

  2. c++内存中字节对齐问题详解

    一.什么是字节对齐,为什么要对齐?    现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特 定的内存地址 ...

  3. 【leetcode】Dungeon Game (middle)

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  4. 1028-Digital Roots

    描述 The digital root of a positive integer is found by summing the digits of the integer. If the resu ...

  5. HDU1180+BFS

    bfs思路:三维标记状态 && 处理好 - | 和时刻的关系即可 /* bfs 思路:三维标记状态 && 处理好 - | 和时刻的关系即可 */ #include< ...

  6. HDU2110+母函数

    /* 母函数(生成函数) 题意: 有n种资产,每种资产num份,每份有val的价值 问取出总价值的1/3有多少种方案 */ #include<stdio.h> #include<st ...

  7. Android ExpandableListView 带有Checkbox的简单应用

    expandablelistview2_groups.xml <?xml version="1.0" encoding="utf-8"?> < ...

  8. P137、面试题23:从上往下打印二叉树

    题目:从上往下打印出二叉树的每个结点,同一层的结点按照从左到右的顺序打印.例如输入如图的二叉树,则依次打印出8,6,10,5,7,9,11.(其实是按层遍历)二叉树结点的定义如下:struct Bin ...

  9. STM32硬件复位时间

    两个参数,,1低电平时间 2低电平压值 1.stm32复位时间 ------  低电平时间:1.5 至 4.5 ms   2.压值

  10. 【Codeforces】#345 Div1

    1. Watchmen1.1 题目描述给$n$个点,求曼哈顿距离等于欧式距离的点对数. 1.2 基本思路由$|x_i-x_j|+|y_i-yj| = \sqrt{(x_i-x_j)^2+(y_i-yj ...