DNA Sorting
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 83069 Accepted: 33428 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

解法一:逆序数+快排(对cmp的完美诠释,原谅我刚刚学)+结构体(第一次在OJ用结构体)——

由于这道题目的数据量不算太大,直接用朴素的求逆序数绝对可以;
逆序数就是看这个数前面有多少个数比当前的数大,这里直接用了一个二重循环;

    #include <iostream>
#include <algorithm>
#include <string>
using namespace std; typedef struct f{
int num;
string w;
}data;
bool cmp( data a, data b ){
return a.num < b.num;
}
int main(){
int i, len, n, j, k;
cin>>len>>n;
data *s = new data[n];
for(i = ; i < n; i++){
s[i].num = ;
cin>>s[i].w;
for(j = ; j < len; j++)
for(k = ; k < j; k++)
if(s[i].w[j] < s[i].w[k])//求逆序数
s[i].num++;
}
sort(s, s+n, cmp);
for(i = ; i < n; i++)
cout<<s[i].w<<endl;
return ;
}

解法二:这里进行求逆序数的有一种方法很巧妙,因为题目中只有4个字母,所以就用到了这种特殊性,我们可以得到O(n)求逆序数的方法;

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
char s[];//储存DNA序列
int sum;//储存每个DNA序列的逆序数
}a[];
bool cmp(node x,node y)//比较函数
{
return x.sum<y.sum;
}
int count_inver(char *str, int len)//求逆序数
{
int i;
int cnt = ;
int a[] = {};//用一个数组个保存字母出现的次数
for(i = len - ; i >= ; i--) {
switch (str[i]) {
case 'A':
a[]++;
a[]++;
a[]++;
break;
case 'C':
a[]++;
a[]++;
cnt += a[];
break;
case 'G':
a[]++;
cnt += a[];
break;
case 'T':
cnt += a[];
}
}
return cnt;
}
int main()
{
int m,n,i,j;
scanf("%d%d",&n,&m);
for(i=;i<m;i++)
{
scanf("%s",a[i].s);
a[i].sum=count_inver(a[i].s,n);
}
sort(a,a+m,cmp);
for(j=;j<m;j++)
printf("%s\n",a[j].s);
}

POJ 1007的更多相关文章

  1. poj 1007 DNA Sorting 解题报告

    题目链接:http://poj.org/problem?id=1007 本题属于字符串排序问题.思路很简单,把每行的字符串和该行字符串统计出的字母逆序的总和看成一个结构体.最后把全部行按照这个总和从小 ...

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

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

  3. Mathematics:DNA Sorting(POJ 1007)

    DNA排序 题目大意:给定多个ACGT序列,按照字母顺序算出逆序数,按逆序数从小到大排列 这题其实很简单,我们只要用一个归并排序算逆序数,然后快排就可以了(插入排序也可以,数据量不大),但是要注意的是 ...

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

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

  5. poj 1007 (nyoj 160) DNA Sorting

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

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

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

  7. poj 1007 DNA Sorting

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

  8. poj 1007 纯水题 排序

    #include<stdio.h> #include<string.h> #include<algorithm> #include<stdlib.h> ...

  9. POJ 1007 DNA Sorting(sort函数的使用)

    Description One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are ...

随机推荐

  1. centos6.5安装配置supervisor

    1.下载并安装supervisor https://pypi.python.org/pypi/supervisor/3.2.0 .tar.gz cd supervisor- python setup. ...

  2. mysql创建数据表时如何判断是否已经存在?

    >>> create table if not exists people(name text,age int(2),gender char(1)); 如上代码表示创建一个名为peo ...

  3. 跨站的艺术-XSS Fuzzing 的技巧

    作者 | 张祖优(Fooying)  腾讯云 云鼎实验室 对于XSS的漏洞挖掘过程,其实就是一个使用Payload不断测试和调整再测试的过程,这个过程我们把它叫做Fuzzing:同样是Fuzzing, ...

  4. iOS9,10没有问题,iOS8上面一登录就崩溃,原因Assets的问题

    在项目中开发中,打包成一个ipa的包,发现iOS9,10,运行非常流畅,iOS8上面一运行就崩溃,找了好久,才找到原因竟然是Assets的问题,一开始我把ipa包放在蒲公英上面托管扫码下载的,用iTu ...

  5. Yii2高级模板vendor和application非同级目录部署

    上面是Yii2的高级模板,当我们有多个application的时候,这种高级模板可以可以提供很好的扩展性,多个application共用一份YII2框架,默认情况下,框架和application是在同 ...

  6. String 类的实现(2)深度拷贝详解

    我们已经知道了浅拷贝存在的问题,即多次析构同一空间.这个问题是类的成员函数引起的,就是前面浅拷贝里相当于编译器自动合成的函数,确切的说,浅拷贝里的问题是由隐士拷贝构造函数和隐士赋值运算符引起的. 拷贝 ...

  7. 性能测试培训:批量执行Jmeter脚本之ant调用

    性能测试培训:批量执行Jmeter脚本之ant调用   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.在poptest的load ...

  8. 高吞吐koa日志中间件

    Midlog中间件 node服务端开发中少不了日志打点,而在koa框架下的日志打点在多进程环境中日志信息往往无法对应上下文,而且在高并发下直接进行写buffer操作(内核调用writev)也会造成内存 ...

  9. 【转】windows浏览共享切换用户登录的方法

    1.打开 “我的电脑” 单击 菜单栏“工具”,选择“映射网络驱动器” ,单击“使用其他用户名”,在弹出的对话框之中输入用户名.密码2.遇到提示“您已经建立连接,不能重复连接”类似信息的时候,在开始-运 ...

  10. 微软 深度学习 cntk ,我目前见过 安装方式最简单的一个框架,2.0之后开始支持C# 咯

    嗨,你也是我这种手残党么?之前试着安装着mxnet和tensorflow,但是因为时间比较短所以往往来不及安装完就失去兴趣,今天看到微软的cntk可以用了,一次性安装好了,并且测试通过 本人环境: W ...