点击打开链接

DNA Sorting
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 75164   Accepted: 30115

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

把每个序列的逆序数求出来然后根据逆序数稳定排序后升序输出,注意qsort中cmp函数的使用,当a == b时返回0则不处理两个数

#include<iostream>
#include<stdlib.h>
#include<algorithm>
#include<string>
using namespace std;
typedef struct NODE
{
int num;
string str;
}Node; int comp(const void* a,const void* b)
{
Node* x=(Node*)a;
Node* y=(Node*)b;
return (x->num)-(y->num);
}
int main()
{
int n, m;
Node array[101];
while(cin>>n>>m)
{
int i;
for(i = 0; i < m; i++)
{
Node node;
cin>>node.str;
int j;
int count = 0;
int len = node.str.length();
for(j = 0; j < len - 1; j++)
{
int k;
if(node.str[j] == 'A')
continue;
for(k = j + 1; k < len; k++)
{
if(node.str[j] > node.str[k])
count ++;
}
}
node.num = count;
array[i] = node;
}
qsort(array, m, sizeof(Node), comp);
for(i = 0; i < m; i++)
cout<<array[i].str<<endl; }
return 0;
}

poj 1007 (nyoj 160) DNA Sorting的更多相关文章

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

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

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

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

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

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

  4. poj 1007 DNA Sorting

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

  5. DNA Sorting POJ - 1007

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

  6. poj 1007 DNA sorting (qsort)

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 95209   Accepted: 38311 Des ...

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

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

  8. 算法:POJ1007 DNA sorting

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

  9. [POJ1007]DNA Sorting

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

随机推荐

  1. 重新加载maven项目的依赖项

    最近在调试reportNG,测试允许完以后,报告总是使用的testNG的格式,并且只有index和overview两个文件. 找了好多帖子,大家都是那么设置的都没有问题,难道是哥人品不好?错! 大家基 ...

  2. PYTHON错误代码及解决办法

    (1)用sklearn进行逻辑回归时,建立完模型,由于要预测的数据量很大,无法一次全部预测,只能每次预测一个样本数据, 在每次以列表形式输入数据进行预测时出现: /Users/donganlan/an ...

  3. ASP.NET MVC 控制器向View传值的三种方法

    转自:http://www.cnblogs.com/shinima/p/3940452.html 1.提供视图模型对象 你能把一个对象作为View方法的参数传递给视图. public ViewResu ...

  4. 编译工程时报illegal character:\65279--转

    windows对UTF-8文件进行了特殊处理,对UTF-8的文本文件自动在前面加了三个byte(EF BB BF),javac编译时,读到最前面这三个byte时报错 illegal character ...

  5. 配置Windows Server2008+iis+php+mysql所需下载安装包

    最近一个朋友让我帮忙给配置服务器iis+php+mysq 环境,遇到了很多问题,特此就在这里说一下.小弟只是在windwos2003 和windwos XP下配置过iis+php+mysql,去朋友那 ...

  6. MVC4 WebAPI(二)——Web API工作方式

    http://www.cnblogs.com/wk1234/archive/2012/05/07/2486872.html 在上篇文章中和大家一起学习了建立基本的WebAPI应用,立刻就有人想到了一些 ...

  7. js为DIV动态设置id属性

    <script> var objs=document.getElementById("iproduct").getElementsByTagName("div ...

  8. (转)[SQL Server] 动态sql给变量赋值(或返回值给变量)

    本文转载自:http://blog.csdn.net/xiaoxu0123/article/details/5684680 [SQL Server] 动态sql给变量赋值(或返回值给变量) decla ...

  9. 【JdbcTemplete】JdbcTemplete代码详解--模板方法详解

    JdbcTemplete类层次结构 JdbcAccessor:对DataSource数据源进行管理和配置: JdbcOperations:定义了通过JDBC操作数据库的基本操作方法: JdbcTemp ...

  10. 查看CentOS版本方法

    查看内核版本 这个命令适用于所有的linux,包括Redhat.SuSE.Debian.Centos等发行版. root@MyMail ~ # uname Linux root@MyMail ~ # ...