码迷,mamicode.com
首页 > 其他好文 > 详细

POJ1007

时间:2017-05-04 20:13:04      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:nal   memory   ast   letters   oid   des   for   mit   else   

DNA Sorting
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 83442   Accepted: 33584

Description

One measure of ``unsortedness‘‘ in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC‘‘, this measure is 5, since D is greater than four letters to its right and E is greater than one letter to its right. This measure is called the number of inversions in the sequence. The sequence ``AACEDGG‘‘ has only one inversion (E and D)---it is nearly sorted---while the sequence ``ZWQM‘‘ has 6 inversions (it is as unsorted as can be---exactly the reverse of sorted). 

You are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but rather in order of ``sortedness‘‘, from ``most sorted‘‘ to ``least sorted‘‘. All the strings are of the same length. 

Input

The first line contains two integers: a positive integer n (0 < n <= 50) giving the length of the strings; and a positive integer m (0 < m <= 100) giving the number of strings. These are followed by m lines, each containing a string of length n.

Output

Output the list of input strings, arranged from ``most sorted‘‘ to ``least sorted‘‘. Since two strings can be equally sorted, then output them according to the orginal order.

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 }

 


 

 

POJ1007

标签:nal   memory   ast   letters   oid   des   for   mit   else   

原文地址:http://www.cnblogs.com/zhaohongtian/p/6809030.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!