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


C# Trie.AddWord方法代码示例

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


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

示例1: AddWord_RemoveWord01

 public void AddWord_RemoveWord01()
 {
     var trie = new Trie();
     trie.AddWord("");
     trie.AddWord("");
     Assert.AreEqual(1, trie.GetWords().Count);
     Assert.AreEqual(2, trie.RemoveWord(""));
     Assert.AreEqual(0, trie.GetWords().Count);
     trie.AddWord("");
     Assert.AreEqual(1, trie.GetWords().Count);
     Assert.AreEqual(1, trie.RemoveWord(""));
     Assert.AreEqual(0, trie.GetWords().Count);
 }
开发者ID:rmandvikar,项目名称:csharp-trie,代码行数:13,代码来源:TrieTest.cs

示例2: Main

        static void Main(string[] args)
        {
            var dictionary = new[] { "algorithm          10",
                "selection sort    20",
                "linked list           20",
                "selection            5",
                "viagra                -100",
            "selection sort fast 10"
            };

            var words = "The selection sort algorithm could be applied to linked lists. viagra";

            var trie = new Trie();

            foreach (var s in dictionary)
            {
                var input = s.Split(' ')
                    .Where(x => !string.IsNullOrEmpty(x))
                    .ToArray();

                var result = input[0];

                for (int i = 1; i < input.Length - 1; i++)
                {
                    result += (' ' + input[i]);
                }

                trie.AddWord(result.ToLower(), double.Parse(input[input.Length - 1]));
            }

            var score = 0.0;
            var spaceCounters = 0;
            var currentWord = string.Empty;

            words = words.ToLower();
            for (var i = 0; i < words.Length; i++)
            {
                if (!char.IsLetter(words[i]))
                {
                    continue;
                }

                if (currentWord.Length == 0)
                {
                    currentWord = words[i].ToString();
                    i++;
                }

                while (i < words.Length && char.IsLetter(words[i]))
                {
                    currentWord += words[i];
                    i++;
                }

                spaceCounters++;

                if (!trie.HasWord(currentWord))
                {
                    currentWord = string.Empty;
                    continue;
                }

                var currentScore = 0.0;

                while (trie.HasWord(currentWord))
                {
                    currentWord += ' ';
                    var j = i;
                    while (trie.GetWordWeight(currentWord) == 0)
                    {
                        j++;

                        while (j < words.Length && char.IsLetter(words[j]))
                        {
                            currentWord += words[j];
                            j++;
                        }

                        currentWord += ' ';

                        if (!trie.HasWord(currentWord))
                        {
                            break;
                        }

                        spaceCounters++;
                        i = j;
                    }

                    var weight = trie.GetWordWeight(currentWord);
                    currentScore = weight == 0 ? currentScore : weight;
                }

                score += currentScore;
                currentWord = string.Empty;
            }

            Console.WriteLine(score / spaceCounters);
        }
开发者ID:cwetanow,项目名称:Telerik,代码行数:99,代码来源:Startup.cs

示例3: Main

	public static void Main()
	{
		string[] words = new string[]{"app","apple","appl", "brown", "brony", "michael",
							"michtell", "mike", "brow","manny", "mann", "scott", "xie", "stokton"};
		Trie dict = new Trie();
		foreach(string s in words)
			dict.AddWord(s);
		List<string> l1 = dict.GetWords("s");
		foreach(string s in l1)
			Console.WriteLine(s);
		Console.WriteLine(dict.CheckWord("xi"));
		Console.WriteLine(dict.CountPrefix("ap"));
	}
开发者ID:siagung,项目名称:Qilu-leetcode,代码行数:13,代码来源:B221.Trie.cs

示例4: BuildSampleTrie

        private ITrie BuildSampleTrie()
        {
            ITrie trie = new Trie();
            string[] strings =
            {
                "123", "1", "23", "1",
                "this", "test", "the", "TEMP", "TOKEN", "TAKE", "THUMP"
            };

            foreach (string s in strings)
            {
                trie.AddWord(s);
            }
            return trie;
        }
开发者ID:rmandvikar,项目名称:csharp-trie,代码行数:15,代码来源:TrieTest.cs


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