入门力扣自学笔记92 C++ (题目编号745)(未理解)

745. 前缀和后缀搜索

题目:

设计一个包含一些单词的特殊词典,并能够通过前缀和后缀来检索单词。

实现 WordFilter 类:

WordFilter(string[] words) 使用词典中的单词 words 初始化对象。
f(string pref, string suff) 返回词典中具有前缀 prefix 和后缀 suff 的单词的下标。如果存在不止一个满足要求的下标,返回其中 最大的下标 。如果不存在这样的单词,返回 -1 。


示例:

输入
["WordFilter", "f"]
[[["apple"]], ["a", "e"]]
输出
[null, 0]
解释
WordFilter wordFilter = new WordFilter(["apple"]);
wordFilter.f("a", "e"); // 返回 0 ,因为下标为 0 的单词:前缀 prefix = "a" 且 后缀 suff = "e" 。


提示:

1 <= words.length <= 104
1 <= words[i].length <= 7
1 <= pref.length, suff.length <= 7
words[i]、pref 和 suff 仅由小写英文字母组成
最多对函数 f 执行 104 次调用


来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/prefix-and-suffix-search
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


代码:

class Trie
{
public:
    bool isWord;
    int weight;
    unordered_map<char, Trie*> child;

    Trie ()
    {
        this->isWord = false;
        this->weight = -1;
    }

    void insert(string word)
    {
        Trie * rt = this;
        for (char c: word)
        {
            if (rt->child.count(c) == 0)
                rt->child[c] = new Trie();
            rt = rt->child[c];
        }
        rt->isWord = true;
    }

    bool search(string word)
    {
        Trie* rt = this;
        for (char c: word)
        {
            if (rt->child.count(c) == 0)
                return false;
            rt = rt->child[c];
        }
        return rt->isWord == true;
    }

    bool startsWith(string word)
    {
        Trie* rt = this;
        for (char c: word)
        {
            if (rt->child.count(c) == 0)
                return false;
            rt = rt->child[c];
        }
        return true;
    }
};


class WordFilter 
{
public:
    Trie * root;

    WordFilter(vector<string>& words) 
    {
        root = new Trie();
        for (int weight = 0; weight < words.size(); weight ++)
        {
            string word = words[weight];                //apple
            string tmp = word + '#' + word;             //apple#apple
            for (int i = 0; i < word.size() + 1; i ++)
            {
                Trie* cur = root;
                for (int j = i; j < tmp.size(); j ++)   //如ple#apple
                {
                    if (cur->child.count(tmp[j]) == 0)
                        cur->child[tmp[j]] = new Trie();
                    cur = cur->child[tmp[j]];
                    cur->weight = weight;
                }
            }
        }
    }
    
    int f(string prefix, string suffix) 
    {
        Trie* rt = root;
        string tmp = suffix + '#' + prefix;
        for (char c: tmp)
        {
            if (rt->child.count(c) == 0)
                return -1;
            rt = rt->child[c];
        }
        return rt->weight;
    }
};

/**
 * Your WordFilter object will be instantiated and called as such:
 * WordFilter* obj = new WordFilter(words);
 * int param_1 = obj->f(pref,suff);
 */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值