Word Break && Word Break II

来源:互联网 发布:mybatis打印sql语句 编辑:程序博客网 时间:2024/05/17 03:01

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;}

这里写图片描述

原创粉丝点击