Пример #1
0
// Pre-condition: str is a valid C String, and k is non-negative and
//                less than or equal to the length of str.
// Post-condition: All of the permutations of str with the first k
//                 characters fixed in their original positions are
//                 printed. Namely, if n is the length of str, then
//                 (n-k)! permutations are printed.
void RecursivePermute(char str[], int k) {
     
     int j;
     
     // Base-case: Since all letters are fixed, we can ONLY print
     // what's stored in str.
     if (k == strlen(str))
         printf("%s\n", str);
         
     else {
          
         // Loop through each possible starting letter for index k,
         // the first index for which we have a choice.
         for (j=k; j<strlen(str); j++) {
         
             // Place the character stored in index j in location k.
             ExchangeCharacters(str, k, j);
             
             // Print out all of the permutations with that character
             // just chosen above fixed. 
             RecursivePermute(str, k+1);
             
             // Put the original character that used to be there back
             // in its place.
             ExchangeCharacters(str, j, k);
         }
     }
}
Пример #2
0
static void RecursivePermute(string str, int k, setADT set)
{
    int i;

    if (k == StringLength(str)) {
        AddPtrElement(set, CopyString(str));
    } else {
        for (i = k; i < StringLength(str); i++) {
            ExchangeCharacters(str, k, i);
            RecursivePermute(str, k + 1, set);
            ExchangeCharacters(str, k, i);
        }
    }
}