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


C# Trie.Contains方法代码示例

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


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

示例1: TrieTests_Contains

        public void TrieTests_Contains()
        {
            Trie<char> trie = new Trie<char>();

            trie.Insert("Hello");

            Assert.IsTrue(trie.Contains("Hello"));
            Assert.IsFalse(trie.Contains("Hell"));
            Assert.IsFalse(trie.Contains("Hellop"));
        }
开发者ID:ZuTa,项目名称:Algorithms,代码行数:10,代码来源:TrieTests.cs

示例2: TrieTests_Contains2

        public void TrieTests_Contains2()
        {
            Trie<int> trie = new Trie<int>();

            int[] array = new int[] { 1, 2, 3 };

            trie.Insert(array);
            trie.Insert(new int[] { });

            Assert.IsTrue(trie.Contains(new int[] { }));
            Assert.IsTrue(trie.Contains(array));
            Assert.IsFalse(trie.Contains(new int[] { 1, 2 }));
        }
开发者ID:ZuTa,项目名称:Algorithms,代码行数:13,代码来源:TrieTests.cs

示例3: Main

    internal static void Main()
    {
        string[] separators = { " ", ",", ".", "!", "?", "\"", "/", "[", "]", "{", "}", "(", ")", ":", ";", "|", "<", ">", "*", "\t", "-", "_" };
        var keywords = new Trie(new TrieNode('#'));
        var wordsReader = new StreamReader(@"..\..\keywords.txt");
        var textReader = new StreamReader(@"..\..\text.txt");
        var writer = new StreamWriter(@"../../output1.txt");
        var result = new Dictionary<string, int>();
        string all;
        var time = new Stopwatch();

        // first variant builds trie from the keywords and checks each word from the text if it is present in the Trie
        time.Start();
        using (wordsReader)
        {
            for (var w = wordsReader.ReadLine(); w != null; w = wordsReader.ReadLine())
            {
                keywords.Add(w.ToLower());
            }
        }

        using (textReader)
        {
            all = textReader.ReadToEnd().ToLower();
        }

        StringBuilder word = new StringBuilder();
        for (int i = 0; i < all.Length; i++)
        {
            if (char.IsLetter(all[i]))
            {
                word.Append(all[i]);
            }
            else
            {
                if (keywords.Contains(word.ToString()))
                {
                    if (result.ContainsKey(word.ToString()))
                    {
                        result[word.ToString()]++;
                    }
                    else
                    {
                        result[word.ToString()] = 1;
                    }
                }

                word.Clear();
            }
        }

        using (writer)
        {
            writer.Write(string.Join(Environment.NewLine, result));
        }

        time.Stop();
        Console.WriteLine("Elapsed time in seconds: {0:0.0000}", time.ElapsedMilliseconds / 1000f);

        // Second variant bilds a Trie with the text and checks each keyword in it - 3 times slower due to too large trie
        time.Reset();

        var text = new Trie(new TrieNode('#'));
        time.Start();

        int wordCount = 0;
        textReader = new StreamReader(@"..\..\text.txt");
        using (textReader)
        {
            all = textReader.ReadToEnd().ToLower();
            for (int i = 0; i < all.Length; i++)
            {
                if (char.IsLetter(all[i]))
                {
                    word.Append(all[i]);
                }
                else
                {
                    text.Add(word.ToString());
                    word.Clear();
                }
            }
        }

        word.Clear();
        writer = new StreamWriter(@"../../output2.txt");
        wordsReader = new StreamReader(@"..\..\keywords.txt");
        using (wordsReader)
        {
            using (writer)
            {
                for (var w = wordsReader.ReadLine(); w != null; w = wordsReader.ReadLine())
                {
                    if (text.TryFindCount(w.ToLower(), out wordCount))
                    {
                        writer.WriteLine("[{0}, {1}]", w, wordCount);
                    }
                }
            }
        }
//.........这里部分代码省略.........
开发者ID:kalinalazarova1,项目名称:TelerikAcademy,代码行数:101,代码来源:TrieTest.cs

示例4: IsLosingWord

 private static bool IsLosingWord(Trie<string> wordList, string word)
 {
     return word.Length >= 4 && wordList.Contains(word);
 }
开发者ID:Gobiner,项目名称:ghost-puzzle,代码行数:4,代码来源:Program.cs

示例5: Contains_KeyExists

        public void Contains_KeyExists()
        {
            Trie<string, char, int> trie = new Trie<string, char, int>();

            trie.Add("key", 5);

            Assert.IsTrue(trie.Contains(trie.First()));
        }
开发者ID:flashcurd,项目名称:Shared.Utilities,代码行数:8,代码来源:TrieTests.cs

示例6: Contains_KeyDoesNotExist

        public void Contains_KeyDoesNotExist()
        {
            Trie<string, char, int> trie = new Trie<string, char, int>();

            trie.Add("key", 5);

            Assert.IsFalse(trie.Contains(new KeyValuePair<string, int>("key2", 1)));
        }
开发者ID:flashcurd,项目名称:Shared.Utilities,代码行数:8,代码来源:TrieTests.cs


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