Find an algorithm (subroutine) that when called repeatedly with the same character-string variable that is initialized with n characters, all different, will cycle through all the permutations of those n characters, so that for example, when called 24 times with a string of length 4, will have cycled that string through all 24 permutations and returned it to its initial state.
The last line gave me an idea. Instead of taking the letters out entirely, just switch them in turn. Swtich the last (n-1) letters until they form every permutation, then switch the letter that precedes the last (n-1) letters with the first one in the last (n-1) letters (designated by __ in the below example), then start over
So it would look like this:
For 1:
A (You can't switch one letter, so A will always be A)
A Back to beginning
Note how once the 1-set is done (each time after the first one) the first letter switches with the second letter
For 2:
AB- First 1 set
BA_ Second 1-set
AB_ Back to beginning
This 2-set is used in the 2+1 or 3-set. Again, after each 2-set is completed, the first letter switches with the second.
ABC- First 2-set
ACB- First 2-set
CAB_ Second 2-set
CBA- Second 2-set
BCA_ Third 2-set
BAC- Third 2-set
ABC_ Back to beginning (not printed)
On a 4 letter scale, that 3-set would be used 4 times. So it would be a recursive way to generate strings.
|
Posted by Gamer
on 2003-07-03 08:36:38 |