一、

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> &dict) {
int len=s.length();
vector<bool> v(len+,false);
v[]=true;
for(int pos=;pos<len;pos++){
for(int i=pos;v[pos]&&i<len;i++){
if(dict.find(s.substr(pos,i-pos+))!=dict.end())
v[i+]=true;
}
}
return v[len];
}
};

二、

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

即不仅要确定字符串是否能被字典分割,还要找出所有可能的组合。参考word break那题的DP思路,首先,从尾部开始逆向看字符串 s ,循环截取一个存在的词(milestone 1),然后在截取的位置递归,继续向前看,继续截取。。。知道到达头部,此时组合出一种答案;然后进入milestone 1 处的下一次循环,如下图的milestone 1,截取另外一个词,找另外一个答案。。。

代码如下:

  1.  class Solution {
    vector<string> midres;
    vector<string> res;
    vector<bool> *dp;
    public:
    vector<string> wordBreak(string s, unordered_set<string> &dict) {
    int len = s.length(); dp = new vector<bool>[len];
    for(int i=; i<len; ++i){
    for(int j=i; j<len; ++j){
    if(dict.find(s.substr(i, j-i+))!=dict.end()){
    dp[i].push_back(true); //第二维的下标实际是:单词长度-1
    }else{
    dp[i].push_back(false); //数组第二维用vector,size不一定是n,这样比n*n节省空间
    }
    }
    }
    func(s, len-);
    return res;
    } void func(const string &s, int i){
    if(i>=){
    for(int j=; j<=i; ++j){ if(dp[j][i-j]){ //注意此处的第二个下标是 i-j,不是i,因为数组的第二维长度是不固定的,第二维的下标实际是单词长度-1 midres.push_back(s.substr(j, i-j+));
    func(s, j-);
    midres.pop_back(); //继续考虑for循环的下一个分段处
    }
    }
    return;
    }
    else{
    string str;
    for(int k=midres.size()-; k>=; --k){ //注意遍历的顺序是倒序的
    str += midres[k]; //注意此处是k,不是i
    if(k>)
    str += " ";
    }
    res.push_back(str);
    return;
    }
    }
    };

注意递归函数的技巧,用全局变量res来保存答案,每次递归成功到达头部时将此中间结果保存到res。

转自:http://blog.csdn.net/jiadebin890724/article/details/34829865

LeetCode ||& Word Break && Word Break II(转)——动态规划的更多相关文章

  1. leetcode 940. 不同的子序列 II (动态规划 ,字符串, hash,好题)

    题目链接 https://leetcode-cn.com/problems/distinct-subsequences-ii/ 题意: 给定一个字符串,判断里面不相同的子串的总个数 思路: 非常巧妙的 ...

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

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

  3. [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

  4. [LeetCode] 244. Shortest Word Distance II 最短单词距离 II

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  5. 【一天一道LeetCode】#79. Word Search

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  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. [LeetCode] 243. Shortest Word Distance 最短单词距离

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  8. [LeetCode] 245. Shortest Word Distance III 最短单词距离 III

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

  9. leetcode面试准备: Word Pattern

    leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...

  10. LeetCode OJ:Word Search(单词查找)

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

随机推荐

  1. 浅谈 HTTP 协议

    一. HTTP简介 超文本传输协议 Hyper Text Transfer Protocol 是一种用于分布式.协作式和超媒体信息系统的应用层协议 HTTP是万维网的数据通信的基础 HTTP有很多应用 ...

  2. 利用visual studio 搜索替换功能清除项目中javascript文件的debugger;

    在做web项目中,写js代码时候,会有一堆的debugger;,当时又懒得删,后面就多起来了,在vs的编辑器里面,其查找替换功能支持正则和整个项目/解决方案替换,这样就很容易删掉debugger;,方 ...

  3. SQL Server数据库和MySQL数据库有什么区别?

    SQL Server数据库和MySQL数据库有什么区别呢?详细很多初入IT行业的朋友对于SQL Server数据库和MySQL数据库经常搞混,认为这两种数据库是同一种,其实不然,今天我们来分析一下这两 ...

  4. 图论trainning-part-1 A. 最短路

    A. 最短路 Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d      Java class name ...

  5. 【Android】SharedPreference存储数据

    SharedPreference存储数据 使用SharedPreference保存数据  putString(key,value) 使用SharedPreference读取数据  getString( ...

  6. BZOJ 1260: [CQOI2007]涂色paint【区间DP】

    Description 假设你有一条长度为5的木版,初始时没有涂过任何颜色.你希望把它的5个单位长度分别涂上红.绿.蓝.绿.红色,用一个长度为5的字符串表示这个目标:RGBGR. 每次你可以把一段连续 ...

  7. Hibernate 笔记 HQL查询 条件查询,聚集函数,子查询,导航查询

    在hibernate中进行多表查询,每个表中各取几个字段,也就是说查询出来的结果集并没有一个实体类与之对应,如何解决这个问题? 解决方案一,按照Object[]数据取出数据,然后自己组bean 解决方 ...

  8. 解决 IDEA 中src下xml等资源文件无法读取的问题

    该问题的实质是,idea对classpath的规定. 在eclipse中,把资源文件放在src文件夹下,是可以找到的: 但是在idea中,直接把资源文件放在src文件夹下,如果不进行设置,是不能被找到 ...

  9. Annual Congress of MUD

    Annual Congress of MUD 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Multiuser dungeon games, also called MUD games ...

  10. uva 11426 线性欧拉函数筛选+递推

    Problem J GCD Extreme (II) Input: Standard Input Output: Standard Output Given the value of N, you w ...