1、
例子:‘1234’
1-234
2-134
3-214
4-231
保证不重复,再后面全排列完成后,交换,保持原来的‘1234’
全排列就是从第一个数字起每个数分别与它后面的数字交换。
#include<iostream>
using namespace std;
#include<assert.h>
void Swap(char *a , char *b)
{
char t;
t=*a;
*a=*b;
*b=t;
}
void Permutation_o(char* pStr, char* pBegin)
{
if (*pBegin == '\0' )
cout << pStr <<endl;
else
{
for (char* pCh = pBegin; *pCh!='\0'; pCh++)
{
Swap(pCh,pBegin);
Permutation_o(pStr, pBegin+1);
Swap(pCh,pBegin);
}
}
}
void Permutation_o(char* pStr)
{
if (pStr==NULL)
return;
Permutation_o(pStr,pStr);
}
int main(void)
{
char str[10] ;
cout <<"input the string: ";
cin>> str;
cout <<"the string's permutation lists:"<<endl;
Permutation_o(str);
return 0;
}
2、重复字符
带重复字符的全排列就是每个字符分别于它后面非重复出现的字符交换。
即:第i个字符与第j个字符交换时,要求[i,j)中没有与第j个字符相等的数。
#include<iostream>
using namespace std;
#include<assert.h>
void Swap(char *a , char *b)
{
char t;
t=*a;
*a=*b;
*b=t;
}
bool IsSwap(char* pBegin,char *pEnd)
{
char *p;
for(p=pBegin; p<pEnd; p++)
{
if(*p == *pEnd)
return false;
}
return true;
}
void Permutation_o(char* pStr, char* pBegin)
{
if (*pBegin == '\0' )
cout << pStr <<endl;
else
{
for (char* pCh = pBegin; *pCh!='\0'; pCh++)
{
if(IsSwap(pBegin,pCh))
{
Swap(pCh,pBegin);
Permutation_o(pStr, pBegin+1);
Swap(pCh,pBegin);
}
}
}
}
void Permutation_o(char* pStr)
{
if (pStr==NULL)
return;
Permutation_o(pStr,pStr);
}
int main(void)
{
char str[10] ;
cout <<"input the string: ";
cin>> str;
cout <<"the string's permutation lists:"<<endl;
Permutation_o(str);
return 0;
}
3、非递归实现(字典排序)
起点:12345
终点:54321
过程:21543下一位值:
找到最后一个升序位置1
从后向前找比替换点大的第一个数(543已经为降序排序)
交换13
翻转541
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#include<assert.h>
#include<stdio.h>
//反转区间
void Reverse(char* pBegin , char* pEnd)
{
while(pBegin < pEnd)
swap(*pBegin++ , *pEnd--);
}
//下一个排列
bool Next_permutation(char *str)
{
char *p=str;
char *pend=str;
char *q=str;
char* pFind=str;
while(*pend !='\0')
pend++;
p=pend-1;
while(p != str)
{
q = p;
p--;
if(*p < *q) //找降序的相邻2数,前一个数即替换数
{
//从后向前找比替换点大的第一个数
pFind = pend-1;
while(*pFind < *p)
pFind--;
swap(*p , *pFind);
//替换点后的数全部反转
Reverse(q , pend-1);
return true;
}
}
Reverse(str , pend); //如果没有下一个排列,全部反转后返回false
return false;
}
int cmp(const void *a,const void *b)
{
return int(*(char *)a - *(char *)b);
}
int main(void)
{
char str[] = "abc";
int num = 1;
qsort(str , strlen(str),sizeof(char),cmp);
do
{
printf("第%d个排列\t%s\n",num++,str);
}while(Next_permutation(str));
return 0;
}
http://v.ku6.com/show/RP7r6vew4Qb_MCF1eYZeOg…html
http://blog.youkuaiyun.com/hackbuteer1/article/details/7462447
http://blog.youkuaiyun.com/zhaojinjia/article/details/9320475
http://blog.youkuaiyun.com/wuzhekai1985/article/details/6643127
http://www.myexception.cn/program/1442108.html