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. Django中的Ajax

    Ajax 很多时候,我们在网页上请求操作时,不需要刷新页面.实现这种功能的技术就要Ajax!(本人定义,不可迷信) jQuery中的ajax就可以实现不刷新页面就能向后台请求或提交数据的功能,我们仍然 ...

  2. 2014年河南省第七届ACM大赛总结

    虽然大赛已经结束了两天,不过比赛的场景还是不断地在眼前回放,一遍遍,这次的比赛给了我很深刻的感悟还有教训. 刚开始比赛选择了贩卖武器那道题,也是全场到最后唯一没有被人做出来的一道题,策略的严重错误,大 ...

  3. Xcode7.1与iOS9之坑

    一.更改http为https 两种方案: 公司后台服务器更改; 作为开发者,可在Xcode暂时退回到http协议.  开发者更改方法如下: 在Info.plist中添加App Transport Se ...

  4. git常用操作指令

    git操作master : 默认开发分支:origin : 默认远程版本库 添加远程仓库:git remote add [name] [url]  通常name为origin 克隆远程仓库:git c ...

  5. Javac编译和JIT编译

    编译过程 不论是物理机还是虚拟机,大部分的程序代码从开始编译到最终转化成物理机的目标代码或虚拟机能执行的指令集之前,都会按照如下图所示的各个步骤进行: 其中绿色的模块可以选择性实现.很容易看出,上图中 ...

  6. linux 设置 jdk环境变量

    1.安装jdk  tar zxvf jdk-8u112-linux-i586.tar.gz 2.设置环境变量:vi /etc/profile 输入i加入内容如下:export JAVA_HOME=/u ...

  7. java 启用新线程异步调用

    2秒后执行 某段代码: demo如下: System.out.println("正常执行...111"); Thread t = new Thread(){ public void ...

  8. Visual Studio 调试技巧 -- 为 Lambda 表达式设置中断

    如果我说 .NET 世界上最厉害的开发平台(语言),资深的同仁肯定不会往下看了,因为这将是一个无休止的争论,到头来搞不好还是人身攻击.然而,如果我说 Visual Studio 是世界上最友好最强大的 ...

  9. [转]Android NDK几点回调方式

    一.NDK中获取android设备ID的方式 Java代码如下(获取设备ANDROID_ID): final String androidId = Secure.getString(context.g ...

  10. Junit简介和常用API

    测试几个的概念 白盒测试——把测试对象看作一个打开的盒子,程序内部的逻辑结构和其他信息对测试人员是公开的. 回归测试——软件或环境的修复或更正后的“再测试”,自动测试工具对这类测试尤其有用. 单元测试 ...