#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。

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

posted @ 2016-07-08 11:06  富贵东坡  阅读(376)  评论(0编辑  收藏  举报