您的位置:首页 > 其它

Sicily 4495 Print permutations解题报告

2012-04-30 02:09 369 查看
先求出全排列,然后快速排序排个字典序,就搞定。

代码如下:

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: