DNA Sorting

来源:互联网 发布:网络驱动卸载了怎么办 编辑:程序博客网 时间:2024/06/06 10:52

 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

1

10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT

Sample Output

CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA

题目大意:有一种测量字符串长度的规则,就是计算一串字符串的每个字符比它右边多少个字符大
如:ZWQM,Z比W、Q、M大,W比Q、M大,Q比M大,那么该字符串的长度就为3+2+1=6。
输入几个DNA串,按如上规则由长度小到大排序。如果长度相同就按输入的顺序输出。

思路:计算每个串的长度保存在一个数组里。因为每个串最多50位,那么它的长度最多是49+48+..+1 = 1225。那么我们可以暴力输出。定义变量i 由 0 到 1225递增。再遍历所有串的长度,如果串的长度 = i,就输出该串。

#include <stdio.h>#include <string.h>int main(){    int N;    scanf("%d",&N);    char dna[105][55];    int measu[105] = {0};    int counter = 0;    while (N--)    {        counter++;        memset(measu,0,sizeof(measu));        memset(dna,0,sizeof(dna));        int n,m;        scanf("%d%d",&n,&m);        for (int i = 1; i <= m; i++)        {            scanf("%s",dna[i]);        }        for (int p = 1; p <= m; p++)//计算每个串的长度        {            for (int q = 0; q < n; q++)            {                for (int k = q+1; k < n; k++)                {                    if (dna[p][q] > dna[p][k]) measu[p]++;                }            }        }        int cut = 0;        for (int i = 0; i < 1226; i++)//暴力输出        {            for (int j = 1; j <= m; j++)            {                if (measu[j] == i)                {                    printf("%s\n",dna[j]);                    cut++;                }            }            if (cut == m) break;//当所有串都输出后终止循环        }        if (counter > 1)        printf("\n");    }    return 0;}
原创粉丝点击