String 字符串排列中的两次交换?

String 字符串排列中的两次交换?,string,recursion,permutation,String,Recursion,Permutation,有人能确切地解释为什么我们需要在查找字符串的所有排列的算法中撤消交换吗 swap((a+i), (a+j)); // Make a[j] the start of this (sub-)permutation starting at i. permute(a, i+1, n); // Find the permuations of a[i+1..n] - and undo them. swap((a+i), (a+j)); // Undo the swap of a[i] and a[j]

有人能确切地解释为什么我们需要在查找字符串的所有排列的算法中撤消交换吗

swap((a+i), (a+j));  // Make a[j] the start of this (sub-)permutation starting at i.
permute(a, i+1, n);  // Find the permuations of a[i+1..n] - and undo them.
swap((a+i), (a+j));  // Undo the swap of a[i] and a[j].

这是什么编程语言?你想做什么呢?发布完整的算法,因为数组“a”是全局的,每次调用permute都会修改它。更好的理解方法是举一个4个字符的小例子,尝试使用&不使用最后一行。你可能会发现这是一个很好的参考。特别是第7.1.2.1节。也