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. php获取客户端ip get_client_ip()

    php获取客户端ip get_client_ip() function get_client_ip(){if (getenv("HTTP_CLIENT_IP") && ...

  2. [转]Ubuntu上的包管理:dpkg,apt和aptitude

    一直以来对于ubuntu的包管理的概念就是apt-get,偶尔手动装个包就是dpkg -i,现在觉得是要系统地了解一下这几个包管理的命令. 原文转自: http://zhouliang.pro/201 ...

  3. 【Android - MD】之NavigationView的使用

    NavigationView是Android 5.0新特性--Material Design中的一个布局控件,可以结合DrawerLayout使用,让侧滑菜单变得更加美观(可以添加头部布局). Nav ...

  4. Android 官方命令深入分析

    原文:www.libgdx.cn Android SDK包括了多种工具来帮助你创建基于Android平台的移动应用.这些工具一般分成两类:SDK 工具和 platform 工具. SDK 工具是独立的 ...

  5. cocos2d-x 3.0 final 中文显示

    cocos2d-x 3.0的中文显示非常easy,首先,你须要一个xml文件保存中文,还须要一个能显示中文的TTF文件 <?xml version="1.0" encodin ...

  6. grep:Binary file (standard input) matches

    grep "key" xxx.log时输出 Binary file xxx.log matches 百度了一下:grep觉得这是二进制文件.解决方式:grep -a. grep - ...

  7. python第三方库推荐 - 通过ntplib在windows上同步时间

    很多时候我们有通过程序脚本同步校正北京时间的需求. 在linux上同步时间比较方便,安装个ntpdate软件就行了. 但是在windows的要同步时间比较麻烦. 这时想到的就是从网络获取一个准确的时间 ...

  8. [CSS] Animating SVG

    <!DOCTYPE> <html lang='en'> <head> <meta charset='utf-8'> <title>Cospl ...

  9. android中listview的一些样式设置

    在Android中,ListView是最常用的一个控件,在做UI设计的时候,很多人希望能够改变一下它的背景,使他能够符合整体的UI设计,改变背景背很简单只需要准备一张图片然后指定属性 android: ...

  10. [转] Spring Boot and React hot loader

    When I develop web applications, I love using React. I'm also a Spring and groovy addict. Those two ...