zoukankan      html  css  js  c++  java
  • #1014 Trie树

    本题主要是求构造一棵Trie树,即词典树用于统计单词。

    C#代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace TestOJ
    {
        public class AplusB
        {
            static void Main()
            {
                   TestTrie1();
            }
               private static void TestTrie1()
            {
                int n = int.Parse(Console.ReadLine());
                Trie trie = new Trie();
                for (int i = 0; i < n; i++)
                {
    
                    string line = Console.ReadLine(); 
                    trie.Insert(line);
                }
                int m = int.Parse(Console.ReadLine());
                for (int i = 0; i < m; i++)
                {
                    string line = Console.ReadLine();
    
                    Console.WriteLine(trie.Serch(line));
    
                }
            }
            class Trie
            {
                public Trie()
                {
                    root = new TrieNode();
                }
                const int SIZE = 26;
                TrieNode root;
                public void Insert(string str)
                {
                    TrieNode node = root;
                    for (int i = 0; i < str.Length; i++)
                    {
                        var c = str[i];
                        var idx = c - 'a';
                        if (node.Nodes[idx] == null)
                        {
                            TrieNode temp = new TrieNode();
                            temp.value = c;
                            node.Nodes[idx] = temp;
                        }
                        else
                        {
                            node.Nodes[idx].num++;
                        }
                        node = node.Nodes[idx];
                    }
                    node.isEnd = true;
                }
                public int Serch(string str)
                {
                    TrieNode node = root;
                    for (int i = 0; i < str.Length; i++)
                    {
                        var c = str[i];
                        var idx = c - 'a';
                        if (node.Nodes[idx] == null)
                            return 0;
                        node = node.Nodes[idx];
                    }
                    return node.num;
                }
                class TrieNode
                {
                    internal TrieNode[] Nodes;
                    internal bool isEnd;
                    internal char value;
                    internal int num;
                    public TrieNode()
                    {
                        Nodes = new TrieNode[SIZE];
                        num = 1;
                    }
                }
            }
        }
    }

    该代码耗时 3312ms,内存 107MB。

    如果仅从实现统计功能需求来考虑的话,我的另一个方式是使用字典来进行统计。原理是对每一个单词进行从头到尾的拆分,每一次拆分的单词作为一个key,value则统计每一次获取到该key的次数。代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace TestOJ
    {
        public class AplusB
        {
            static void Main()
            {
                  int n = int.Parse(Console.ReadLine());
                Dictionary<string, int> dict = new Dictionary<string, int>(n * 10); 
                for (int i = 0; i < n; i++)
                {
    
                    string line = Console.ReadLine(); 
                    for (int j = 1; j <= line.Length; j++)
                    {
                        string cut = line.Substring(0, j);
                        if (!dict.ContainsKey(cut))
                        {
                            dict.Add(cut, 1);
                        }
                        else
                        {
                            dict[cut]++;
                        }
                    }
                }
                int m = int.Parse(Console.ReadLine());
                for (int i = 0; i < m; i++)
                {
                    string line = Console.ReadLine();
                    if (dict.ContainsKey(line))
                    {
                        Console.WriteLine(dict[line]);
                    }
                    else
                    {
                        Console.WriteLine(0);
                    }
                }
            }
        }
    }

    该代码耗时 3069ms,消耗内存 48MB。

    可以看到耗费的时间和内存都有所减少,缺点是无法统计到匹配的是那些单词。

  • 相关阅读:
    mybatis 梳理9--别名
    mybatis 梳理8--多个参数 @Param
    mybatis 梳理7--map的使用 (很好用)
    mybatis 梳理6--模糊查询
    mybatis 梳理5-- 增删改查 基于注解 (少)
    mybatis 梳理4--增删改查 基于配置文件(多)
    mybatis 梳理3--搭建环境 小栗子 结合 梳理10 复习
    mybatis 梳理2--代理设计模式 静态代理、动态代理(Proxy) (懵)
    mybatis 梳理1--文档官网、简介、持久化、持久层、持久层框架
    梳理5--核心配置文件pom 基础配置、构建配置、插件(记得!)、区别dependencies 和 dependencyManagement
  • 原文地址:https://www.cnblogs.com/jidanfan/p/5652685.html
Copyright © 2011-2022 走看看