kon3155 发表于 2008-8-14 10:59:46

用算法如何实现类似数据库中的笛卡尔积?

有n类事件,每一类事件又有若干个例子。如A(1,2,3),B(1,2,3),C(1,2),D(1,2),现在要对各个事件的例子组合进行操作,请问如何对这些组合进行存储,也就是采用何种数据结构比较合理?我用例子说明组合的意思:

每个事件单独表示为下面的表:
table1    table2   table3   table4   

   A          B          C          D
-------    -------   ------   -----
   1          1         1         1
   2          2         2         2
   3          3                  
                     
当事件之间两两组合时,就得到以下六个表:

table5   table6   table7   table8    table9    table10

    AB      AC   AD   B   C      BD   C   D
-------   -------   -----    -------    ------   -------
    11      11   11      11      1   1    1   1
    12      12   12      12      1   2    1   2
    13      21   21      21      2   1    2   1
    21      22   22      22      2   2    2   2
    22      31   31      31      3   1   
    23      32   32      32      3   2
    31                  
    32
    33

当事件每三个三个组合时,得到下面的表:

   table11   table12   table13      table14   

ABC      ABD      ACD      BCD
---------    --------   --------   --------
111      111      111      111
112      112      112      112
121      121      121      121
122      122      122      122
131      131      211      211
132      132      212      212
   .            .            .            .
   .            .            .            .
   .            .            .            .
   .            .            .            .

四个事件之间组合得到下面的表:

       table15
   A    B    C    D
------------------
   1    1    1    1
   1    1    1    2
   1    1    2    1
   1    1    2    2
   1    2    1    1
   1    2    1    2
         .
         .
         .
         .

我要如何对这些表进行统一的表示呢?也就是采用何种数据结构比较合理?

kon3155 发表于 2008-8-19 17:27:47

已解决,在csdn 上找到了正解,跟大家分享一下!
以4类事件为例:
string[][] a = new string[];
            a = new string[] { "king", "of", "the", "world" };
            a = new string[] { "cs", "app" };
            a = new string[] { "good", "cool", "dev" };
            a = new string[] { "king", "of", "the"};
            System.Collections.ArrayList arrlist = new System.Collections.ArrayList();
            int[] index = new int;

            while (index < a.Length)
            {
                string temp = "";
                for (int i = 0; i < index.Length; i++)
                {
                  temp += a] + "&";
                }

                arrlist.Add(temp.Substring(0, temp.Length - 1));

                index++;

                for (int i = index.Length - 1; i > 0; i--)
                {
                  if (index >= a.Length)
                  {
                        index = 0;
                        index++;
                  }
                }
            }

            Array b = arrlist.ToArray(typeof(string));

kon3155 发表于 2008-8-19 17:33:56

测试结果
king&cs&good&king
king&cs&good&of
king&cs&good&the
king&cs&cool&king
king&cs&cool&of
king&cs&cool&the
king&cs&dev&king
king&cs&dev&of
king&cs&dev&the
king&app&good&kin
king&app&good&of
king&app&good&the
king&app&cool&kin
king&app&cool&of
king&app&cool&the
king&app&dev&king
king&app&dev&of
king&app&dev&the
of&cs&good&king
of&cs&good&of
of&cs&good&the
of&cs&cool&king
of&cs&cool&of
of&cs&cool&the
of&cs&dev&king
of&cs&dev&of
of&cs&dev&the
of&app&good&king
of&app&good&of
of&app&good&the
of&app&cool&king
of&app&cool&of
of&app&cool&the
of&app&dev&king
of&app&dev&of
of&app&dev&the
the&cs&good&king
the&cs&good&of
the&cs&good&the
the&cs&cool&king
the&cs&cool&of
the&cs&cool&the
the&cs&dev&king
the&cs&dev&of
the&cs&dev&the
the&app&good&king
the&app&good&of
the&app&good&the
the&app&cool&king
the&app&cool&of
the&app&cool&the
the&app&dev&king
the&app&dev&of
the&app&dev&the
world&cs&good&kin
world&cs&good&of
world&cs&good&the
world&cs&cool&kin
world&cs&cool&of
world&cs&cool&the
world&cs&dev&king
world&cs&dev&of
world&cs&dev&the
world&app&good&ki
world&app&good&of
world&app&good&th
world&app&cool&ki
world&app&cool&of
world&app&cool&th
world&app&dev&kin
world&app&dev&of
world&app&dev&the
页: [1]
查看完整版本: 用算法如何实现类似数据库中的笛卡尔积?