我想从数组中找到两个最长的单词,由较小的单词组成。我的代码如下。
当前输出是:
catxdogcatsrat,ratcatdogcat,catsdogcats,dogcatsdog
所需的输出是:
ratcatdogcat,catsdogcats
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();
}
}
答案 0 :(得分:0)
我认为你正在寻找像这样的东西
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拆分为2个部分,smallstrchar
数组包含小于4的小字符串的所有字符,Bigstrchar
包含大于5的字符串的所有字符。现在{{ 1}}取出所有不存在于smallstrchar并存在于Bigstrchar中的字符。现在我们有了需要从排序中排除的字符列表。
最后Except
在该字符串中查找是否包含该char。如果没有,那么添加到Finallist。稍后我们可以从列表中取2。
希望这有帮助
答案 1 :(得分:0)
您可以使用
简化将数组添加到list2list2.AddRange(stringrray)
答案 2 :(得分:0)
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;
}
这段代码有一点bug。 OrderbyDescending
子句可确保在cats
之前删除cat
。否则s
将保留,代码将无法按预期工作。但是如果我们使用一些虚构的值,这段代码将无法正常工作。例如:
List<string> words = new List<string>() { "abz", "e", "zefg", "f", "g", "abzefg" };
让我们看看abzef
。该算法将首先删除zefg
,但之后无法再进行任何操作。实际上,这个词是abz
,e
,f
和g
的组合。