生成List <list <keypairvalue <string,string =“”>&gt;&gt;的子集的排列。在C#</list <keypairvalue <string,>

时间:2015-02-20 17:57:20

标签: c# list permutation

请查看以下示例代码:

List<List<KeyValuePair<string, string>>> list = new List<List<KeyValuePair<string, string>>>();

list.Add(new List<KeyValuePair<string, string>>());
list[0].Add(new KeyValuePair<string, string>("Category 1", "Value A"));
list[0].Add(new KeyValuePair<string, string>("Category 1", "Value B"));

list.Add(new List<KeyValuePair<string, string>>());
list[1].Add(new KeyValuePair<string, string>("Category 2", "Value C"));
list[1].Add(new KeyValuePair<string, string>("Category 2", "Value D"));

list.Add(new List<KeyValuePair<string, string>>());
list[2].Add(new KeyValuePair<string, string>("Category 3", "Value E"));

它也可以表示为:

+---------------------------+---------------------------+
| ["Category 1", "Value A"] | ["Category 1", "Value B"] |
+---------------------------+---------------------------+
| ["Category 2", "Value C"] | ["Category 2", "Value D"] |
+---------------------------+---------------------------+
| ["Category 3", "Value E"] |                           |
+---------------------------+---------------------------+

我需要重新排列这些值,其中每列将包含一个唯一类别,并且我必须复制并交换值,以便实现给定类别限制的所有可能的值排列。换句话说......

+---------------------------+---------------------------+---------------------------+
| ["Category 1", "Value A"] | ["Category 2", "Value C"] | ["Category 3", "Value E"] |
+---------------------------+---------------------------+---------------------------+
| ["Category 1", "Value B"] | ["Category 2", "Value D"] | ["Category 3", "Value E"] |
+---------------------------+---------------------------+---------------------------+
| ["Category 1", "Value B"] | ["Category 2", "Value C"] | ["Category 3", "Value E"] |
+---------------------------+---------------------------+---------------------------+
| ["Category 1", "Value A"] | ["Category 2", "Value D"] | ["Category 3", "Value E"] |
+---------------------------+---------------------------+---------------------------+

请注意,我已经交换了第一列中的第三个和第四个值,以实现唯一的行。在考虑我的问题时,请忽略使用哪个列,只要我在此特定列/类别顺序中以唯一行结束,交换哪个值并不重要。另外,就像我之前所说的那样,这只是一个例子,我需要的东西无论元素数量多少都能发挥作用。

这是我到目前为止(这里的变量列表与上面相同):

int elementAmount = list.Count;
int combinationAmount = list.Select(x => x.Count).Aggregate(1, (x, y) => x * y); //Multiply sublists element count

List<List<KeyValuePair<string, string>>> sortedList = new List<List<KeyValuePair<string, string>>>(combinationAmount);

for (int i = 0; i < combinationAmount; i++)
sortedList.Add(new List<KeyValuePair<string, string>>(elementAmount));

for (int j = 0; j < elementAmount; j++)
{
    for (int i = 0; i < combinationAmount; i++)
        sortedList[i].Add(list[j].Count > i ? list[j][i] : sortedList[i - list[j].Count][j]);
}

输出:

+---------------------------+---------------------------+---------------------------+
| ["Category 1", "Value A"] | ["Category 2", "Value C"] | ["Category 3", "Value E"] |
+---------------------------+---------------------------+---------------------------+
| ["Category 1", "Value B"] | ["Category 2", "Value D"] | ["Category 3", "Value E"] |
+---------------------------+---------------------------+---------------------------+
| ["Category 1", "Value A"] | ["Category 2", "Value C"] | ["Category 3", "Value E"] |
+---------------------------+---------------------------+---------------------------+
| ["Category 1", "Value B"] | ["Category 2", "Value D"] | ["Category 3", "Value E"] |
+---------------------------+---------------------------+---------------------------+

如果我想出一个误导性的标题,我很抱歉,在一行中总结整个问题有点困难。请根据您的心意编辑它。

编辑 :我只是错过了互换。

1 个答案:

答案 0 :(得分:2)

首先,回答问题。请注意,之后我会发布一些不会被数据结构混淆的东西,这些数据结构只是通过递归显示排列。

    private void button1_Click(object sender, EventArgs e)
    {
        List<List<KeyValuePair<string, string>>> list = new List<List<KeyValuePair<string, string>>>();

        list.Add(new List<KeyValuePair<string, string>>());
        list[0].Add(new KeyValuePair<string, string>("Category 1", "Value A"));
        list[0].Add(new KeyValuePair<string, string>("Category 1", "Value B"));

        list.Add(new List<KeyValuePair<string, string>>());
        list[1].Add(new KeyValuePair<string, string>("Category 2", "Value C"));
        list[1].Add(new KeyValuePair<string, string>("Category 2", "Value D"));

        list.Add(new List<KeyValuePair<string, string>>());
        list[2].Add(new KeyValuePair<string, string>("Category 3", "Value E"));

        List<List<KeyValuePair<string, string>>> sortedList = new List<List<KeyValuePair<string, string>>>();

        permutation(list, 0, new List<KeyValuePair<string, string>>());
    }


    private void permutation( List<List<KeyValuePair<string, string>>> options, int srcPos, List<KeyValuePair<string, string>> result)
    {
        if (result.Count == options.Count)
            WriteOne(result);
        else
        {
            foreach (KeyValuePair<string, string> opt in options[srcPos])
            {
                List<KeyValuePair<string, string>> theClone = new List<KeyValuePair<string, string>>(result); 
                theClone.Add(opt); 
                permutation(options, srcPos + 1, theClone);
            }
        }
    }

    private void WriteOne(List<KeyValuePair<string, string>> OneResult)
    {
        StringBuilder line = new StringBuilder(80);
        StringBuilder line2 = new StringBuilder(80);
        line.Append("|"); line2.Append("+");
        foreach (KeyValuePair<string, string> item in OneResult)
        {
            line.Append(" [");       line2.Append("--");
            line.Append(item.Key);   line2.Append(new string('-', item.Key.Length));
            line.Append(", ");       line2.Append("--");
            line.Append(item.Value); line2.Append(new string('-', item.Value.Length));
            line.Append("] |");     line2.Append("--+");
        }
        line.AppendLine(); line2.AppendLine();
        Console.WriteLine(line.ToString());
        Console.WriteLine(line2.ToString());
    }

现在更简单的事情是产生上面的那个。它是完全相同的算法,但通用的List符号并不会使它变得混乱。

    private void Test()
    {
        permutation(new string[] { "AB", "CD", "E" }, 0, "");
    }


    private void permutation(string[] options, int srcPos, string result)
    {
        if (result.Length == options.Length)
            Console.WriteLine(result);
        else
        {
            foreach (char opt in options[srcPos])
            {
                permutation(options, srcPos + 1, result + opt);
            }
        }
    }