Find valid assignments of integers in arrays (permutations with given order)

Posted by evident on Stack Overflow See other posts from Stack Overflow or by evident
Published on 2011-01-07T11:45:14Z Indexed on 2011/01/07 11:53 UTC
Read the original article Hit count: 289

Hi everybody!

I am having a general problem finding a good algorithm for generating each possible assignment for some integers in different arrays.

Lets say I have n arrays and m numbers (I can have more arrays than numbers, more numbers than arrays or as much arrays as numbers).

As an example I have the numbers 1,2,3 and three arrays:

{ }, { }, { }

Now I would like to find each of these solutions:

{1,2,3}, { }, { }
{ }, {1,2,3}, { }
{ }, { }, {1,2,3}
{1,2}, {3}, { }
{1,2}, { }, {3}
{ }, {1,2}, {3}
{1}, {2,3}, { }
{1}, { }, {2,3}
{ }, {1}, {2,3}
{1}, {2}, {3}

So basically I would like to find each possible combination to assign the numbers to the different arrays with keeping the order. So as in the example the 1 always needs to come before the others and so on...

I want to write an algorithm in C++/Qt to find all these valid combinations.

Does anybody have an approach for me on how to handle this problem? How would I generate these permutations?

© Stack Overflow or respective owner

Related posts about c++

Related posts about arrays