Print all combinations of M members of a set of N elements in a sequence such that each set can be obtained from the previous set by deleting one member and adding one member.
=================================
Take an example to understand:
given str: abcd , n =4, m =3
abc, abd, acd, dbc
main()
{
string s = "abcd";
sub = s.substr(0,m);
rem = s.substr(m+1, n);
comb(sub, rem);
}
comb( string s, string remain)
{
// extract the rest apart from first char
sub = s.substr(1, s.len);
// get all the combinations of the substr.
comb(sub, remain);
foreach ( char r in remain )
{
s[0] = r;
remain.remove(r);
comb(substr, r);
}
}
// take an example and run through the code to better understand the above.
Wednesday, June 20, 2007
printing combinations
Problem:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment