阅读背景:

Sicily 4495 Print permutations解题报告

来源:互联网 

先求出全排列,然后快速排序排个字典序,就搞定。

代码如下:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

const int MaxNum=9;
char iArr[MaxNum];
int count;
char result[40320][10];

void ksort(int l, int h, char a[][10] )
{ 
    int e = h;
    int p = l; 
    char temp[10];
    if (h < l + 2)
        return;
    while (l < h) { 
        while (++l < e && strcmp(a[l], a[p]) <= 0); 
        while (--h > p && strcmp(a[h], a[p]) >= 0); 
        if (l < h) {
            strcpy(temp, a[l]);
            strcpy(a[l], a[h]);
            strcpy(a[h], temp);
        } 
    } 
    strcpy(temp, a[h]);
    strcpy(a[h], a[p]);
    strcpy(a[p], temp);
    ksort(p, h, a); 
    ksort(l, e, a);
}

inline void Swap(char* a, char* b)
	{
		int temp;
		temp = *a;
		*a = *b;
		*b = temp;
	}
int FullArray(int k,int m)
{
	int i=0;
	if(k == m)
	{
		strcpy(result[::count], iArr);
		::count++;
		return 1;
	}
	for(i = k; i <= m; i++)
	{
		Swap(&iArr[k], &iArr[i]);
		FullArray(k+1, m);
		Swap(&iArr[i], &iArr[k]);
	}
	return 1;

}
int main()
 {   
	int n, i;
	scanf("%s", iArr);
	n = strlen(iArr);
	::count = 0;
	FullArray(0, n-1);
	ksort(0, ::count, result);
	for(i = 0; i < ::count; i++)
	{
       printf("%s\n", result[i]);      
    }
     return 0;
 }#include &l



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: