首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找出由其他单词组成的两个最长的单词

找出由其他单词组成的两个最长的单词
EN

Stack Overflow用户
提问于 2016-12-02 04:17:12
回答 3查看 318关注 0票数 2

我想从数组中找到两个最长的单词,由较小的单词组成。下面给出了我的代码。

电流输出为:

猫猫,老鼠猫,猫猫,猫猫

必需的输出是:

老鼠猫,猫猫

代码语言:javascript
复制
class program 
{
    public static void Main(String[] args)
    {

        List<string> list2 = new List<string>();
        string[] stringrray =   { "cat", "cats", "catsdogcats", "catxdogcatsrat", "dog", "dogcatsdog",
                                 "hippopotamuses", "rat", "ratcatdogcat" };

        list2.Add(stringrray[0]);
        list2.Add(stringrray[1]);
        list2.Add(stringrray[2]);
        list2.Add(stringrray[3]);
        list2.Add(stringrray[4]);
        list2.Add(stringrray[5]);
        list2.Add(stringrray[6]);
        list2.Add(stringrray[7]);
        list2.Add(stringrray[8]);
        List<string> list = new List<string>();
        var mod = list2.OrderByDescending(x => x.Length).ToList();
        int j = 1;
        for (int k = 0; k < mod.Count; k++)
        {
            for (int i = 0; i < mod.Count-j; i++)
            {
                if (mod[i].Contains(mod[mod.Count - j]))
                {
                    j++;
                    list.Add(mod[i]);
                }
            }
        }
        var mod1 = list.OrderByDescending(x => x.Length);
        foreach (var i in mod1)
        {
            Console.WriteLine(i);
        }
        Console.ReadLine();
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-12-02 04:30:56

我想你是在找这样的东西

代码语言:javascript
复制
string[] stringrray = { "cat", "cats", "catsdogcats", "catxdogcatsrat", "dog", "dogcatsdog", "hippopotamuses", "rat", "ratcatdogcat" };
List<string> list2 = new List<string>(stringrray);
List<string> Finallist = new List<string>();
char[] smallstrchar = String.Join("", list2.Where(x => x.Length <= 4)).ToCharArray();
char[] bigstrchar = String.Join("", list2.Where(x => x.Length > 4)).ToCharArray();
char[] modchar = bigstrchar.Except(smallstrchar).ToArray();

foreach(string bigstr in list2)
{
    if(!(bigstr.IndexOfAny(modchar) != -1))
    {
        Finallist.Add(bigstr);
    }
}
Finallist = Finallist.OrderByDescending(x => x.Length).Take(2).ToList();
foreach(string finalstr in Finallist)
{
    Console.WriteLine(finalstr);
}

首先是stringrray,它包含所有需要处理的字符串,并从中找到最长的字符串。使用您的代码,它还接受包含x的字符串,但是所有其他字符都是匹配的。因此,我在list2中列出了一个字符串列表,其中包含了所有的值。然后将list2分割成两部分,即smallstrchar数组包含小于4的较小字符串的所有字符,Bigstrchar包含大于5的字符串字符。现在,Except删除了小字符串中不存在的所有字符,并显示在Bigstrchar中。现在我们有了需要排除在这类中的字符列表。

最后,要在该字符串中查找的IndexOfAny是否包含该字符。如果没有,那么添加到决赛。稍后,我们可以从名单中拿出2。

希望这能有所帮助

票数 0
EN

Stack Overflow用户

发布于 2016-12-02 04:41:35

可以简化将数组添加到list2中

代码语言:javascript
复制
list2.AddRange(stringrray)
票数 0
EN

Stack Overflow用户

发布于 2016-12-02 06:36:58

你可以用这个代码..。

代码语言:javascript
复制
static void Main(string[] args)
{
    List<string> words = new List<string>() { "cat", "cats", "catsdogcats", "catxdogcatsrat", "dog", "dogcatsdog", "hippopotamuses", "rat", "ratcatdogcat" };
    List<string> result = new List<string>();
    // solution 1
    foreach (string word in words)
    {
        if (IsCombinationOf(word, words))
        {
            result.Add(word);
        }
    }

    // solution 2
    result = words.Where(x => IsCombinationOf(x, words)).ToList();
}

public static bool IsCombinationOf(string word, List<string> parts)
{
    // removing the actual word just to be secure.
    parts = parts.Where(x => x != word).OrderByDescending(x => x.Length).ToList();

    // erase each part in word. Only those which are not in the list will remain.
    foreach (string part in parts)
    {
        word = Regex.Replace(word, part, "");
    }

    // if there are any caracters left, it hasn't been a combination
    return word.Length == 0;
}

但是..。

这段代码有一个小错误。OrderbyDescending子句确保在cat之前删除cats。否则,s将保持不变,并且代码将无法像预期的那样工作。但是,如果我们使用一些虚构的值,这个代码将不能正常工作。例如:

代码语言:javascript
复制
List<string> words = new List<string>() { "abz", "e", "zefg", "f", "g", "abzefg" };

让我们来看看abzef。该算法将首先删除zefg,但之后就不可能更进一步了。实际上,这个词是abzefg的组合。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40924720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档