コード例 #1
0
        public List <string> AutoComplete(TrieDescription trie, string pattern)
        {
            // Because the two first characters are the Id of the TrieRoot object
            pattern = pattern.ToLower().Substring(2);

            TrieDescription start = trie;

            for (int i = 0; i < pattern.Length; ++i)
            {
                int ind = TrieConstants.GetCharIndex(pattern[i]);
                if (trie.Children[ind] == null)
                {
                    return(new List <string>());
                }
                if (i == pattern.Length - 1)
                {
                    start = trie.Children[ind];
                    break;
                }
                trie = trie.Children[ind];
            }

            var words = ListWords(start);

            for (var i = 0; i < words.Count; ++i)
            {
                words[i] = Id + pattern + words[i];
            }

            return(words);
        }
コード例 #2
0
        public bool AddWord(string word)
        {
            // Because the two first characters are the Id of the TrieRoot object
            word = word.ToLower().Substring(2);

            var tracer = Trie;

            for (var i = 0; i < word.Length; ++i)
            {
                int ind = TrieConstants.GetCharIndex(word[i]);
                if (tracer.Children[ind] == null)
                {
                    tracer.Children[ind] = new TrieDescription()
                    {
                        Key = word[i]
                    };
                }

                if (i == word.Length - 1)
                {
                    tracer.Children[ind].IsWord = true;
                }

                tracer = tracer.Children[ind];
            }
            return(true);
        }