DNA Sorting

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2182    Accepted Submission(s): 1062

Problem 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.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

 
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 (1 < 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''. If two or more strings are equally sorted, list them in the same order they are in the input file.
 
Sample Input
1 10 6 AACATGAAGG TTTTGGCCAA TTTGGCCAAA GATCAGATTT CCCGGGGGGA ATCGATGCAT
 
Sample Output
CCCGGGGGGA AACATGAAGG GATCAGATTT ATCGATGCAT TTTTGGCCAA TTTGGCCAAA
 

思路:从小到大排列。。。不好说,看题;

代码:

 #include<stdio.h>
#include<algorithm>
using namespace std;
typedef struct{
int num;
char str[];
}node;
int cmp(node a,node b){
return a.num<b.num;
}
int search(int n,char *a){int flot=;
for(int i=;i<n;++i){
for(int j=i+;j<n;++j){
if(a[j]-a[i]<)flot++;
}
}
return flot;
}
int main(){
int m,n,T;
node dna[];
scanf("%d",&T);
while(T--){scanf("%d%d",&n,&m);
int i=;
for(int i=;i<m;++i){
scanf("%s",dna[i].str);
dna[i].num=search(n,dna[i].str);
}
sort(dna,dna+m,cmp);
for(int i=;i<m;++i)printf("%s\n",dna[i].str);
}
return ;
}

快排:

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
typedef struct{
int num;
char str[];
}node;
node dna[];
int search(int n,char *a){
int flot=;
for(int i=;i<n;++i){
for(int j=i+;j<n;++j){
if(a[j]-a[i]<)flot++;
}
}
return flot;
}
void Swap(node &a, node &b){
swap(a.num, b.num);
char s[];
strcpy(s, a.str);
strcpy(a.str, b.str);
strcpy(b.str, s);
}
int handle(int l, int r){
int j = l - ;
for(int i = l; i <= r; i++){
if(dna[i].num <= dna[r].num){
j++;
Swap(dna[i], dna[j]);
}
}
return j;
}
void quiksort(int l, int r){
int m;
if(l < r){
m = handle(l, r);
quiksort(l, m - );
quiksort(m + , r);
}
}
int main(){
int m,n,T; scanf("%d",&T);
while(T--){scanf("%d%d",&n,&m);
int i=;
for(int i=;i<m;++i){
scanf("%s",dna[i].str);
dna[i].num=search(n,dna[i].str);
}
quiksort(, m - );
for(int i=;i<m;++i)printf("%s\n",dna[i].str);
}
return ;
}

DNA Sorting(排序)的更多相关文章

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

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

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

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

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

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

  4. poj 1007 (nyoj 160) DNA Sorting

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

  5. poj 1007 DNA Sorting

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

  6. [POJ 1007] DNA Sorting C++解题

        DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 77786   Accepted: 31201 ...

  7. DNA Sorting POJ - 1007

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

  8. DNA Sorting

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 105159   Accepted: 42124 De ...

  9. 算法:POJ1007 DNA sorting

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

随机推荐

  1. MySQL查询随机数据的4种方法和性能对比

    从MySQL随机选取数据也是我们最常用的一种发发,其最简单的办法就是使用”ORDER BY RAND()”,本文介绍了包括ORDER BY RAND()的4种获取随机数据的方法,并分析了各自的优缺点. ...

  2. TabBarItem图片大小改变

    在TabBarItem设计的时候不需要title只要image的时候,如何将image居中显示. tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, ...

  3. ThinkPHP实现跨模块调用操作方法概述

    ThinkPHP实现跨模块调用操作方法概述 投稿:shichen2014 字体:[增加 减小] 类型:转载   使用 $this 可以调用当前模块内的方法,但是很多情况下经常会在当前模块中调用其他模块 ...

  4. IoC容器Autofac之实例优化(三)

    回顾之前的代码 //这个类的作用是筛选出MPG类型的电影 public class MPGMovieLister { public Movie[] GetMPG() { var finder = Mo ...

  5. NET基础课--开发工具实用功能

    1.浏览代码结构 类视图 2.重构功能 提取长的的方法体中的部分方法到单独函数中 路径:选择代码段,右击重构----提取方法 3.代码结构 a 代码对齐 点[编辑]-[高级]-[设置选定内容的格式] ...

  6. jquery文本框验证字符长度和只能输入数字

    <input type="text" class="chujia" onkeyup="this.value=this.value.replace ...

  7. js静态方法

    1.ajax() 方法是属于“函数”本身的,和返回的对象没有关系 2.bark药调用,必须药new Hashiqi()得到对象,且由返回对象才能调用 3.ajax()方法药调用,不需要new对象,直接 ...

  8. 返回某个界面——nav

     NSInteger index=[[self.navigationController viewControllers]indexOfObject:self];  [self.navigationC ...

  9. [Head First Python]4. summary

    1- strip()方法可以从字符串去除不想要的空白符 (role, line_spoken) = each_line.split(":", 1) line_spoken = li ...

  10. DOM和jQuery

    一.DOM 在学习DOM之前你应该已经具备了以下三个知识点的应用:HTML CSS javascript DOM 是 W3C(万维网联盟)的标准. W3C DOM 标准被分为 3 个不同的部分: 核心 ...