1379 DNA Sorting

来源:互联网 发布:阿里云ddns客户端 编辑:程序博客网 时间:2024/05/01 09:04

DNA Sorting

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


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
110 6AACATGAAGGTTTTGGCCAATTTGGCCAAAGATCAGATTTCCCGGGGGGAATCGATGCAT
 

Sample Output
CCCGGGGGGAAACATGAAGGGATCAGATTTATCGATGCATTTTTGGCCAATTTGGCCAAA

题意:

输入几组字符串,统一长度已经给出,数量也已知,将这些字符串按自己的逆序数(不知道逆序数?那就百度吧...)从大到小输出........

刚开始没看明白题很久没做出来,后来才发现,这么简单的题.......


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct str//这里用结构体挺方便的{    char x[105];    int y;}s[105];int cmp(str a,str b)//结构体排序函数{    return a.y>b.y;}int main(){    int t,i,j,k,len,n;    scanf("%d",&t);    while(t--)    {        memset(s,0,sizeof(s));        scanf("%d%d",&len,&n);        getchar();//注意        for(i=0;i<n;++i)        {            scanf("%s",s[i].x);//输入字符串            for(j=0;j<len;++j)//这个双循环直接就开始计算逆序数的值            {                for(k=j+1;k<len;++k)                {                    if(s[i].x[k]>=s[i].x[j])//遇到满足后面小于等于前面的,就加 1 ;                    {                        ++s[i].y;                    }                }            }        }        sort(s,s+n,cmp);//排序        for(i=0;i<n;++i)//输出        {            printf("%s\n",s[i].x);        }    }    return 0;}



0 0
原创粉丝点击