全部博文(930)
分类: LINUX
2009-07-16 15:19:30
题目描述:
给定n个字符串s[0..n-1],按照一定排列,首尾相连,连成一个长串。求出一种排列使得连成的长串是所有可能的长串中字典序最小的那个。打印出那种排列连成的长串。
没什么好说的就是原来的Int数组变成现在的字符数组排序而已,就用快速排序吧
#include
#include
#include
#define MAX 20
int quickpartition(char str[], int start, int end)
{
int i = start-1;
int j = start;
char tmp;
#if 0
int num = getrand(end-start+1);
tmp = str[num];
str[num]= str[end];
str[end] = tmp;
#endif
char x = str[end];
for(;j
if(str[j]<=x)
{
i++;
tmp = str[i];
str[i]= str[j];
str[j] = tmp;
}
}
tmp = str[i+1];
str[i+1]= str[end];
str[end] = tmp;
return i+1;
}
int quicksort(char str[], int start,int end)
{
int q = 0;
int i = 0;
if(start
q = quickpartition(str, start, end);
for(;i<=end;i++)
printf("%c\t",str[i]);
printf("\n");
quicksort(str, start, q-1);
quicksort(str, q+1, end);
}
}
int main(int argc, char *argv[])
{
char str[MAX];
srand((unsigned)time(NULL));
printf("please input the str(only a-z):\n");
scanf("%s",str);
printf("Your input is:%s\n",str);
quicksort(str, 0, strlen(str)-1);
//str[strlen(str)] = '\0';
printf("After process the str is:%s\n",str);
system("PAUSE");
return 0;
}