一个简单的统计问题(解决方案:Trie树)

题目如图

  输入几个不重复的单词和几个前缀,分别统计出单词中包含前缀的个数。

Trie树

  这个题目用到了 Trie 树.它在百度百科中的定义如下:在计算机科学中,Trie,又称字典树、单词查找树或键树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。Trie的核心思想是空间换时间,利用字符串的公共前缀来降低查询时间的开销以达到提高效率的目的。它有3个基本性质:根节点不包含字符,除根节点外每一个节点都只包含一个字符。从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。每个节点的所有子节点包含的字符都不相同。

代码示例

package algorithm.tree;

public class Trie {
    class Node {
        char value;
        byte end = 0;
        Node[] next = new Node[26];
        int count;
    }

    private Node root;

    public Trie() {
        root = new Node();
    }

    public boolean put(String value) {
        if (value == null || value.isEmpty()) {
            return false;
        }
        Node p = root;
        int index;
        char[] values = value.toCharArray();
        for (int i = 0; i < values.length; i++) {
            index = values[i] - 'a';
            if (p.next[index] == null) {
                Node node = new Node();
                node.value = values[i];
                p.next[index] = node;
            }
            p = p.next[index];
            p.end = 0;
            p.count++;
        }
        p.end = 1;
        return true;
    }

    public boolean find(String value,boolean pattern) {
        if (value == null || value.isEmpty()) {
            return false;
        }
        Node p = root;
        char[] values = value.toCharArray();
        for (int i = 0; i < values.length; i++) {
            int index = values[i] - 'a';
            if (p.next[index] == null) {
                return false;
            }
            p = p.next[index];
        }
        return pattern ? true : p.end == 1;
    }

    public int count(String value) {
        if (value == null || value.isEmpty()) {
            return 0;
        }
        Node p = root;
        char[] values = value.toCharArray();
        for (int i = 0; i < values.length; i++) {
            int index = values[i] - 'a';
            if (p.next[index] == null) {
                return 0;
            }
            p = p.next[index];
        }
        return p.count;
    }

}

测试

    public static void main(String[] args) {
        Trie trie = new Trie();

        trie.put("banana");
        trie.put("band");
        trie.put("bee");
        trie.put("absolute");
        trie.put("acm");

        //2
        int count1 = trie.count("ba");
        //3
        int count2 = trie.count("b");
        //1
        int count3 = trie.count("band");
        //0
        int count4 = trie.count("abc");

    }
posted @   丶Pz  阅读(256)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
历史上的今天:
2017-02-21 一个Python小白5个小时爬虫经历 【续】
点击右上角即可分享
微信分享提示

目录

目录

X
+

"大爷常来玩呀"

微信支付