WordBreakII

struct TrieNode{
    char val;
    bool isEnd;
    TrieNode* next[26] = {NULL};
    TrieNode(char x) : val(x), isEnd(false) {}
};

class TrieTree{
private:
    TrieNode* root = new TrieNode(‘a‘);
public:
    void trieInsert(string word){
        TrieNode* RootTemp = root;
        for(int i = 0; i < word.size(); i++){
            if(RootTemp->next[word[i]-‘a‘] == NULL){
                RootTemp->next[word[i]-‘a‘] = new TrieNode(word[i]);
                RootTemp = RootTemp->next[word[i]-‘a‘];
            }
            else{
                RootTemp = RootTemp->next[word[i]-‘a‘];
            }
        }
        RootTemp->isEnd = true;
    }
    void getSentense(vector<string>& result, string& sentense, string s){
        string sentenseTemp = sentense;
        sentense.push_back(‘ ‘);
        TrieNode* RootTemp = root;
        for(int i = 0; i < s.size(); i++){
            if(RootTemp->next[s[i]-‘a‘] != NULL){
                RootTemp = RootTemp->next[s[i]-‘a‘];
                sentense.push_back(s[i]);
                if(RootTemp->isEnd == true && i+1 != s.size()){
                    getSentense(result,sentense,string(s,i+1,s.size()-i-1));
                }
                else if(RootTemp->isEnd == true && i+1 == s.size()){
                        sentense.erase(sentense.begin());
                        result.push_back(sentense);
                     }
            }
            else{
                sentense = sentenseTemp;
                return;
            }
        }
        sentense = sentenseTemp;
    }
};

class Solution {
public:
    bool isBreak(string s, unordered_set<string> &dict){
        if(dict.empty()) return false;
        vector<bool> canBreak(s.size()+1,false);
        canBreak[0] = true;
        for(int i = 1; i <= s.size(); i++){
            for(int j = i-1; j >= 0; j--){
                if(canBreak[j] == true){
                    if(dict.find(string(s,j,i-j)) != dict.end()){
                        canBreak[i] = true;
                        break;
                    }
                }
            }
        }
        return canBreak[s.size()];
    }
    vector<string> wordBreak(string s, unordered_set<string> &dict) {
        vector<string>  result;
        TrieTree        myTrieTree;
        string          sentense;
        if(isBreak(s,dict)){
            for(auto& word : dict){
                myTrieTree.trieInsert(word);
            }
            myTrieTree.getSentense(result, sentense, s);
            return result;
        }else return result;
    }
};

用了DP和Trietree Backtracking

时间: 2024-10-12 16:44:07

WordBreakII的相关文章

Word Break II

https://oj.leetcode.com/problems/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, givens = "catsanddog

[email&#160;protected] [139/140] Word Break &amp; Word Break II

https://leetcode.com/problems/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, givens = "leetcode",dict = ["leet"

[LeetCode]Word Break 特里

意甲冠军:推断字符串给定的字符串是否构成词典. 来推断目标字符串相匹配整个字典.我们需要来推断目标字符串的每个前缀开始的下一场比赛,这需要匹配目标字符串的成功,所有前缀的枚举. class TrieNode{//from http://www.cnblogs.com/x1957/p/3492926.html public: TrieNode* ch[26];//char指针数组 bool isWord; TrieNode():isWord(false){ memset(ch,0,sizeof(T

LeetCode题目总结分类

注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problems/longest-valid-parentheses/ (也可以用一维数组,贪心)http://oj.leetcode.com/problems/valid-parentheses/http://oj.leetcode.com/probl

转载 ACM训练计划

leetcode代码 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problems/longest-valid-parentheses/ (也可以用一维数组,贪心)http://oj.leetcode.com/problems/valid-parentheses/http://oj.leetcode.com/problems/largest-rectang

Palindrome Partitioning leetcode java

题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","b"], ["a","a",&

[leetcode]Word Break II @ Python

原题地址:https://oj.leetcode.com/problems/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, givens = "c

Chapter five Depth First Search(深度优先搜索)

组合搜索问题 Combination 问题模型:求出所有满足条件的"组合".判断条件:组合中的元素是顺序无关的.时间复杂度:与 2^n 相关. 1.Chapter one 第2题 subsets(子集) 2.Chapter one 第3题 subsets-ii(子集II) 3.combination-sum(数字组合) 给出一组候选数字(C)和目标数字(T),找到C中所有的组合,使找出的数字和为T.C中的数字可以无限制重复被选取.所有的数字(包括目标数字)均为正整数.元素组合(a1, 

leetcode刷题(一)

1.数组 三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. 例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4], 满足要求的三元组集合为:[ [-1, 0, 1], [-1, -1, 2]] 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/3sum class