标签:nal memory ast letters oid des for mit else
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 83442 | Accepted: 33584 |
Description
Input
Output
Sample Input
10 6 AACATGAAGG TTTTGGCCAA TTTGGCCAAA GATCAGATTT CCCGGGGGGA ATCGATGCAT
Sample Output
CCCGGGGGGA AACATGAAGG GATCAGATTT ATCGATGCAT TTTTGGCCAA TTTGGCCAAA
重点,归并排序求逆序数
1 #include <stdio.h> 2 #include <malloc.h> 3 #include <string.h> 4 #define MAXN 256 5 char a[MAXN]; 6 char c[MAXN]; 7 int cnt=0; 8 void MergeSort(int l, int r){ 9 int mid, i, j, tmp; 10 if( r > l+1 ){ 11 mid = (l+r)/2; 12 MergeSort(l, mid); 13 MergeSort(mid, r); 14 tmp = l; 15 for( i=l, j=mid; i < mid && j < r; ){ 16 if( a[i] > a[j] ){ 17 c[tmp++] = a[j++]; 18 cnt += mid-i; // 19 } 20 else c[tmp++] = a[i++]; 21 } 22 if( j < r ) for( ; j < r; ++j ) c[tmp++] = a[j]; 23 else for( ; i < mid; ++i ) c[tmp++] = a[i]; 24 for ( i=l; i < r; ++i ) a[i] = c[i]; 25 } 26 } 27 int main(void){ 28 int n,m; 29 scanf("%d%d",&n,&m); 30 char ** strs = (char **)malloc(m*sizeof(char*)); 31 int * cnts = (int *)malloc(m*sizeof(int)); 32 int i; 33 for(i = 0;i<m;i++){ 34 strs[i] = (char *)malloc((n+1)*sizeof(char)); 35 scanf("%s",strs[i]); 36 //printf("%s\n",strs[i]); 37 cnt = 0; 38 strcpy(a,strs[i]); 39 //printf("%s\n",a); 40 MergeSort(0,n); 41 //printf("%d\n",cnt); 42 cnts[i] = cnt; 43 } 44 45 for(i = 0;i<m-1;i++){ 46 int j,p=i; 47 for(j = i+1;j<m;j++){ 48 if(cnts[p]>cnts[j]){ 49 p = j; 50 } 51 } 52 if(p!=i){ 53 int tmp = cnts[p]; 54 cnts[p] = cnts[i]; 55 cnts[i] = tmp; 56 char * str = strs[p]; 57 strs[p] = strs[i]; 58 strs[i] = str; 59 } 60 } 61 for(i = 0;i<m;i++){ 62 printf("%s\n",strs[i]); 63 free(strs[i]); 64 } 65 free(strs); 66 free(cnts); 67 return 0; 68 }
标签:nal memory ast letters oid des for mit else
原文地址:http://www.cnblogs.com/zhaohongtian/p/6809030.html