[POJ] 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'' toleast 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

Source
East Central North America 1998

解题思路

首先求出逆序数,然后对逆序数进行排队,最后输出排序后的DNA序列。对于求逆序数,只需对序列进行遍历,找出本应出现在某字符之后,却出现在了它之前的字符数即可(顺序为ACGT)。

实现代码

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

typedef struct node
{
    char s[100];
    int value; //逆序对数
}DNA;

bool cmp(DNA d1, DNA d2)
{
    return d1.value < d2.value;
}

//获取逆序对数
int rev_cnt(char *ch)
{
    int cnt[4] = {0};
    int reverse_cnt = 0;
    int i = 0;
    while(ch[i])
    {
        switch(ch[i])
        {
        case 'A':
            cnt[0]++;
            reverse_cnt += cnt[1] + cnt[2] + cnt[3];
            break;
        case 'C':
            cnt[1]++;
            reverse_cnt += cnt[2] + cnt[3];
            break;
        case 'G':
            cnt[2]++;
            reverse_cnt += cnt[3];
            break;
        case 'T':
            cnt[3]++;
            break;
        }
        i++;
    }

    return reverse_cnt;
}

void sort(DNA *d, int len)
{
    for (int i = 0; i < len - 1; i++)
    {
        int index = i;
        for (int j = i + 1; j < len; j++)
        {
            if (d[j].value < d[index].value)
            {
                index = j;
            }
        }

        if (index != i)
        {
            //DNA temp;
            //memcpy(&temp, &d[i], sizeof(d[i]));
            //memcpy(&d[i], &d[index], sizeof(d[index]));
            //memcpy(&d[index], &temp, sizeof(temp));
            DNA temp = d[i];
            d[i] = d[index];
            d[index] = temp;
        }
    }
}

int main()
{
    int m, n;
    scanf("%d%d", &m, &n); //m为输入字符长度,n为数据组数
    DNA *d = new DNA[n];
    int i = 0;
    while(i < n)
    {
        scanf("%s", d[i].s);
        d[i].value = rev_cnt(d[i].s);
        i++;
    }

    sort(d, d + n, cmp);
    //sort(d, n);自定义排序函数
    for (int i = 0; i < n; i++)
    {
        cout<<d[i].s<<endl;
    }

    delete [] d;
    return 0;
}

排序部分可以用<algorithm>里面的sort,也可以是自定义的。输入部分如果将scanf改为cin.getline(d[i].s, m+1)则需要先用一个scanf("%c", &c)读取两个整数之后的回车符。


转载:http://blog.csdn.net/foreverling/article/details/44559603

目录
相关文章
|
前端开发 数据库连接 Go
Go Web 开发 Demo【用户登录、注册、验证】(1)
Go Web 开发 Demo【用户登录、注册、验证】
|
存储 数据安全/隐私保护 时序数据库
InfluxDB权限配置
InfluxDB权限配置
377 1
|
JavaScript 前端开发 索引
LayUI前框框架普及版(二)
LayUI前框框架普及版
208 0
|
文字识别 开发工具
印刷文字识别操作报错合集之请求返回503是什么原因
在使用印刷文字识别(OCR)服务时,可能会遇到各种错误。例如:1.Java异常、2.配置文件错误、3.服务未开通、4.HTTP错误码、5.权限问题(403 Forbidden)、6.调用拒绝(Refused)、7.智能纠错问题、8.图片质量或格式问题,以下是一些常见错误及其可能的原因和解决方案的合集。
|
SQL 关系型数据库 MySQL
mysql中kill掉所有锁表的进程
mysql中kill掉所有锁表的进程
252 0
C4.
|
存储 数据安全/隐私保护 C++
C++的claas变量
C++的claas变量
C4.
114 0
|
小程序 搜索推荐 前端开发
微搭低代码自定义组件开发教程
微搭低代码自定义组件开发教程
微搭低代码自定义组件开发教程
|
SQL 消息中间件 分布式计算
完美避坑!记一次Elasticsearch集群迁移架构实战
Elastic自身设计了集群分片的负载平衡机制,当有新数据节点加入集群或者离开集群,集群会自动平衡分片的负载分布。
|
Ubuntu Shell Linux
博主日常工作中使用的shell脚本分享
博主日常工作中使用的shell脚本分享
312 0
博主日常工作中使用的shell脚本分享