zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    c# 生成、读取xml
    http长连接与短连接
    p.net 子页面刷新父页面,页面自动刷新方法汇总
    遍历页面上所有控件
    从数据库导入到Excel表格(同时传四个表的数据到一个Excel中)
    .net海量数据分页通用存储过程
    SQL大数据量分页存储过程效率测试
    给一个接口传递参数,并接收返回的参数
    在asp.net中长内容自动分页的实现.NET教程
    GridView72般技巧
  • 原文地址:https://www.cnblogs.com/zpjjtzzq/p/4265762.html
Copyright © 2011-2022 走看看