pku 1007 一次AC(posted by biggates)

本文介绍了一个用于评估DNA字符串“排序性”的算法,并通过计算每条DNA字符串中的逆序对数量来实现排序。输入包括字符串长度及待处理的DNA序列集合,输出则是按“最有序”到“最无序”排列的DNA序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

半个小时, 一遍AC,真高兴啊

下文为源代码,一些注释是在测试时方便观察和比较用的,大家酌情看吧

/*

 

DNA Sorting

Time Limit:1000MS  Memory Limit:10000K

Total Submit:9694 Accepted:3892

 

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<stdlib.h>

#include<memory.h>

 

//////////////////////////////////////////////

//#include<time.h>

//////////////////////////////////////////////

 

const int MAX_LENGTH = 51;

const int MAX_TIMES = 100;

 

int times = 0;

int length = 0;

 

//int tempSortRate[MAX_LENGTH];

 

struct sorting

{

    int index;

    int sortrate;

};

 

inline int cmp(const void* a, const void* b)

{

    return ( (sorting*)a )->sortrate - ( (sorting*)b )->sortrate;

}

 

 

//getSortRate:在以head为首的字符中查找从第n个开始的排序混乱度

//n从0开始

//由于每次都要搜索n之后的所有字符,因此用DP

//信息保存在tempSortRate中,

//又:所有的A都是不产生排序混乱度的

 

 

/*

int getSortRate(char* head, const int n)

{

    int SR = 0;

 

    char comparation = *(head + n);

 

    switch(comparation)

    {

    case 'A':

        tempSortRate[n] = getSortRate(head, n+1);

        return tempSortRate[n];

        break;

    case 0:

        tempSortRate[n] = 0;

        return 0;

        break;

    default:

        break;

    }

 

    if(tempSortRate[n] != -1)

    {

        return tempSortRate[n];

    }

 

    SR = getSortRate(head, n + 1);

 

    if( *(head + n + 1) < comparation && *(head + n + 1) != 0)

    {

        SR++;

    }

 

    tempSortRate[n] = SR;

 

    return SR;

}

 

void initTSR(void)

{

    memset(tempSortRate, -1, sizeof(tempSortRate) );

}

*/

 

//对于每个不同的字符串,都要清空一次tempSortRate

 

 

int getSortRate(char* head, const int n)

{

    int i = 0;

    int j = 0;

    int SR = 0;

    for(i = 0; i < length - n; i++)

    {

        for(j = i; j < length - n; j++)

        {

            if( *(head + n + j) < *(head + n + i) )

            {

                SR++;

            }

        }

    }

 

    return SR;

}

 

 

 

int main()

{

    int i = 0;

    char buffer[MAX_TIMES][MAX_LENGTH];

    sorting sorted[MAX_TIMES];

 

    memset(buffer, 0, sizeof(buffer) );

    memset(sorted, 0, sizeof(sorted) );

 

    scanf("%d %d", &length, &times);

 

    //////////////////////////////////////////

    //clock_t start, finish;

    //start = clock();

    //////////////////////////////////////////

 

    for(i = 0; i < times; i++)

    {

//        initTSR();

        scanf("%s", buffer[i]);

 

        sorted[i].sortrate = getSortRate(buffer[i], 0);

        sorted[i].index = i;

 

    }

    //for(i = 0; i < times; i++)

 

    qsort(sorted, times, sizeof(sorting), cmp);

 

    for(i = 0; i < times; i++)

    {

        printf("%s/n", buffer[ sorted[i].index ]);

 

        //printf("%s %d/n", buffer[ sorted[i].index ], sorted[i].sortrate);

    }

 

    //////////////////////////////////////////

    //finish = clock();

    //printf("The Program is finished in %dMS(s)./n", finish - start);

    //////////////////////////////////////////

 

    //system("pause");

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值