【问题标题】:Find Biggest Concatenation Word in List在列表中查找最大的连接词
【发布时间】:2015-05-09 23:16:56
【问题描述】:

我有一些代码不能按预期工作。作为输出,我得到:

Concatenation.Concatenation+ConcatWord

我不知道如何让它按我的意愿工作。它应该输出数组中最大的连接词及其字符数。输入可能是:

string[] words = {"five","fivetwo","fourfive","fourfivetwo","one","onefiveone","two","twofivefourone"}` 

输出应该是:

"fourfivetwo" with 11 characters

Here's my code

我认为这部分有问题,但我不确定。:

List<ConcatWord> concatWords = new List<ConcatWord>();
for (int i = 0; i < data.Length; i++)
{
    ConcatWord concatWord = new ConcatWord(i, data[i]);
    for (int j = 0; j < data.Length; j++)
    {
        if (i != j)
        {
            if (data[i].Contains(data[j]) && data[i].Length > data[j].Length)
            {
                concatWord.words.Add(data[j]);
            }
        }
    }
}

【问题讨论】:

  • 我无法理解这个问题。请阅读stackoverflow.com/help/mcve 并提供一个重现问题的示例以及所需的输出。
  • 您能分享一下您的程序的输入和预期输出吗?
  • 这段代码不好理解。我会把它分解成单独的方法,这样每个方法都做一件事。
  • 什么是ConcatWord
  • @Jon Kittell,好的,我添加了另一个带有一些评论的代码,ConcatWord 是我从单词数组中预期更大的串联单词列表。

标签: c# arrays concatenation


【解决方案1】:

试试这个:

string[] words = { "five", "fivetwo", "fourfive", "fourfivetwo", "one", "onefiveone", "two", "twofivefourone" };

var allCombinations = words.SelectMany(w => words, (left, right) => left + right);
var combinedWords = words.Where(w => allCombinations.Contains(w));
string longestCombinedWord = combinedWords.OrderByDescending(w => w.Length).First();

Console.WriteLine(longestCombinedWord);

【讨论】:

    【解决方案2】:

    这里是你可以缩短它的方法

    string[] data = { "five","fivetwo","fivetwo","fivetwo","fourfive","fourfivetwo","one","onefiveone","two","twofivefourone" };
    
    string longestWord = data.OrderByDescending(words => words.Length)
                             .Distinct()
                             .First();
    
    Console.WriteLine (longestWord);
    

    【讨论】:

    • 是的,这个最长的词,但这不是连接词,包括数组中的其他词...
    • @Vaik 你的意思是删除重复项吗?检查我更新的答案...Distinct() 将删除重复项
    • 需要类似于fourfivetwo,它不是数组中最大的单词,但是这个单词包括fourfivetwo 使用连接的数组。
    【解决方案3】:

    你可以把它拆开。

    class Program
        {
            static void Main()
            {
                var searchTerms = new List<string> {"one", "two", "three", "four", "five"};
                var words = new List<string>
                {
                    "five",
                    "fivetwo",
                    "fourfive",
                    "fourfivetwo",
                    "one",
                    "onefiveone",
                    "two",
                    "twofivefourone"
                };
    
                var wordsAndMatches = new Dictionary<string, int>()
                {
                    {"five", 0}, {"fivetwo", 0}, {"fourfive", 0},
                    { "fourfivetwo", 0}, {"one", 0}, {"onefiveone", 0},
                    {"two", 0}, {"twofivefourone", 0}
                };
    
                foreach (var word in words)
                {
                    var numberOfMatches = GetNumberOfOccurances(word, searchTerms);
                    wordsAndMatches[word] = numberOfMatches;
                }
    
                foreach (var wordsAndMatch in wordsAndMatches)
                {
                    var result = string.Format("{0} contains {1} number of matches.", wordsAndMatch.Key, wordsAndMatch.Value);
                    Console.WriteLine(result);
                }
    
                var highestNumberOfConcatenations = wordsAndMatches.Values.OrderByDescending(x => x).FirstOrDefault();
                var wordWithHighestNumberOfMatches = wordsAndMatches.FirstOrDefault(x => x.Value == highestNumberOfConcatenations);
                Console.WriteLine("{0} has the highest number of matches at {1} matches.", wordWithHighestNumberOfMatches.Key, wordWithHighestNumberOfMatches.Value);
            }
    
            private static int GetNumberOfOccurances(string word, IEnumerable<string> searchTerms)
            {
                return searchTerms.Count(word.Contains);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2020-09-04
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      • 2020-08-03
      • 1970-01-01
      • 2015-02-24
      • 2013-11-16
      • 2020-01-29
      相关资源
      最近更新 更多