POJ1007-DNA Sorting-ACM

简介: DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 83442   Accepted: 33584 Description One measure of ``unsort...
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 }

 


 

 

目录
相关文章
|
机器学习/深度学习 存储 缓存
Lecture 3:动态规划
Lecture 3:动态规划
114 1
|
存储 C++
【PAT甲级 - C++题解】1107 Social Clusters
【PAT甲级 - C++题解】1107 Social Clusters
59 0
AtCoder Beginner Contest 221 E - LEQ(组合数学 树状数组)
AtCoder Beginner Contest 221 E - LEQ(组合数学 树状数组)
152 0
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
152 0
2020 ICPC Asia Taipei-Hsinchu Site Programming Contest H. Optimization for UltraNet (二分+最小生成树+算贡献)
2020 ICPC Asia Taipei-Hsinchu Site Programming Contest H. Optimization for UltraNet (二分+最小生成树+算贡献)
126 0
AtCoder Beginner Contest 203(Sponsored by Panasonic) D.Pond(二分+二维前缀和)
AtCoder Beginner Contest 203(Sponsored by Panasonic) D.Pond(二分+二维前缀和)
88 0
AtCoder Beginner Contest 203 Pond(二分+二维前缀和)
大体思路: 二分,将原矩阵根据二分的值变成01矩阵,如果元素值> val 就变为1,否则0 对于k * k 的矩阵,统计区域内元素之和,如果 sum < ⌊k2 / 2⌋ + 1,意味着当前k * k矩阵的中位数小于x,而x是我们的答案(最小中位数), ①sum < ⌊k2 / 2⌋ + 1 情况下x取得太大,r = mid ②反之,x还可能取更小的,l = mid 但是需要注意下l的初始值,当取0 or 1的时候是会wa掉的:
241 0
AtCoder Beginner Contest 203 Pond(二分+二维前缀和)
Mad Scientist (纯模拟题)
Mad Scientist 题目描述 Farmer John’s cousin Ben happens to be a mad scientist. Normally, this creates a good bit of friction at family gatherings, but it can occasionally be helpful, especially when Farmer John finds himself facing unique and unusual problems with his cows.
138 0
|
算法 测试技术
lecture 2.1 简单算法
(一)while循环 1. Convert the following into code that uses a while loop.
1126 0