zoukankan      html  css  js  c++  java
  • Java字典树

    研一的时候软件工程课上老师让实现一个计算文本中某字符串的频率,第一想法就是hashmap键值对,然后老师将给定的1M文本换成了100M,1G。。我还记得第一节课100M测试文档我用笔记本跑了十分钟。。后来师兄提醒用字典树,然后靠着度娘“实现”了老师的要求。。今天心血来潮,又把字典树捡起来简单写了一遍
    package Trie;
    /**
     * 字典树
     * */
    public class DicTree {
        public static Node root;
    
        static class Node {
            char c;
            int num;
            Node[] son;
    
            public Node() {
                this.son = new Node[26];
            }
        }
    
        public DicTree() {
            this.root = new Node();
        }
    
        public static void insert(String str) {
            if (str == null || str.length() == 0)
                return;
            char[] chars = str.toCharArray();
            int pos;
            Node now = root;
            for (int i = 0, length = chars.length; i < length; i++) {
                Node newNode = new Node();
                pos = chars[i] - 'a';
                if (now.son[pos] == null) {
                    now.son[pos].num++;
    
                } else {
                    now.son[pos].num = 1;
                }
                now = now.son[pos];
            }
        }
    
        //求某字符串的数量
        public static int count(String str) {
            char[] chars = str.toCharArray();
            Node now = root;
            for (int i = 0; i < str.length(); i++) {
                int pos = chars[i] - 'a';
                now = now.son[pos];
            }
            return now.num;
        }
    
    }
     
  • 相关阅读:
    hdu 1686 Oulipo
    [NOI1997] 积木游戏
    错误录——未完待续
    NOI 2014 魔法森林
    hdu 4010 Query on The Trees
    求助大佬6——1种贪心
    51 nod 1205 流水线调度
    bzoj 1180: [CROATIAN2009]OTOCI
    HNOI2010 弹飞绵羊
    SDOI2008 洞穴勘测
  • 原文地址:https://www.cnblogs.com/ZoHy/p/12400692.html
Copyright © 2011-2022 走看看