创建排列数组

时间:2012-09-29 17:01:44

标签: algorithm

我有一个看起来像这样的数组

mainArray =>
    [a] =>
        [A]
        [B]
        [C]
    [b] =>
        [D]
        [E]
        [F]
    [c] =>
        [G]
        [H]
        [I]

我想将这些放入这样的数组中:

secondArray =>
    [0] => { [A], [D], [G] }
    [1] => { [A], [E], [G] }
            .
            .
            .
    [n] => { [C], [F], [I] }

我无法弄清楚如何在$ secondArray中获取正确数量的元素,这些元素以{[A],[B],..}中的某个元素开头。例如,有多少以[A]开头。

这是我认为我必须做的事情:

secondArray =>
   [0] =>
        [A]

secondArray =>
   [0] =>
        [A]
        [D]

secondArray =>
   [0] =>
        [A]
        [D]
        [G]


secondArray =>
   [0] =>
        [A]
        [D]
        [G]
   [1] =>
        [A]
        [D]
        [H]


secondArray =>
   [0] =>
        [A]
        [D]
        [G]
   [1] =>
        [A]
        [D]
        [H]
   [2] =>
        [A]
        [D]
        [I]

secondArray =>
   [0] =>
        [A]
        [D]
        [G]
   [1] =>
        [A]
        [D]
        [H]
   [2] =>
        [A]
        [D]
        [I]
   [3] =>
        [A]
        [E]
        [G]
   [4] =>
        [A]
        [E]
        [H]
   [5] =>
        [A]
        [E]
        [I]

secondArray =>
   [0] =>
        [A]
        [D]
        [G]
   [1] =>
        [A]
        [D]
        [H]
   [2] =>
        [A]
        [D]
        [I]
   [3] =>
        [A]
        [E]
        [G]
   [4] =>
        [A]
        [E]
        [H]
   [5] =>
        [A]
        [E]
        [I]
   [6] =>
        [A]
        [F]
        [G]
   [7] =>
        [A]
        [F]
        [H]
   [8] =>
        [A]
        [F]
        [I]

等等,但我真的无法想到如何实现它......

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

您可以按顺序生成元素。

实现迭代器:

-remember the array of input arrays, if there exists a next element.
   and an array of indexes of the same length.
-if any input array is empty, the result set is empty and there is no next (first) element.
-initialise the array of indexes to all zeroes.

To get the next element:
-If you remember there is no next element, fail.
-compute the result:
--start with an empty result
--For each input array and its corresponding index
---Append input[index] to the result
-compute the next set of indexes:
--Iterate the indexes in reverse order. For each index
---Increment the index
---If the index now points past its corresponding input array
----Reset the index to zero
---Else
----Return the result, exiting the function.
-Remember there is no next element
-Return the result (last element).

如果您确实想要一次性使用所有组合,则代码会略微简化:

-if any input array is empty, the result set is empty.
-initialise the array of indexes to all zeroes.
-compute the result and store in the result set.
-while not done generating results:
--Iterate the indexes in reverse order. For each index
---Increment the index
---If the index now points past its corresponding input array
----Reset the index to zero
---Else
----Compute the result from the current indexes
----Add the result to the result set
----Continue generating the results
--(all indexes were reset to zero) finish generating the results.
-return the result set.

答案 1 :(得分:1)

在Ruby中:

['A', 'B', 'C'].product(['D', 'E', 'F'])
# => [['A', 'D'], ['A', 'E']...

Array#product可以接受多个数组。