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。

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

  • 相关阅读:
    delphi shr和shl的作用
    delphi socket 编程 使用多线程
    mysql 移除服务,并在cmd下切换目录
    delphi 结构体和TList的用法
    delphi 使用工控机控件 iThreadTimes 出现问题, 导致主程序创建页面的时候, 阻塞消息, 不能正常执行。
    Unicode 和 UTF-8 的区别
    Python 模块 re (Regular Expression)
    Python的函数式编程
    反向解析与PTR(Pointer Record)
    simhash算法
  • 原文地址:https://www.cnblogs.com/jidanfan/p/5652685.html
Copyright © 2011-2022 走看看