字符串的全排列

来源:互联网 发布:人工智能利弊 编辑:程序博客网 时间:2024/05/17 08:00

关于字符串的全排列的算法

其中IsSwap()是用来剔除会导致重复的排列的情况的!

void Permutation(char* pStr){if(pStr==NULL)return;PermutationCore(pStr,pStr);}bool IsSwap(char* Begin,char*End ){while(Begin!=End){if(*Begin==*End)return false;Begin++;}return true;}void PermutationCore(char* pStr,char* pBegin){if(*pBegin=='\0'){printf("%s\n",pStr);}else{for(char* pCh=pBegin;*pCh!='\0';++pCh){if(IsSwap(pBegin,pCh)){Swap(pCh,pBegin);     PermutationCore(pStr,pBegin+1);     Swap(pCh,pBegin);}}}}

0 0
原创粉丝点击