当前位置: 首页>>代码示例>>C#>>正文


C# Trie.ContainsWord方法代码示例

本文整理汇总了C#中Trie.ContainsWord方法的典型用法代码示例。如果您正苦于以下问题:C# Trie.ContainsWord方法的具体用法?C# Trie.ContainsWord怎么用?C# Trie.ContainsWord使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Trie的用法示例。


在下文中一共展示了Trie.ContainsWord方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: VerifiesContainsWordForSmallTries

        public void VerifiesContainsWordForSmallTries()
        {
            var trie = new Trie(strings);

            Assert.IsTrue(strings.All(trie.ContainsWord));
            Assert.AreEqual(5, moreStrings.Count(trie.ContainsWord));
            Assert.AreEqual(0, evenMoreStrings.Count(trie.ContainsWord));

            trie = new Trie(moreStrings);
            Assert.IsTrue(moreStrings.All(trie.ContainsWord));
            Assert.AreEqual(6, strings.Count(trie.ContainsWord));
            Assert.AreEqual(0, evenMoreStrings.Count(trie.ContainsWord));

            Assert.IsFalse(trie.ContainsWord("on"));
            Assert.IsFalse(trie.ContainsWord("ony"));
            Assert.IsFalse(trie.ContainsWord("onyx"));
            Assert.IsFalse(trie.ContainsWord(""));
            Assert.IsFalse(trie.ContainsWord("q"));
        }
开发者ID:davghouse,项目名称:SPOJ,代码行数:19,代码来源:TrieTests.cs

示例2: DoTest

        public static void DoTest()
        {
            var trie = new Trie();

            // Insert some how to words
            var prefix_howTo = "How to make";

            var word_howToSand = prefix_howTo + " a sandwitch";
            var word_howToRobot = prefix_howTo + " a robot";
            var word_howToOmelet = prefix_howTo + " an omelet";
            var word_howToProp = prefix_howTo + " a proposal";
            var listOfHow = new List<string>() { word_howToSand, word_howToRobot, word_howToOmelet, word_howToProp };

            trie.Add(word_howToOmelet);
            trie.Add(word_howToSand);
            trie.Add(word_howToRobot);
            trie.Add(word_howToProp);

            // Count of words = 4
            Debug.Assert(trie.Count == 4);

            // Insert some dictionary words
            var prefix_act = "act";

            var word_acts = prefix_act + "s";
            var word_actor = prefix_act + "or";
            var word_acting = prefix_act + "ing";
            var word_actress = prefix_act + "ress";
            var word_active = prefix_act + "ive";
            var listOfActWords = new List<string>() { word_acts, word_actor, word_acting, word_actress, word_active };

            trie.Add(word_actress);
            trie.Add(word_active);
            trie.Add(word_acting);
            trie.Add(word_acts);
            trie.Add(word_actor);

            // Count of words = 9
            Debug.Assert(trie.Count == 9);



            //
            // ASSERT THE WORDS IN TRIE.

            // Search for a word that doesn't exist
            Debug.Assert(false == trie.ContainsWord(prefix_howTo));

            // Search for prefix
            Debug.Assert(true == trie.ContainsPrefix(prefix_howTo));

            // Search for a prefix using a word
            Debug.Assert(true == trie.ContainsPrefix(word_howToSand));

            // Get all words that start with the how-to prefix
            var someHowToWords = trie.SearchByPrefix(prefix_howTo).ToList();
            Debug.Assert(someHowToWords.Count == listOfHow.Count);

            // Assert there are only two words under the prefix "acti" -> active, & acting
            var someActiWords = trie.SearchByPrefix("acti").ToList<string>();
            Debug.Assert(someActiWords.Count == 2);
            Debug.Assert(someActiWords.Contains(word_acting));
            Debug.Assert(someActiWords.Contains(word_active));

            // Assert that "acto" is not a word
            Debug.Assert(trie.ContainsWord("acto") == false);

            // Check the existance of other words
            Debug.Assert(trie.ContainsWord(word_actress) == true);
            Debug.Assert(trie.ContainsWord(word_howToProp) == true);



            //
            // TEST DELETING SOMETHINGS

            // Removing a prefix should fail
            var removing_acto_fails = false;
            try
            {
                // try removing a non-terminal word
                trie.Remove("acto");
                removing_acto_fails = false;
            }
            catch
            {
                // if exception occured then code works, word doesn't exist.
                removing_acto_fails = true;
            }

            Debug.Assert(removing_acto_fails == true);
            Debug.Assert(trie.Count == 9);

            // Removing a word should work
            var removing_acting_passes = false;
            try
            {
                // try removing a non-terminal word
                trie.Remove(word_acting);
                removing_acting_passes = true;
//.........这里部分代码省略.........
开发者ID:ChijunShen,项目名称:C-Sharp-Algorithms,代码行数:101,代码来源:TrieTest.cs


注:本文中的Trie.ContainsWord方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。