#include <stdio.h>
/*
我们以三个字符abc为例来分析一下求字符串排列的过程。
首先我们固定第一个字符a,求后面两个字符bc的排列。
当两个字符bc的排列求好之后,我们把第一个字符a和后面的b交换,得到bac,
接着我们固定第一个字符b,求后面两个字符ac的排列。
现在是把c放到第一位置的时候了。记住前面我们已经把原先的第一个字符a和后面的b做了交换,
为了保证这次c仍然是和原先处在第一位置的a交换,我们在拿c和第一个字符交换之前,
先要把b和a交换回来。在交换b和a之后,再拿c和处在第一位置的a进行交换,得到cba。
我们再次固定第一个字符c,求后面两个字符b、a的排列。
既然我们已经知道怎么求三个字符的排列,
那么固定第一个字符之后求后面两个字符的排列,就是典型的递归思路了。
基于前面的分析,我们可以得到如下的参考代码:
*/
void Permutation(char* pStr, char* pBegin);
void Permutation(char* pStr);
void swap(char *a,char* b);
void main()
{
char Str[]="abc";
//Permutation(Str);
Permutation(Str, Str);
printf("\n----------------\n %s \n",Str);
}
/*
abc
acb
bac
bca
cba
cab
----------------
abc
请按任意键继续. . .
*/
/////////////////////////////////////////////////////////////////////////
// Get the permutation of a string,
// for example, input string abc, its permutation is
// abc acb bac bca cba cab
/////////////////////////////////////////////////////////////////////////
void Permutation(char* pStr)
{
Permutation(pStr, pStr);
}
/////////////////////////////////////////////////////////////////////////
// Print the permutation of a string,
// Input: pStr - input string
// pBegin - points to the begin char of string
// which we want to permutate in this recursion
/////////////////////////////////////////////////////////////////////////
void Permutation(char* pStr, char* pBegin)
{
printf("pStr[%s],pBegin[%c]\n",pStr,(*pBegin =='\0')?'0':(*pBegin));
if(!pStr || !pBegin)
return;
// if pBegin points to the end of string,
// this round of permutation is finished,
// print the permuted string
if(*pBegin == '\0')
{
printf("%s\n", pStr);
return;
}
// otherwise, permute string
else
{
for(char* pCh = pBegin; *pCh != '\0'; ++ pCh)
{
// swap pCh and pBegin
swap(pCh,pBegin);
printf("swap pCh[%c] and pBegin[%c] => pStr[%s]\n",*pCh,*pBegin,pStr);
Permutation(pStr, pBegin + 1);
// restore pCh and pBegin
swap(pCh,pBegin);
printf("restore pCh[%c] and pBegin[%c] => pStr[%s]\n",*pCh,*pBegin,pStr);
}
}
}
void swap(char *a,char* b)
{
char tmp=*a;
*a=*b;
*b=tmp;
}
pStr[abc],pBegin[a]
swap pCh[a] and pBegin[a] => pStr[abc]
pStr[abc],pBegin[b]
swap pCh[b] and pBegin[b] => pStr[abc]
pStr[abc],pBegin[c]
swap pCh[c] and pBegin[c] => pStr[abc]
pStr[abc],pBegin[0]
abc
restore pCh[c] and pBegin[c] => pStr[abc]
restore pCh[b] and pBegin[b] => pStr[abc]
swap pCh[b] and pBegin[c] => pStr[acb]
pStr[acb],pBegin[b]
swap pCh[b] and pBegin[b] => pStr[acb]
pStr[acb],pBegin[0]
acb
restore pCh[b] and pBegin[b] => pStr[acb]
restore pCh[c] and pBegin[b] => pStr[abc]
restore pCh[a] and pBegin[a] => pStr[abc]
swap pCh[a] and pBegin[b] => pStr[bac]
pStr[bac],pBegin[a]
swap pCh[a] and pBegin[a] => pStr[bac]
pStr[bac],pBegin[c]
swap pCh[c] and pBegin[c] => pStr[bac]
pStr[bac],pBegin[0]
bac
restore pCh[c] and pBegin[c] => pStr[bac]
restore pCh[a] and pBegin[a] => pStr[bac]
swap pCh[a] and pBegin[c] => pStr[bca]
pStr[bca],pBegin[a]
swap pCh[a] and pBegin[a] => pStr[bca]
pStr[bca],pBegin[0]
bca
restore pCh[a] and pBegin[a] => pStr[bca]
restore pCh[c] and pBegin[a] => pStr[bac]
restore pCh[b] and pBegin[a] => pStr[abc]
swap pCh[a] and pBegin[c] => pStr[cba]
pStr[cba],pBegin[b]
swap pCh[b] and pBegin[b] => pStr[cba]
pStr[cba],pBegin[a]
swap pCh[a] and pBegin[a] => pStr[cba]
pStr[cba],pBegin[0]
cba
restore pCh[a] and pBegin[a] => pStr[cba]
restore pCh[b] and pBegin[b] => pStr[cba]
swap pCh[b] and pBegin[a] => pStr[cab]
pStr[cab],pBegin[b]
swap pCh[b] and pBegin[b] => pStr[cab]
pStr[cab],pBegin[0]
cab
restore pCh[b] and pBegin[b] => pStr[cab]
restore pCh[a] and pBegin[b] => pStr[cba]
restore pCh[c] and pBegin[a] => pStr[abc]
----------------
abc