单词拆分I/II(字典树/dp)

单词拆分(字典树)

题目:https://leetcode-cn.com/problems/word-break/
代码:https://leetcode-cn.com/problems/word-break-ii/solution/dong-tai-gui-hua-qian-zhui-shu-by-scut_dell/

class Solution {
public:
    
    class Node {
    public:
        Node* next[26];
        bool has_word;
        Node() {
            has_word = false;
            for(int i = 0;i < 26;i++) next[i] = nullptr;
            // memset(next, NULL, sizeof(next));
        }
    };
    
    void insert(Node* root, string s) {
        for (auto c: s) {
            if (!root->next[c - 'a']) {
                root->next[c - 'a'] = new Node();
            }
            root = root->next[c - 'a'];
        }
        root->has_word = true;
    }
    
    void collect(string &s, vector<vector<int>> &next, int idx, string now, vector<string> &ret) {
        if (idx == s.size()) {
            now.pop_back();
            ret.push_back(now);
            return;
        }
        for (auto p: next[idx]) {
            collect(s, next, p + 1, now + s.substr(idx, p + 1 - idx) + " ", ret);
        }
    }
    
    bool wordBreak(string s, vector<string>& wordDict) {
        Node* root = new Node();
        for (auto word: wordDict) {
            insert(root, word);
        }
        
        vector<bool> match(s.size() + 1, false);
        vector<vector<int>> next(s.size(), vector<int>());
        
        match[s.size()] = true;
        
        for (int i = s.size() - 1;i >= 0;i--) {
            Node* t = root;
            for (int j = i;j < s.size();j++) {
                if (t->next[s[j] - 'a']) {
                    t = t->next[s[j] - 'a'];
                    if (t->has_word && match[j + 1]) {
                        next[i].push_back(j);
                    }
                } else {
                    break;
                }
            }
            if (next[i].size() > 0) {
                match[i] = true;
            }
        }
        return match[0];       
        // vector<string> ret;
        // if (!match[0]) {
        //     return ret;
        // } else {
        //     collect(s, next, 0, "", ret);
        //     return ret;
        // }
    }
};

单词拆分II(dp)

题目:https://leetcode-cn.com/problems/word-break-ii/
代码:https://leetcode-cn.com/problems/word-break-ii/solution/140-by-ikaruga/

class Solution {
public:
	vector<string> wordBreak(string s, vector<string>& wordDict)
	{
		if (!wordBreak_139(s, wordDict)) return {};

		size_t validEnd = 0;
		vector<vector<string>> dp(s.size() + 1, vector<string>());

		for (size_t i = 0; i < s.size(); i++)
		{
			if (i == validEnd + 1) return {};
			if (i != 0 && dp[i].empty()) continue;
			for (auto& word : wordDict)
			{
				size_t newEnd = i + word.size();
				if (newEnd > s.size()) continue;
				if (memcmp(&s[i], &word[0], word.size()) != 0) continue;
				validEnd = max(validEnd, newEnd);
				if (i == 0)
				{
					dp[newEnd].push_back(word);
					continue;
				}
				for (auto& d : dp[i])
				{
					dp[newEnd].push_back(d + " " + word);
				}
			}
		}

		return dp.back();
	}
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值