题解 | #字典树的实现#

字典树的实现

https://www.nowcoder.com/practice/7f8a8553ddbf4eaab749ec988726702b

#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct TrieNode
{
    int pass;
    int end;
    vector<TrieNode*> nexts;

    TrieNode()
        :pass(0),end(0),nexts(26,nullptr) {};
};

class Trie
{
public:
    Trie()
    {
        root = new TrieNode();
    }

    ~Trie()
    {
        freeTrie(root);
    }    

    void insert(const string& word)
    {
        TrieNode* node = root;
        node->pass++;
        for(auto ch : word)
        {
            int path = ch - 'a';
            if(path < 0 || path >= 26) return;
            if(node->nexts[path] == nullptr)
            {
                node->nexts[path] = new TrieNode();
            }
            node = node->nexts[path];
            node->pass++;
        }
        node->end++;
    }

    void erase(const string& word)
    {
        if(search(word) > 0)
        {
            TrieNode* node = root;
            node->pass--;
            for(auto ch : word)
            {
                int path = ch - 'a';
                if(path < 0 || path >= 26) return ;
                if(--node->nexts[path]->pass == 0)
                {
                    delete node->nexts[path];
                    node->nexts[path] = nullptr;
                    return;
                }
                
                node = node->nexts[path];
            }

            node->end--;
        }
    }

    int search(const string& word)
    {
        TrieNode* node = root;
        for(auto ch : word)
        {
            int path = ch - 'a';
            if(path < 0 || path >=26 || node->nexts[path] == nullptr)
            {
                return 0;
            }
            node = node->nexts[path];
        }
        return node->end ;
    }

    int prefixNumber(string pre)
    {
        TrieNode* node = root;
        for(auto ch : pre)
        {
            int path = ch - 'a';
            if(path < 0 || path >= 26 || node->nexts[path] == nullptr)
            {
                return 0;
            }
            node = node->nexts[path];
        }
        return node->pass;
    }

private:
    void freeTrie(TrieNode* node)
    {
        if(node == nullptr)
        {
            return;
        }

        for(TrieNode* next : node->nexts)
        {
            freeTrie(next);
        }

        delete node;
    }

private:
    TrieNode* root;
};

int main() {
    int op = 0;
    string str;
    int n = 0;
    while(cin >> n)
    {
        Trie trie;
        while(n--)
        {
            cin>>op>>str;
            switch(op)
            {
                case 1:
                    trie.insert(str);
                    break;
                case 2:
                    trie.erase(str);
                    break;
                case 3:
                    cout << (trie.search(str) > 0 ? "YES" : "NO")<<endl;
                    break;
                case 4:
                    cout << trie.prefixNumber(str) <<endl;
                    break;
            }
        }
    }
}
// 64 位输出请用 printf("%lld")



























全部评论

相关推荐

06-20 19:40
中原工学院 Java
网络存储:十几天不会让你拉人办卡就结束了吧?
点赞 评论 收藏
分享
水墨不写bug:疑似没有上过大学
点赞 评论 收藏
分享
家人们,我现在真的好纠结。我是26届的,目前还没有实习过。我现在的情况是,想参加秋招,但是感觉自己的简历特别空,没有实习经历会不会秋招直接凉凉啊?可我又听说现在很多公司对26届实习生也不太感冒,说什么不确定性大。而且我最近在准备考公,时间上也有点冲突。要是把时间花在实习上,备考时间就少了。但要是不实习,又怕以后就业有问题😫有没有懂行的友友帮我分析分析:26届现在不实习,秋招找工作真的会很难吗?考公和实习该怎么平衡啊?如果现在不实习,考完公再去找实习还来得及吗?真的太焦虑了,希望大家能给我点建议🙏
小破站_程序员YT:我可能和大家的观点不一样。人的精力是有限的,不能既要还要。你又想实习又想考公最后又要秋招上岸,我觉得哪有那么多的选择。你如果想考上岸,那就全力以赴。如果想秋招上岸,就继续投实习,投没了,就继续准备秋招,秋招不行继续春招。别到最后,考公没上岸,觉得是花了时间浪费在找实习上了, 秋招没上岸,觉得是浪费时间准备考公去了。我是认为很难说可以去平衡 不喜勿喷,可以叫我删除
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务