A long lost art of programming. £º£©
You may design this algorithm in two step:
1. get all the combinations of chars in your string.
2. for every combination, compute its possible arragements.
like "123" in first step, we get
"1" "2" "3" "12" "23" "13" "123"
the second step, we try to rearrange every terms, so we have:
"1" "2" "3" "12" "21" "23" "32" "13" "31" "132" "123" "312"....
The algorithm to choose combinations:
for each string, we define a string of bits with the same length of it. if
one char of the string is to be choosed, then the corresponding bit is set
to 1, else 0. so you can see ,every possible combinations correspond to a
binary number. now it's easy to understand the seudo codes below:
Combination(cha r * s)
{
int t=slen(s);//get its length.
for(int i=1;i<exp(2,t-1);i++)//searching all possible combinations
{
string trs="";//empty it
for(int j=0;j<t;j++)
if(i&exp(2,j)== 1)//find chars with corresponding bit set to 1
trs+=s[j];//add the char into the set
RearrangeAndOut put(trs);
}
}
You may use recurse algorithm to rearrange the set. I think it's easy to
copy one from your text book.
The algorithm is very slow, you'd better redesign it using bit operations
and check for duplicated results in case you input string like "112".
"AsuWoo" <wu*****@msn.co m> дÈëÓʼþ
news:OD******** ******@TK2MSFTN GP09.phx.gbl...
hi,
I want to implement a function that prints all possible combinations of a
characters in a string,eg. input "123"into a textbox,
add "1","2","3","12 ","13","23","12 3",to a listbox,Or "ab" into a
textbox,add "a","b","ab "in a listbox,could anyone help me out ?