[M字典树] lc676. 实现一个魔法字典(字典树+树上dfs+好题)

1. 题目来源

链接:676. 实现一个魔法字典

2. 题目解析

需要注意一个点,当字符串完全出现了也不算做有效答案哈,必须要改变一个字符才可以。所以第一遍写的答案就没有通过…

思路:

  • trie 建树
  • 此时需要用 dfs 进行搜索,条件为:
    • 从头搜这个字符串,搜到字符串末尾后,需要判断, trie 中是否存在这个字符串,且要求改变的次数恰好为 1 次。
    • 如果上式成立,则为 true。

关键是:如何体现恰好有几个字母不同这个条件?

  • 遍历 trie 时,额外维护一下当前遍历的位置已经出现了多少个不同的字符即可。
  • 实际上还是遍历树,只不过在这里是 trie 树。
  • 在这里插入图片描述

  • 时间复杂度 O ( n ∗ m ) O(n*m) O(nm) 字符串总长度
  • 空间复杂度 O ( n ∗ m ∗ 26 ) O(n*m*26) O(nm26)

const int N = 1e4+5;
int son[N][26], cnt[N], idx;

class MagicDictionary {
public:
    void insert(string s) {
        int p = 0;
        for (auto &c : s) {
            int u = c - 'a';
            if (!son[p][u]) son[p][u] = ++ idx;
            p = son[p][u];
        }
        cnt[p] ++ ;
    }

    MagicDictionary() {
        memset(son, 0, sizeof son);
        memset(cnt, 0, sizeof cnt);
        idx = 0;
    }
    
    void buildDict(vector<string> dictionary) {
        for (auto s : dictionary) insert(s);
    }
    
    bool search(string searchWord) {
        // 字符串,trie 的 p 索引,u 当前位置,c 改变字符数量
        auto dfs = [&](auto&& dfs, string& s, int p, int u, int c) -> bool {
            if (cnt[p] > 0 && u == s.size() && c == 1) return true;
            if (c > 1 || u == s.size()) return false;

            for (int i = 0; i < 26; i ++ ) {
                if (!son[p][i]) continue;
                if (dfs(dfs, s, son[p][i], u + 1, c + (s[u] - 'a' != i)))
                    return true;
            }

            return false;
        };

        return dfs(dfs, searchWord, 0, 0, 0);
    }
};

/**
 * Your MagicDictionary object will be instantiated and called as such:
 * MagicDictionary* obj = new MagicDictionary();
 * obj->buildDict(dictionary);
 * bool param_2 = obj->search(searchWord);
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Ypuyu

如果帮助到你,可以请作者喝水~

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

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

打赏作者

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

抵扣说明:

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

余额充值