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

重点,归并排序求逆序数

 #include <stdio.h>
#include <malloc.h>
#include <string.h>
#define MAXN 256
char a[MAXN];
char c[MAXN];
int cnt=;
void MergeSort(int l, int r){
int mid, i, j, tmp;
if( r > l+ ){
mid = (l+r)/;
MergeSort(l, mid);
MergeSort(mid, r);
tmp = l;
for( i=l, j=mid; i < mid && j < r; ){
if( a[i] > a[j] ){
c[tmp++] = a[j++];
cnt += mid-i; //
}
else c[tmp++] = a[i++];
}
if( j < r ) for( ; j < r; ++j ) c[tmp++] = a[j];
else for( ; i < mid; ++i ) c[tmp++] = a[i];
for ( i=l; i < r; ++i ) a[i] = c[i];
}
}
int main(void){
int n,m;
scanf("%d%d",&n,&m);
char ** strs = (char **)malloc(m*sizeof(char*));
int * cnts = (int *)malloc(m*sizeof(int));
int i;
for(i = ;i<m;i++){
strs[i] = (char *)malloc((n+)*sizeof(char));
scanf("%s",strs[i]);
//printf("%s\n",strs[i]);
cnt = ;
strcpy(a,strs[i]);
//printf("%s\n",a);
MergeSort(,n);
//printf("%d\n",cnt);
cnts[i] = cnt;
} for(i = ;i<m-;i++){
int j,p=i;
for(j = i+;j<m;j++){
if(cnts[p]>cnts[j]){
p = j;
}
}
if(p!=i){
int tmp = cnts[p];
cnts[p] = cnts[i];
cnts[i] = tmp;
char * str = strs[p];
strs[p] = strs[i];
strs[i] = str;
}
}
for(i = ;i<m;i++){
printf("%s\n",strs[i]);
free(strs[i]);
}
free(strs);
free(cnts);
return ;
}

POJ1007-DNA Sorting-ACM的更多相关文章

  1. [POJ1007]DNA Sorting

    [POJ1007]DNA Sorting 试题描述 One measure of ``unsortedness'' in a sequence is the number of pairs of en ...

  2. 算法:POJ1007 DNA sorting

    这题比较简单,重点应该在如何减少循环次数. package practice; import java.io.BufferedInputStream; import java.util.Map; im ...

  3. poj1007——DNA Sorting

    Description One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are ...

  4. DNA Sorting POJ - 1007

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 114211   Accepted: 45704 De ...

  5. poj 1007:DNA Sorting(水题,字符串逆序数排序)

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 80832   Accepted: 32533 Des ...

  6. DNA Sorting 分类: POJ 2015-06-23 20:24 9人阅读 评论(0) 收藏

    DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 88690 Accepted: 35644 Descrip ...

  7. poj 1007 (nyoj 160) DNA Sorting

    点击打开链接 DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 75164   Accepted: 30 ...

  8. [POJ] #1007# DNA Sorting : 桶排序

    一. 题目 DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 95052   Accepted: 382 ...

  9. poj 1007 DNA Sorting

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 95437   Accepted: 38399 Des ...

  10. DNA Sorting(排序)

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) DNA Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

随机推荐

  1. [JIT_APP]Android SQLite简介

    SQLite介绍 SQLite是一个非常流行的嵌入式数据库,它支持SQL语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目(Mozilla, PHP, Pyt ...

  2. 《A First Course in Probability》-chaper8-极限定理-弱大数定理

    基于之前强大数定理的得证,这里我们再结合切比雪夫不等式,能够得到弱大数定理. 弱大数定理: 表面上,强大数定理和弱大数定理好像是质同的,但是他们之间真正的区别到底是什么呢?

  3. Java宝典

    本人最近参加了几家公司的面试,在其中发现了不少笔试题,虽然是平常再简单不过的,但一不小心还是会出错.今天特意找时间写下来和大家分享. 1.访问控制符权限问题.   同一个包中 同一个类中 不同包的子类 ...

  4. 【转】jQuery列表拖动排列-jquery list dragsort插件参数和使用方法

    转自:http://www.itokit.com/2014/0820/75058.html 我们在编辑页面元素排序的时候,我推荐使用jquery插件:dragsort. dragsort官网地址:ht ...

  5. 如何将ER图转换成关系模式集

    在ER图中,主要是实体类型和联系类型. 1.实体类型的转换 (“——”表示对应关系) 实体类型——关系模式 实体的属性——关系模式的属性 实体标识符——关系模式的键 2.联系的转换 一元联系较简单,三 ...

  6. 转自知乎,亲民好酒推荐 分类: fool_tree的笔记本 2014-11-08 17:37 652人阅读 评论(0) 收藏

    这里尽量为大家推荐一些符合大众喜好.业内公认好评."即使你不喜欢,你也会承认它不错"的酒款.而且介绍到的酒款还会有一个共同的特征,就是能让你方便的在网上买到. 大概会分为烈酒,利口 ...

  7. 史上最简单的带流控功能的http server

    s.py import time import SimpleHTTPServer import SocketServer BYTES_PER_SECOND=160*1024 class MyHTTPR ...

  8. web.xml(7)_mime-mapping、welcome-file-list、error-page

    10.mime-mapping:mime-mapping包括两个子元素extension和mime-type.定义某一个扩展名和某一MIME Type做对映. MIME(Multipurpose In ...

  9. [转] Immutable 详解及 React 中实践

    https://zhuanlan.zhihu.com/p/20295971 作者:camsong链接:https://zhuanlan.zhihu.com/p/20295971来源:知乎著作权归作者所 ...

  10. Android内存优化之——static使用篇(使用MAT工具进行分析)

    这篇文章主要配套与Android内存优化之——static使用篇向大家介绍MAT工具的使用,我们分析的内存泄漏程序是上一篇文章中static的使用内存泄漏的比较不容易发现泄漏的第二情况和第三种情况—— ...