首页 文章详情

poj 1007 DNA Sorting

C语言题库 | 262 2021-10-12 13:21 0 0 0
UniSMS (合一短信)

DNA Sorting


Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 122100
Accepted: 48702

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



DNA排序


描述

在一个序列中,“无序”的一种度量方法是对彼此无序的条目的数量。例如,在字母序列'' DAABEC''中,这个度量是5,因为D大于它右边的四个字母,而E大于它右边的一个字母。这个度量称为序列的逆序数。序列“AACEDGG”只有一个逆序(E和D)——它几乎是有序的——而序列“ZWQM”有6个逆序(它是尽可能未排序的——正好是有序的逆序)。


你负责编目一个DNA串序列(序列只包含四个字母a, C, G,和T)。然而,你想要编目他们,不是按字母顺序,而是按“排序”的顺序,从“最排序”到“最排序”。所有的字符串都是相同的长度。


输入

第一行包含两个整数:一个正整数n (0 < n <= 50)给出字符串的长度;一个正整数m (0 < m <= 100)表示字符串的数量。后面是m行,每一行包含一个长度为n的字符串。


输出

输出输入字符串的列表,排序从''最排序''到''最小排序''。因为两个字符串可以相等地排序,然后根据原始顺序输出它们。


Sample Input

10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT

Sample Output

CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA

TTTGGCCAAA




思路:分别计算逆序数,然后因为个数太少,可以用冒泡排序,然后输出

,记得用结构体


代码:

#include<stdio.h>
#include<string.h>
#define M 200
struct dna
{
char str[M];
int ans;
};
struct dna d[M];
struct dna t;
void main()
{
int n,m,i,j,k;
scanf("%d%d",&n,&m);
for(i=0;i<m;i++)
{
scanf("%s",d[i].str);
d[i].ans=0;
for(j=0;j<n;j++)
{
for(k=j;k<n;k++)
if(d[i].str[j]>d[i].str[k])
d[i].ans++;
}
}
for(i=0;i<m;i++)
for(j=i;j<m;j++)
{
if(d[i].ans>d[j].ans)
{
t=d[i];
d[i]=d[j];
d[j]=t;
}
}
for(i=0;i<m;i++)
{
printf("%s\n",d[i].str);
}


}


good-icon 0
favorite-icon 0
收藏
回复数量: 0
    暂无评论~~
    Ctrl+Enter