单词拆分II-LintCode

来源:互联网 发布:linux使用crontab-e 编辑:程序博客网 时间:2024/06/05 16:18

给一字串s和单词的字典dict,在字串中增加空格来构建一个句子,并且所有单词都来自字典。
返回所有有可能的句子。

样例:
给一字串lintcode,字典为[“de”, “ding”, “co”, “code”, “lint”]
则结果为[“lint code”, “lint co de”]。

第一次代码,TLE

#ifndef C582_H#define C582_H#include<iostream>#include<string>#include<vector>#include<unordered_set>#include<unordered_map>using namespace std;class Solution {public:    /*    * @param s: A string    * @param wordDict: A set of words.    * @return: All possible sentences.    */    vector<string> wordBreak(string &s, unordered_set<string> &wordDict) {        // write your code here        vector<string> res;        string str;        findWord(s, wordDict, 1, str, res);        for (auto &c : res)        {            c.pop_back();        }        return res;    }    void findWord(string &s, unordered_set<string> &wordDict,int len,string &str,vector<string> &res)    {        if (s.size()==0)        {            res.push_back(str);        }        else        {            for (int i = len; i <= s.size(); ++i)            {                if (wordDict.find(s.substr(0, i)) != wordDict.end())                {                    string nstr = s.substr(i);                          string mstr = str;                    str += (s.substr(0, i) + " ");                    findWord(nstr, wordDict, 1, str, res);                    str = mstr;                }            }        }    }};#endif

利用isBreak(),预先判断是否可以拆分

#ifndef C582_H#define C582_H#include<iostream>#include<string>#include<vector>#include<unordered_set>#include <map> using namespace std;class Solution {public:    /*    * @param s: A string    * @param wordDict: A set of words.    * @return: All possible sentences.    */    vector<string> wordBreak(string &s, unordered_set<string> &wordDict) {        // write your code here        vector<string> res;        string str;        if (isBreak(s,wordDict))            findWord(s, wordDict, 1, str,res);        for (auto &c : res)        {            c.pop_back();        }        return res;    }    void findWord(string &s, unordered_set<string> &wordDict,int len,string &str,vector<string> &res)    {        if (s.size()==0)        {            res.push_back(str);        }        else        {            for (int i = len; i <= s.size(); ++i)            {                if (wordDict.find(s.substr(0, i)) != wordDict.end())                {                    string nstr = s.substr(i);                          string mstr = str;                    str += (s.substr(0, i) + " ");                    findWord(nstr, wordDict, 1, str,res);                    str = mstr;                }            }        }    }    bool isBreak(string &s, unordered_set<string> &wordDict)    {        int len = s.size();        vector<bool> dp(len + 1, false);        dp[0] = true;        for (int i = 1; i <= len; ++i)        {                       for (int j = 0; j < i; ++j)            {                if (dp[j] && (wordDict.find(s.substr(j, i-j)) != wordDict.end()))                {                    dp[i] = true;                    break;                }            }        }        return dp[len];    }};#endif
原创粉丝点击