题意:求出给定串的全排列
解题思路:对于给定的字符串,对它sort以后进行求下一个排列即可 是UVA146的加强版,
解题代码:
1 // File Name: 10098.c 2 // Author: darkdream 3 // Created Time: 2013年07月11日 星期四 13时34分28秒 4 5 #include<stdio.h> 6 #include<string.h> 7 #include<stdlib.h> 8 #include<time.h> 9 #include<math.h> 10 #include<ctype.h> 11 int cmp(const void *a, const void *b) 12 { 13 return *(char *)a - *(char *)b; 14 } 15 int len; 16 char str[100] = {0}; 17 int dosomething(){ 18 19 int ok =0 ; 20 int siti = 0 ; 21 int sitj = 0 ; 22 for(int j = len -1; j >= 0 ;j --) 23 { 24 for(int i = len-1; i >= j ;i -- ) 25 { 26 27 if(str[j] < str[i]) 28 { 29 sitj = i ; 30 siti = j ; 31 ok = 1; 32 break; 33 } 34 } 35 if(ok) 36 break; 37 } 38 if(ok == 0 ) 39 { 40 return ok; 41 } 42 char temp = str[sitj]; 43 str[sitj] = str[siti]; 44 str[siti] = temp; 45 qsort(str+siti+1,len-1-siti,sizeof(char),cmp); 46 printf("%s ",str); 47 return ok; 48 } 49 int main(){ 50 51 //freopen("/home/plac/problem/input.txt","r",stdin); 52 //freopen("/home/plac/problem/output.txt","w",stdout); 53 int n; 54 scanf("%d",&n); 55 while(n--) 56 { 57 memset(str,0,sizeof(str)); 58 scanf("%s",str); 59 len = strlen(str); 60 qsort(str,len,sizeof(char),cmp); 61 printf("%s ",str); 62 while(dosomething()); 63 printf(" "); 64 } 65 return 0 ; 66 }