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. bzoj 2741 分块+可持久化trie

    多个询问l,r,求所有子区间异或和中最大是多少 强制在线 做法: 分块+可持久化trie 1.对于每块的左端点i,预处理出i到任意一个j,()i,j)间所有子区间异或和中最大为多少,复杂度O(\(n\ ...

  2. dubbo 运行过程

    Overview Architecture Provider: 暴露服务的服务提供方. Consumer: 调用远程服务的服务消费方. Registry: 服务注册与发现的注册中心. Monitor: ...

  3. Windows Media Player axWindowsMediaPlayer1 分类: C# 2014-07-28 12:04 195人阅读 评论(0) 收藏

    属性/方法名: 说明: [基本属性] URL:String; 指定媒体位置,本机或网络地址 uiMode:String; 播放器界面模式,可为Full, Mini, None, Invisible p ...

  4. OpenCV 2.4.9

    2014.4.25 感谢那些參加开发.发送错误报告以及通过其它方式帮助我们的全部人和公司. 源代码如今已经可以从SourceForge和Github上下载了. 2.4.9版本号的帮助文章也更新到如今的 ...

  5. HTML5事件——contextmenu 隐藏鼠标右键菜单

    在window中单击右键或在Mac中Ctrl+单击时会触发contextmenu事件,通过取消其默认动作能够提供自己定义菜单. 首先先写一个自己的菜单: <style> ul, li { ...

  6. Thread Runnable 多线程

    1. 实现多线程的两种方法         a) 让这个类继承java.lang.Thread,然后重写run方法         b) 让这个类实现 java.lang.Runnable接口,实现r ...

  7. date和long的相互转换

    import java.text.SimpleDateFormat; import java.util.Date; public class T { public static void main(S ...

  8. Android中Cursor类的概念和用法

    http://blog.sina.com.cn/s/blog_618199e60101fskp.html 使用过 SQLite数据库的童鞋对 Cursor 应该不陌生,加深自己和大家对Android ...

  9. Redis Admin UI

    https://github.com/ServiceStackApps/RedisAdminUI 最近的v4版本不能用,需要下载v3版本,下载地址 https://github.com/Service ...

  10. oracle安装遇到的问题

    这两天要做一个项目,教师招聘系统.要用oracle.就安装了oracle 12c,安装的过程中遇到了一些问题,最后自己解决了.我是win7系统. 第一个报错: [INS-30131]执行安装程序验证所 ...