try to generate all possible permutations, which have that property that val(a_i) is less or equal than val(a_j) foreach i,j
try to use BFS
in first step insert to queue letters A,B,C,D,E,F ....
qlen - length of queue
qact - act element in queue
Code: Select all
MAX = 6; /* size of {A,B,C,D,E,F} set */
while(qact < qlen)
{
q = queue[qlen];
if(LENGTH(q) >= K) break; /* in positions K..qlen are permutations ... */
for(i=0;i<MAX;i++) if(UNIQUE_IN_QUEUE(q + set(i))
{
INSERT_TO_QUEUE(q + set(i));
qlen++;
}
qact++;
}
UNIQUE_IN_QUEUE could be realized as you wish - linear search, binary search, lookup table
INSERT_TO_QUEUE could be realized the same - it's very important to insert data to queue by it's length....
I think that time complexity is O(N^2) where N is number of combinations ...
I hope, that should be work
Regards
Dominik
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)