单词拆分II-LintCode

给一字串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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值