C# 学习笔记:Trie树

问题描述:

#1014 : Trie树


方法1:

本方法参考了hihoCoder problem 1014:Trie Tree文档的C++版本。

using System;


namespace TrieProject
{
    public class Program
    {
        static void Main()
        {
            int n = int.Parse(Console.ReadLine());
            TreeNode root = new TreeNode();
            while (n > 0)
            {
                TrieTreeController.BuildTrieTree(root, Console.ReadLine());
                n -= 1;
            }
            n = int.Parse(Console.ReadLine());
            while (n > 0)
            {
                Console.WriteLine(TrieTreeController.GetNodeCount(root, Console.ReadLine()));
                n -= 1;
            }
        }
    }
    public class TreeNode
    {
        public TreeNode()
        {
            this.size = 0;
            this.children = new TreeNode[26];
        }

        public int size;
        public TreeNode[] children;

        public int AddChild(char key)
        {
            int count = (int)key - 97;
            if (children[count] == null)
                children[count] = new TreeNode();
            return count;
        }
    }
    public static class TrieTreeController
    {
        public static void BuildTrieTree(TreeNode root, string word)
        {
            for (int i = 0; i < word.Length; i++)
            {
                root = root.children[root.AddChild(word[i])];
                root.size++;
            }
        }
        public static int GetNodeCount(TreeNode root, string word)
        {
            for (int i = 0; i < word.Length; i++)
            {
                int count = (int)word[i] - 97;
                if (root.children[count] == null)
                    return 0;
                else
                    root = root.children[count];
            }
            return root.size;
        }
    }
}

方法2:

using System;
using System.Collections.Generic;

namespace TrieProject
{
    public class Program
    {
        static void Main()
        {
            int n = int.Parse(Console.ReadLine());
            TreeNode root = new TreeNode('\0');
            while (n > 0)
            {
                TreeNode.BuildTrieTree(root, Console.ReadLine());
                n -= 1;
            }
            n = int.Parse(Console.ReadLine());
            while (n > 0)
            {
                Console.WriteLine(TreeNode.GetNodeCount(root, Console.ReadLine()));
                n -= 1;
            }
        }
    }
    public class TreeNode
    {
        public TreeNode(char key)
        {
            this.key = key;
            this.size = 0;
            this.children = new List<TreeNode>();
        }

        char key;
        int size;
        IList<TreeNode> children;

        public char Key
        {
            get { return key; }
            set { key = value; }
        }
        public int Size
        {
            get { return size; }
        }
        public int Count
        {
            get { return this.children.Count; }
        }
        public IList<TreeNode> Children
        {
            get { return this.children; }
        }
        public TreeNode this[int index]
        {
            get { return this.children[index]; }
            set { this.children[index] = value; }
        }

        public TreeNode AddChild(char key)
        {
            TreeNode node = new TreeNode(key);
            this.children.Add(node);
            int i = Count - 2;
            while (i >= 0 && children[i].key > node.key)
            {
                children[i + 1] = children[i];
                i--;
            }
            children[i + 1] = node;
            return node;
        }

        private static string getTailString(string word)
        {
            return word.Length > 1 ? word.Substring(1) : null;
        }
        public static void BuildTrieTree(TreeNode root, string word)
        {
            if (!string.IsNullOrEmpty(word))
            {
                char target = word[0];
                bool found = false;
                for (int i = 0; i < root.Count; i++)
                {
                    if (root[i].Key == target)
                    {
                        found = true;
                        root[i].size++;
                        BuildTrieTree(root[i], getTailString(word));
                        break;
                    }
                }
                if (!found)
                {
                    var node = root.AddChild(target);
                    node.size++;
                    BuildTrieTree(node, getTailString(word));
                }
            }
        }

        public static int GetNodeCount(TreeNode root, string word)
        {
            int result = 0;
            if (!string.IsNullOrEmpty(word))
            {
                char target = word[0];
                for (int i = 0; i < root.Count; i++)
                {
                    if (root[i].Key == target)
                    {
                        result += GetNodeCount(root[i], getTailString(word));
                    }
                    if (root[i].Key >= target)
                    {
                        break;
                    }
                }
            }
            else
            {
                result = root.Size;
            }
            return result;
        }
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值