Esempio n. 1
0
 private void BuildTrier(string[] words)
 {
     Root = new TrierNode(' ');
     foreach (var word in words)
     {
         var current = Root;
         foreach (var wc in word)
         {
             if (!current.Children.ContainsKey(wc))
             {
                 current.Children[wc] = new TrierNode(wc);
             }
             current = current.Children[wc];
         }
         current.IsEndingChar = true;
     }
 }
Esempio n. 2
0
        public bool Query(char letter)
        {
            TrierNode longest = null;

            while (current != null)
            {
                if (current.Children.TryGetValue(letter, out TrierNode child))
                {
                    if (longest == null)
                    {
                        longest = child;
                    }
                    if (child.IsEndingChar)
                    {
                        current = longest;
                        return(true);
                    }
                }
                current = current.Fail;
            }
            current = longest ?? trier.Root;
            return(false);
        }
Esempio n. 3
0
 public StreamChecker(string[] words)
 {
     // build trier and fail pointer
     trier   = new Trier(words);
     current = trier.Root;
 }