コード例 #1
0
ファイル: Trie.cs プロジェクト: albertk78/Study
        /// <summary>
        /// Find the longest word made up of other words
        /// 
        /// 'cat', 'cats', 'catsdogcats', 'catxdogcatsrat', 'dog', 'dogcatsdog', 'hippopotamuses', 'rat', 'ratcat', 'ratcatdog', 'ratcatdogcat'
        /// Then the longest compound word is ‘ratcatdogcat’ with 12 letters
        /// Note that the longest individual words are ‘catxdogcatsrat’ and ‘hippopotamuses’ with 14 letters, but they’re not fully constructed by other words
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string LongestCompoundWord(string[] text)
        {
            Trie trie = new Trie();

            //Add all words in the trie
            for (int index = 0; index < text.Length; index++)
            {
                string word = text[index];
                trie[word].Add(index);
            }

            //for each word, iterate through the trie and find prefixes made up of full word (termininal is true).
            //If prefix exists, then it's a possible candidate just search the trie for the remainder of the word
            string longestWord = string.Empty;

            foreach (string word in text)
            {
                List<string> segments = new List<string>();

                StringBuilder currentWord = new StringBuilder();
                TrieNode current = trie.Root;
                foreach (char letter in word)
                {
                    current = current.Children[letter];
                    currentWord.Append(letter);

                    //we have found an existing word
                    if (current.IsTerminal)
                    {
                        segments.Add(currentWord.ToString());
                        currentWord.Clear();
                    }
                }

                //more than 1 segment
                if (segments.Count > 1)
                {
                    //check if remainder is an existing word
                    if (trie.Contains(segments[segments.Count - 1]))
                    {
                        //valid word, check length
                        if (word.Length > longestWord.Length)
                        {
                            longestWord = word;
                        }
                    }
                }
            }

            return longestWord;
        }
コード例 #2
0
ファイル: Trie.cs プロジェクト: albertk78/Study
        /// <summary>
        /// Return all indexes of the search word occurring in the text
        /// 
        /// We can parse the text and store all indexes in a dictionary...requires n space and o(1) search time
        /// 
        /// Instead of we use more space efficient trie
        /// 
        ///     author, authority, and authorize - Hashtable index uses 6+9+9=24 characters but trie uses only 6+3+2=11
        ///     
        /// </summary>
        /// <param name="text"></param>
        /// <param name="search"></param>
        /// <returns></returns>
        public static int[] SearchWordPosition(string[] text, string search)
        {
            Trie trie = new Trie();

            for (int index = 0; index < text.Length; index++)
            {
                string word = text[index];
                trie[word].Add(index);
            }

            return trie[search].ToArray();
        }