Word Break && Word Break II

leetcode上面的两道动态规划题。
题目1 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”
给定一个字符串s和一个单词字典,确定是否可以将s分割为空间分隔的
一个或多个字典单词的序列。

思路:
用f[i]表示0-i可否被目录字典分割。
f[i] =( f[j] &&( j-i的子串在目录中)) : 如果f[j]也就是(0-j位置的子串)可被分割且(j到i的子串在目录中)说明f[j]是可分割的。
n是字符串的长度,如果f[n] = true说明字符串可以被目录字典分割。
unordered_set是哈希的结构。

/*判断字串是否被字符典可分*/
bool wordBreak(string s, unordered_set<string> &dict)
{
    if (dict.size() == 0 || s.size() == 0)
        return false;
    vector<bool> f(s.size()+1, false);
    //长度有size元素有+1个隔板
    f[0] = true;
    //f[i]表示[0-i]可否被分词
    for (int i = 1; i <=s.size(); ++i) //判断f[i]
    {
        for (int j = i - 1; j >= 0; --j) //f[i] = f[j] && (i-j)是子串
        {
            cout<<s.substr(j, i-j).c_str()<<endl;
            if (f[j] && (dict.find(s.substr(j, i-j)) != dict.end()))
            {
                f[i] = true;
                break;
            }
        }
    }

    return f[s.size()];
}

题目二 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”].

给定一个字符串s和一个单词字典,在s中添加空格来构造每个单词的句子
,每个单词是一个有效的字典单词。返回所有这些可能的句子。
思路:
增加一个dp[i][j]表示0-i的子串可被划分,从j处划分。
auto变量会根据后面的赋值改变自己的属性。crbegin()中c代表常量,r代表翻转。


#include <iostream>
using namespace std;
#include <unordered_set>

void getpath(string& s, vector< vector<bool>>& dp, vector<string> &path,
    int len, int cur);
/*判断字串是否被字符典可分*/
void wordBreak(string s, unordered_set<string> &dict)
{
    if (dict.size() == 0 || s.size() == 0)
        return ;
    vector<bool> f(s.size() + 1, false);
    //长度有size元素有+1个隔板
    f[0] = true;
    //f[i]表示[0-i]可否被分词
    int len = s.size();
    vector<vector<bool> > dp(len + 1, vector<bool>(len + 1,false));
    for (int i = 1; i <= len; ++i) //判断f[i]
    {
        for (int j = i - 1; j >= 0; --j) //f[i] = f[j] && (i-j)是子串
        {
            if (f[j] && (dict.find(s.substr(j, i - j)) != dict.end()))
            {
                f[i] = true;
                dp[i][j] = true;
                //[j,i]可划分
            }
        }
    }
    vector<string>  path;
    getpath(s, dp, path, len, len);
}

void getpath(string& s, vector< vector<bool>>& dp, vector<string> &path,
    int len, int cur)
{
    if (cur == 0)
    {
        string ret;
        for (auto it = path.crbegin(); it != path.crend(); ++it)
        {
            ret += *it;
            ret += " ";
        }
        ret.erase(ret.end()-1);
        //删除结尾多余的空格符
        cout<<ret.c_str()<<endl;
    }
    for (int i = 0; i <= len; ++i)
    {
        if (dp[cur][i])
        {
            path.push_back(s.substr(i,cur-i));
            getpath(s, dp, path, len, i);
            path.pop_back();
        }
    }
}

int main()
{
    string str = "catsanddog";
    unordered_set<string> s;
    s.insert("cats");
    s.insert("cat");
    s.insert("dog");
    s.insert("sand");
    s.insert("and");
    wordBreak(str, s);
    return 0;
}

这里写图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值