2021-12-28 472. 连接词(Trie树、DFS、一个有意思的剪枝)

//朴素Trie再DFS做法
//必须剪枝时间复杂度才够
class Solution {
public:
    int son[100000][26];
    int end[100000];
    int idx = 0;
    bool dfs(string& s, int startIndex){
        if(startIndex == s.size()) return true;

        string temp = "";
        for(int i = startIndex; i < s.size(); i++){
            temp += s[i];
            if(query(temp)){
                if(dfs(s, i + 1)) return true;
            }
            
        }
        return false;
    }
    bool query(string& s){
        int p = 0;
        for(char& c: s){
            int x = c - 'a';
            if(son[p][x] == -1) return false;
            p = son[p][x];
        }
        return end[p];
    }
    void insert(string& s){
        int p = 0;
        for(char& c : s){
            int x = c - 'a';
            if(son[p][x] == -1) son[p][x] = ++idx;
            p = son[p][x];
        }
        end[p] = 1;
    }
    bool static cmp(string& a, string& b){
        return a.size() < b.size();
    }
    vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {
        memset(son, -1, sizeof son);
        memset(end, 0, sizeof end);
        int n = words.size();
        sort(words.begin(), words.end(), cmp);
        vector<string> ans;
        for(int i = 0; i < n; i++){
            if(words[i] == "") continue;
            
            if(dfs(words[i], 0)) ans.push_back(words[i]);
            //A是X、Y、Z连接词,则由A构成的连接词B可以由X、Y、Z构成
            //剪枝,A不用再添加
            else insert(words[i]);
        }
        return ans;
    }
};
//Query操作中直接DFS
class Solution {
public:
    int son[100000][26];
    int end[100000];
    int idx = 0;
    bool dfs(string& s, int startIndex){
        if(startIndex == s.size()) return true;

        string temp = "";
        for(int i = startIndex; i < s.size(); i++){
            temp += s[i];
            if(query(temp)){
                if(dfs(s, i + 1)) return true;
            }
            
        }
        return false;
    }
    bool query(string& s){
        int p = 0;
        for(char& c: s){
            int x = c - 'a';
            if(son[p][x] == -1) return false;
            p = son[p][x];
        }
        return end[p];
    }
    void insert(string& s){
        int p = 0;
        for(char& c : s){
            int x = c - 'a';
            if(son[p][x] == -1) son[p][x] = ++idx;
            p = son[p][x];
        }
        end[p] = 1;
    }
    bool static cmp(string& a, string& b){
        return a.size() < b.size();
    }
    vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {
        memset(son, -1, sizeof son);
        memset(end, 0, sizeof end);
        int n = words.size();
        sort(words.begin(), words.end(), cmp);
        vector<string> ans;
        for(int i = 0; i < n; i++){
            if(words[i] == "") continue;
            
            if(dfs(words[i], 0)) ans.push_back(words[i]);
            //A是X、Y、Z连接词,则由A构成的连接词B可以由X、Y、Z构成
            //剪枝,A不用再添加
            else insert(words[i]);
        }
        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Prince_H_23

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值